[SOLVED] Open File Not by Name but by File Type?

When I'm logging into Critrix, a file gets downloaded to my folder. I want to target this file and open it.

The file name is different every time, BUT it'll always have the same file extension and it'll always be just one file with that extension in the folder.

I tried to use the Open action, and I can see how you use it for specific files - don't know what's the right syntax to identify/ open a file based on its extension. Of course, perhaps the Open action is not even the right way of going about this - let me know if I need a nudge in a different direction.

Thanks for your time, as always!

Just a thought, I can't test right now and I'm not sure what you mean by, "it'll always be the same file with that extension in the folder". Do you mean there will only ever be one file with that extension in the folder?

If that's what you mean, I think you could match the filename with a Regular Expression that was something like "*.ext" where ".ext" is the filetype extension and the wildcard * matches anything.

I think what you want to do is associate that particular file extension with your particular application. You do that by using the Get Info command (⌘I) and setting the Open With popup to your application, confirming you want all files of this type to be opened with that application.

Then the Keyboard Maestro Open action will open files having that extension with that application.

I must have explained it incorrectly. I download a file that is always different in name, but the folder will always have 1x of these file extensions in it - so I want to open the file not by name (which I can't code/ predict), but by its extension (which is always the same)

thanks for suggesting this - I tried to add the Asterix to the file path but nothing happened... :frowning:

Instead of describing it, give an example.

I’m not at a computer right now but you could use the for each action to sort through each file, get its extension using the get file attribute action, and if it’s extension is the one you’re looking for, open file and break from loop.

Hope that makes sense because I’m on my phone so can’t give much more details at the moment. But if it doesn’t, I can provide more info later on.

-Chris

I'll be honest, you suggested some actions that I've never used - here's my best guess of how to throw that together (and it doesn't work). Perhaps I missed something easy?

Also, is this not super resource intensive to iterate through the entire folder? Is there no way of just telling KM to open all files in a folder with a particular extension (in this case, 1)?

image

Instead of Get file type, use Get extension. Then it will work. (File type would return a value like "File" rather than ".ica"

To find a file with a certain extension in a folder full of other files with different extensions, whatever method you use will need to iterate through the files until a match is found.

many thanks for looking into this. I changed that one option, but unfortunately it's still not working.. any other thoughts?

Instead of shortened '~/Downloads' try the full path.

Try changing "Open with default application" to the "Open with" and pick the application you want to use. If that still doesn't work try adapting the below (it works at the moment to open a .txt file with TextEdit and uses local variables so doesn't leave clutter behind after running).

EXAMPLE Open File in a Folder Matching Extension.kmmacros (4.2 KB)

Click to Show Image of Macro

In addition to the other comments, this variable needs to be wrapped... in other words it should be %Variable%item%.

Other than that, it should work. That’s the exact process I had in mind earlier. :+1:t2:

Action screenshot (click to expand/collapse)

1 Like

This is it!!

That fixed it when I followed @Zabobon's instructions! Thanks so much guys for the patience and for helping me on this learning journey.

Couple of things - how would you as experts troubleshoot / debug this? (e.g. is there a way to step through this and see the variables, etc?)

@cdthomer why does this variable there have to be wrapped, but not when I call on my other "fileext"? What's the heuristic I should use in the future to avoid mistakes?

@Zabobon you used LOCAL__File - I can actually see this in the dropdown, along with a few other variables named in the same way. Are these preferable to use?

Any Variable named with "local" as part of its name will only exist for that one Macro run and be cleared out afterwards. Keyboard Maestro Variables named like your originals "filext" will persist which can be good if you want to use those same stored values in other Macros. It is considered good practice to use local Variables for tasks where you only need the values for a single Macro run so that you don't end up with a mess of hundreds of stored Variables.

https://wiki.keyboardmaestro.com/manual/Variables

Yep, the mess makes sense now :smiley: I'm grateful for pointing this out @Zabobon

Make sure you understand the difference between:

Text and Numeric Fields

Different fields with have different indicators when the cursor is in the field:

image

I'd go even further and say it's good practice to always use Local variables unless you need another type for a particular reason, eg persistence.

For example, using Locals means you don't have to worry about accidentally using the same variable name in different macros -- if those were Globals and the macros were to run simultaneously you could have Macro A setting a value, Macro B changing it, then Macro A using the wrong value...

@NoorTheGrey -- you'll also see @Zabobon and others using the "double-underscore" separator: Local then __ then name. That's a neat trick because in many places, like the "Prompt for User Input" action dialog, the double-underscore "hides" itself and everything before it from the user.

I was late in realising that, and find it hard to shake the "single-underscore" habit -- and harder still to spot when I've used which in a macro. If you always use the double-underscore you'll make your life a lot easier!

2 Likes

Ah, and I thought it was just a stylistic preference!! many thanks for pointing this out - will definitely do this from now on.

If/when it works, consider posting a screenshot of what you ended up with, so people don't need to read through all this text @NoorTheGrey :wink::+1: