I have a basic question that I couldn’t find the answer to. Is there a way to set the default application (the “always open with” app) of any given file to any arbitrary application? I can see how this could be done with GUI scripting, but it would be preferable if there were a programatic way to do it. The “info for” command “gets” the default application, but I don’t know of any way to “set” it. And the “Finder” and “System Events” properties of a file don’t include a “default application” property. Am I missing something simple?
It’s easier to use the default binding than to set the strong binding of a file. You can set the default binding by just changing the creator or file type of the item. To change the strong binding, you would need to write to the resource fork of the file.
One disadvantage in using the default bindings is that some apps don’t have their own creator type (signature). Another is if a file has a weak binding (set in the LaunchServices preferences), you would need to delete that item from the plist. Lastly, who knows what Apple will do in the future.
To set the strong binding, you would have to find out what kind of data to place in the resources. I haven’t found exactly what data should be written to the resource fork yet. You would have to do a lot of experimenting.
Thank you very much for the information about setting a default application. Hmmmmm… Not so straighforward.
I’ll be specific about my problem:
I have text files whose content is "osascript"s that either execute AppleScript commands line-by-line (-e switch) or saved AppleScript scripts. I then convert those text files to double-clickable Unix executables using Apple’s “Launcher” utility application. (By the way, the reason for this is to gain the enhanced execution speed of "osascript"s relative to applets opened via the Finder where speed is important.) The problem I’m trying to solve is to programmatically convert the text files to “Launcher” documents without having to do the usual Get Info > Open with > Other … > etc until you finally dig down into the “Launcher” utility, which is tedious. I’ve noticed that the file creator and type don’t change after converting from text to “Launcher” document. (I wonder if this is an example of what you said, namely that “some apps don’t have their own creator type (signature).”) So I’m not sure how I might accomplish the task using the default binding.
Any thoughts you might have are greatly appreciated.
I’m not sure I understand your ‘situation’ exactly, but it occurred to me you might be able to use ‘RCDefaultApp’ to create an extension-based binding to Applet Launcher for your text files…
Downloaded Launcher. It has a creator type “Lnch”. So something like this should work:
set f to choose file
tell app “Finder” to set creator type of f to “Lnch”
I wrote a simple test script:
#!/bin/sh
echo ‘hello’ > ~/Desktop/new.txt
made it executable, and changed the open with to Launcher. When I double clicked the script, there seems to be a bug. The text file "new.txt’ doesn’t appear until I click on the desktop and click back onto the script. Also, my scripts name is “LaunchTest.sh”. When I get info on a duplicate of it (non-executable), the open with is set to Launcher(default) automatically. It seems that the extension .sh is set to be a Launcher defualt extension. In other words, you might just have to add extensions to your files. Personally, I don’t like this. I rather just use the creator type or the strong bindings (open with - not all files of this type). I have to look into this later.
You are the man! I saved a sample “osascript” text file without any extension (the contents of which was simply: osascript -e ‘say “Hello”’), changed the creator type to “Lnch” using the AppleScript you suggested, used Get Info to make Launcher the default application for all such files (“Change All”), and double-clicked it to confirm that it would execute properly (it said “Hello”). I then created several other “osascript” text files, again without an extension, and changed their creator types to “Lnch”. Presto! They all executed via Launcher when double-clicked. Furthermore, text files whose creator type I didn’t change opened in their respective text editors as desired. By the way, the “Change All” dialog that opened was very informative as to which files will have “Launcher” as their default application:
...This change will apply to all Launcher documents with type "TEXT"...
(I take “all Launcher documents” to mean those of creator type “Lnch”). You solved my problem. Thank you so much. (BTW, is it possible that the “.sh” file extension in your script somehow creates a conflict with Launcher? I believe that extension is used for Unix scripts. Perhaps my omission of an extension obviates any potential conflicts. Just a thought.)
Peter,
I looked at RCDefaultApp, and it does appear to be an effective solution to the problem of creating default applications. Since my needs are very specific, I’ll probably stay with the solution kel outlined for now. But it’s nice to know that application exists in case of future need.
Actually, you didn’t do it exactly how I said to. You’re not supposed to change all or even do anything in the info window. All you had to try first was to change the creator type. If you ever need to change it back, repost and I’ll tell you how to delete the entry in the com.apple.LaunchServices.plist. When you changed all, you did a weak binding and the message in the change all dialog is confusing. It doesn’t mean what you think it does. What it meant was that, now that you changed all documents of that kind to Launcher, all documents of that kind are Launcher documents. So, if things happen like text documents opening with Launcher, then you need to do the plist thing. But you changed the creator type too, so I’m not sure what will happen.
Reading the read me for Launcher, it says that you can change the extensions it handles. What the system did was read the info.plist in the Launcher bundle. LaunchServices then assigned Launcher to that extension. I think if I delete that extension from the info.plist for Launcher, it will revert back to Terminal or whatever it was.
I was just about to post again because I did in fact run into a problem, then I saw your post. When I changed just the creator type to “Lnch”, double-clicking did not open the file with Launcher but rather with the text editor that created the file (in this case, TextWrangler). After I did the Get Info > Change All thing, then I got the behavior as described in my last post. However, I now have a new curious behavior: when I create a text file using one text editor (Word, TextEdit, or TextWrangler) and edit it in only its original editor, it opens in its native text editor when double-clicked; but when I open it in a different text editor, modify it and save it, it is then opened by Launcher when double-clicked! So you were right, things are a bit messed up. One other observation: a file created without a file type or creator type using the “touch” command in Terminal, which is then given the creator type “Lnch”, does open in Launcher dependably. Is that the safest/most reliable way to create such “osascript” files?
When I want to create a Launcher executable document, create it with no file type or creator type using the Terminal nano text editor, then convert it’s creator type to “Lnch” in AppleScript
This does result in an executable Launcher document, and I don’t seem to be having any problems with text editors creating Launcher documents.
All files have file type and creator type. Some like files created with TextEdit use the null character. e.g. using unix ‘touch’ command, you can see this:
do shell script "touch ~/Desktop/clean.txt"
set f to choose file -- chose the new file
set c to file creator of (info for f)
{c, length of c, c contains (ASCII character 0)}
I’m trying to think of a good way to have Launcher not be so imposing. I would delete all the entries in the com.apple.LaunchServices.plist for Launcher. These were entered there because of the change all option in the info window. I would drop the creator type plan, since it seems like it doesn’t work. You can change all the files you changed with:
set f to choose file
set z to ascii character 0
set z to z & z & z & z
tell app "Finder" to set creator type of f to z
Some files you created with file type “TEXT” and had a different creator like for TextWranggler. Leave those as is or reset to what it was (creator type for TextWranggler).
Since you’re not using extensions on your unix scripts, they’ll still run I think if you use the first line beginning with “#!/bin/foo”. I have to read the read me for Launcher again.
I guess you know how to delete the entries in the plist.
Isn’t this fun! It’s like a big jigsaw puzzle. Hope we’re not missing any pieces.
Thank you for the insight into the use of null characters in file types and creator types.
I suppose for now I might create a unique file extension and do a Get Info > Open with Launcher > Change All for that extension (as Peter suggested). If I learned from you properly, that should create a weak binding for that file extension. I can live with that. Would that there were a tell application “Finder” to tell file so-and-so to set default application to so-and-so command!
I was thinking the same thing. Maybe .launcher or .lnch or something like that. Part of the problem I was having was that on my system, TextEdit is not set as the default applicaiton for .sh, so it wasn’t overriding Launcher’s ‘none’ role for .sh extensions. There was no default application for .sh and what I was reading in the read me didn’t match my system.
After you’ve set up a default application for the .lnch extension (change all), you won’t need a “tell application “Finder” to tell file so-and-so to set default application to so-and-so command!”. All you would need to do is change the extensions for the scripts (text files) that you want to open with Launcher to .lnch or whatever.
on open these_files
repeat with this_file in these_files
set n to name of (info for this_file)
tell application "Finder" to set name of this_file to (n & ".lnch")
end repeat
return
end open
It just appends .lnch to the name. Sounds like a good plan to me.
Just thought of something. Make a toggler. If the dropped file ends with .lnch, then strip it. Otherwise, append it.
on open these_files
repeat with this_file in these_files
set n to name of (info for this_file)
tell application "Finder"
if n ends with ".lnch" then
set new_name to text 1 thru -6 of n
else
set new_name to (n & ".lnch")
end if
set name of this_file to new_name
end tell
end repeat
return
end open
The first time run, the system asks for a default application. After the first run, every file with extension “.lnch” opens with Launcher. This way, you don’t have to edit Launcher’s info.plist, although I did delete most of the interpreter extensions in the info.plist.
Yes, things are working well on this end too. Thanks for putting together the droplet and for all your help on this. I now have a new weapon in the armamentarium: osascripts that are double-clickable Launcher documents, execute AppleScript code at potentially far greater speed than applets opened by Finder, and run in the background (by the very nature of osascript behavior.) Granted there are disadvantages, not the least of which is the lack of returned values or “execution completed” messages from the osascripts, but for certain tasks, they are just the right answer.
Oops. Instead of: “[osascripts]…run in the background (by the very nature of osascript behavior)”, I meant to say: “[osascripts]…run in the background (by the very nature of Launcher document behavior)”.