Set the "Open With" of a Finder selection?

I have a handler that uses GUI scripting to set the “Open with” of a file to the handler’s argument (an application name). It no longer works. (I sent a message to applescript-users about this.)

Is there a way to set the “Open with” of a file directly with AppleScript, ASObjc, or JXA?


Thanks Chris (@ccstone),

I was running around in circles, trying to figure out how set the Open With property of a file.


Your script in that thread looks like it may be just what I need.

#  Directly set Open-With for a given file.

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set theItem to item 1 of finderSelectionList

set theApp to alias ((path to startup disk as text) & "Applications:App-Store-Applications:Archive_Tools:The Archive")

tell application "System Events" to set default application of theItem to theApp

I've been working on a macro to set the icon for a file and record some information in the Comments, and both of those could be set with Finder in AppleScript:

But the default application was sadly, frustratingly not in that list. Your script let me know to look in System Events, which did have that property:

May I ask you some questions about your script? I know it's been seven years, but hopefully they're not too obscure.

First, you have the term "alias" in that script in two places. I don't understand the need for that because I don't understand what it does, either in general or here. Would you give me some clues?

Second, the script creates the path to the desired application in what I think of as the "old style" path format. What would it take to specify the path with slashes instead of colons? Is the slash-delimited form a POSIX path, or is POSIX a special case?

Third, your script has list from the Finder selection, but it looks like it only ever processes one item. Is that intentional or did you mean to process all selected files? How would you do that?


Hi @August - I know your query is directed at @ccstone but since he’s been absent for a little while I thought I’d try to help out.

I can’t pretend to approach Chris’s experience with AppleScript but here goes:

For your first two points I’d advise you to consult Apple’s documentation on Aliases and Files here AppleScript Fundamentals

As for your third point, it would be usual to set the default app for opening a file one at a time which is why the script processes just the first item selected in Finder and since I would code it similarly this is, I would say, deliberate. The way to get what’s selected in Finder is by the selection word which returns a list, so you have to assume the list contains zero or more items and if more than one assume the first is, for the purposes of this script, the one you’re trying to set the default app for. Should you wish to process all the selected Finder items in other scripts, then you’d need to enclose the relevant code in a Repeat with loop. This is documented here Control Statements Reference

Now, I am no AppleScript expert and I only stand on the shoulders of giants so should you have any follow-up questions I may not be able to help further :man_shrugging:

Thanks Tiffle,

I hope that it's understood in this public forum that while thanks may be directed at an individual, questions are for everyone to hear, as are answers, hopefully.

Thanks for your clear answer about lists and using simply the first item of the list without any Repeat directive. It now makes sense to do it that way.

I have been banging around on AppleScript for over two years and I had not found that Apple doc yet. (I'm bewildered at that idea, but there it is.) In case anyone finds this thread because they are asking similar questions, here is the answer to my questions about aliases and POSIX paths that I found in that doc:

An alias object is a dynamic reference to an existing file system object. Because it is dynamic, it can maintain the link to its designated file system object even if that object is moved or renamed.

A file object represents a specific file at a specific location in the file system. It can refer to an item that does not currently exist, such as the name and location for a file that is to be created. A file object is not dynamic, and always refers to the same location, even if a different item is moved into that place. The POSIX file pseudo-class is roughly synonymous with file: POSIX file specifiers evaluate to a file object, but they use different semantics for the name, as described in Specifying Paths.

The following is the recommended usage for these types:

  • Use an alias object to refer to existing file system objects.
  • Use a file object to refer to a file that does not yet exist.
  • Use a POSIX file specifier if you want to specify the file using a POSIX path.

Thank you so much!

1 Like

This is the only way I know to do it:

Set Selected File Default App.kmmacros (24 KB)

Macro screenshot

1 Like

Thanks @noisneil,

That's exactly what I was thinking of in the first place, and which I got started with on my own. And then I found this thread from 2016 with its use of default application as a System Events property and got excited about being able to do it in AppleScript. For some macros, I love watching them work as they open menus and enter text and do all kinds of stuff for me. And for other macros, I prefer to have it just happen transparently, without showing context menus, etc.

Thanks for completing this route.

1 Like

This summary from the Apple doc provided guidance, but not a full answer.

What I was trying to do to was pass a variable identifying the Finder selection from KBM, where the hotkey is a trigger, to AppleScript, which tells System Events to change the default application parameter.

It just wasn't working for me.

What turned out to be the issue is that the KBM variable is text, and unfortunately you can't turn text directly into an alias, you have to go through converting the text to a POSIX file name first.

When I did this, going directly from text to alias:

set kmInst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine" to ¬
	set local_KBMFile to getvariable ¬
		"local_ChosenIconFilename" instance kmInst

set selectedFile to local_KBMFile as alias

tell application "Finder"
	set fileName to name of selectedFile
end tell

I got an error of:

Execute an AppleScript failed with script error: text-script:205:210: execution error: Can’t make "/Users/amohr/Documents/Personal/DeskSpaceIDs/[K] Keyboard Maestro ⌥⌘K .rtf" into type alias. (-1700).

But when I added the explicit change from text to POSIX first:

set kmInst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine" to ¬
	set local_KBMFile to getvariable ¬
		"local_ChosenIconFilename" instance kmInst

set selectedFile to POSIX file local_KBMFile as alias

tell application "Finder"
	set fileName to name of selectedFile
end tell

It worked as I had hoped.

1 Like