how to change "open with" defaults via command line?!?

folks,

i know that if i get info on a file -- in this case, a filemaker 5.5 file -- i can choose the default "open with" settings and apply those settings to all files of that kind. see the attached image for what i mean.

but how do i do that via the command line?

thus far, here's what i've got: i know that i can open a document called "scrubbing shell.sh" on my desktop WITH either MS Word of TextEdit by calling:

open -a textedit /Users/david/Desktop/scrubbing-shell.sh

or

open -a Microsoft\ Word /Users/david/Desktop/scrubbing-shell.sh

no sweat. BUT....how do i set the defaults so that it ALWAYS opens with that app in the future when double-clicking via the GUI?

many thanks, ya'll!

david koff
associate systems admin
the j. paul getty trust
dkoff AT getty DOT edu

macbook pro intel 2 dual core, Mac OS X (10.4.9)

Posted on Apr 26, 2007 2:28 PM

Reply
17 replies

Apr 27, 2007 3:08 PM in response to the razor

some updates to share:

#1) launchservices, a framework, seems to keep track of this information.

#2) for more specific information go here: http://www.macosxhints.com/article.php?story=20031215144430486

#3) one suggestion was to play with this file via command line by calling this line in terminal:

defaults read com.apple.LaunchServices LSHandlers

this spits out a list (not too long) of roles and handlers. i guess the idea would be to take the current scheme for filemaker 8.5 which looks like this:

<snip>
{LSHandlerRoleAll = "com.filemaker.pro85"; LSHandlerURLScheme = fmp7; },
</snip>

...and adding another line for filemaker 5.5

any suggestions on how to do that?

Apr 27, 2007 7:59 PM in response to the razor

Hi David,
That's some nice research. As a matter of further interest, your personal preferences for LaunchServices are stored in the file, ~/Library/Preferences/com.apple.LaunchServices.plist. This file is XML, which is plain text. It can be edited with any text editor, UNIX or otherwise, although the editor cannot insert any formating into the file. Of course, as you've learned, the "defaults" command will also write to that file.

On the other hand, what to put in that file is another matter altogether. To my knowledge, Apple hasn't documented the interpretation LaunchServices makes of the entries in that file. We've figured out some stuff about the old syntax. For instance in the top level key for each "entry", an "E:" precedes the filename suffix for which the preference applies. Someone even figured out how the LSBundleLocator data was encoded. I think it was base64. Fortunately the new syntax is much simpler and contains no data keys. All keys in the new syntax are strings. I haven't studied the new syntax but you don't have to know everything.

Of course you don't necessarily have to produce the information from scratch; there's lots already in the file. For instance, if you copy one entry and paste it back in below, then change the filename suffix to which it applies, you will have created a new preference that would cause files whose name ends with that suffix to be opened by the same application as the original entry.

The defaults command can insert data in this file but it is difficult to make it write deep entries. One way I've found to do this is to write an entire entry at once. You can use a notation like the output of the defaults command or you can pass it raw XML. Because I know XML, I prefer the latter but you have a choice. As an example, on my machine the following command will insert an entire entry:

defaults write com.apple.LaunchServicescopy LSPrefsBindings -dict-add 'C:CARO;T:PDF' '<array>
<dict>
<key>LSBundleIdentifier</key>
<string>com.adobe.acrobat.reader</string>
<key>LSBundleLocator</key>
<data>
AAAAAADaAAMAAAAAt0av6gAASCsAAAAAAAn0MAAJ9CQA
ALmskYAAAAAAASD//kFQUExDQVJP/////wABABAACfQw
AAn0LgABTAQAAASwAA4AJgASAEEAYwByAG8AYgBhAHQA
IABSAGUAYQBkAGUAcgAgADUALgAwAA8ACgAEAEcAYQBy
AHkAEgBFQXBwbGljYXRpb25zL0Fjcm9iYXQgUmVhZGVy
IDUuMC5hcHAvQ29udGVudHMvTWFjT1MvQWNyb2JhdCBS
ZWFkZXIgNS4wAAATAAEvAP//AAA=
</data>
<key>LSBundleRoleMask</key>
<integer>-1</integer>
<key>LSBundleSignature</key>
<string>CARO</string>
<key>LSBundleVersion</key>
<integer>329215</integer>
</dict>
</array>'

That entry should cause PDF files to be opened by Acrobat Reader, at least on my machine. It would be interesting to know if the same LSBundleLocator would work on other machines but that's a good reason to learn the new syntax.

There's another important thing I don't know. That is how to make the changes take effect. Certainly they would take effect after a reboot and person preferences should take effect when you logout and log back in. However, there's probably a way to make the changes take effect more quickly and if someone figures that out, please post it.
--
Gary
~~~~
Don't assume that every sad-eyed woman has loved and
lost -- she may have got him.

Apr 30, 2007 5:10 PM in response to Gary Kerbaugh

gary,

you shared some great info here so thank you. i still have a few questions because i'm relatively new to coding and unix:

#1) why - when i open my plist file in textedit or MS word - am i getting a lot of goobledy-gook? i can obviously see some prefs in there but the file is full of other machine code or untranslated characters in between each entry.

#2) where did you come up with the <data> portion, integers and strings of your code? there's nothing in my plist file to suggest this when i open it in a text editor but there IS more info if i terminal in and view the file with a read command like - "read com.apple.LaunchServices"

#3) you spoke of copying and pasting entries from here to there and i can't see how you've cherry picked this out.

many thanks, ya'll.

david

Apr 30, 2007 9:27 PM in response to the razor

Hi David,
Sorry, I should have known you'd have written to it with the "defaults" command. The file is now in binary format. There are also other reasons for it to be so but it doesn't really matter; it's a simple matter to convert it to text. The following command will do that for you.

plutil -convert xml1 ~/Library/Preferences/com.apple.LaunchServices.plist

Please note that this is all one line, no matter how the browser or terminal wraps it.
--
Gary
~~~~
A holding company is a thing where you hand an
accomplice the goods while the policeman searches you.

May 1, 2007 11:54 AM in response to Gary Kerbaugh

gary,

OK, so we're moving along here and your tip to convert the file to XML is, obviously the key to looking at and analyzing the file. only, i've been playing with the GUI interface to change the "open with" settings and NOT seeing any changes in either my plist or the launchservices handlers database.

which is to say:

#1) i've got FMPro 5.5 and 8.5 on my machine.
#2) i've got a FMPRo 5.5 file (.fp5 suffix) on my desktop.
#3) i've used the "open with" command via the GUI in the get info window for that file and chosen a NEW app (fmpro 8.5) to open the file and then applied that change to ALL files of that kind.
#4) i've tested that file by double clicking. the file opens correctly with FMPro 8.5.
#5) i restart.
#6) i monitor no changes to the ~/Library/Preferences/com.apple.LaunchServices.plist or the results of terminal command: defaults read com.apple.LaunchServices LSHandlers
#7) i then put everything back to "open with" for FMPro 5.5, restart and check these two settings again. no dice.

here are the important text bites from these files:

from the "defaults read com.apple.LaunchServices LSHandlers" dump:

{LSHandlerRoleAll = "com.filemaker.pro85"; LSHandlerURLScheme = fmp7; },
{LSHandlerRoleAll = "com.adiumx.adiumx"; LSHandlerURLScheme = aim; },
{
LSHandlerContentTag = fp5;
LSHandlerContentTagClass = "public.filename-extension";
LSHandlerIgnoreCreator = 1;
LSHandlerRoleAll = "com.filemaker.pro85";
}

from the ~/Library/Preferences/com.apple.LaunchServices.plist file:

<dict>
<key>LSHandlerRoleAll</key>
<string>com.filemaker.pro85</string>
<key>LSHandlerURLScheme</key>
<string>fmp7</string>
</dict>
<dict>
<key>LSHandlerContentTag</key>
<string>fp5</string>
<key>LSHandlerContentTagClass</key>
<string>public.filename-extension</string>
<key>LSHandlerIgnoreCreator</key>
<true/>
<key>LSHandlerRoleAll</key>
<string>com.filemaker.pro85</string>
</dict>

you'd think that my .fp5 files would ALWAYS open with FMPro 8.5 given the settings i'm reading here, wouldn't you?

May 1, 2007 2:26 PM in response to the razor

It was in reply to Gary's question:

There's another important thing I don't know. That is how to make the changes take effect. Certainly they would take effect after a reboot and person preferences should take effect when you logout and log back in. However, there's probably a way to make the changes take effect more quickly and if someone figures that out, please post it.

May 1, 2007 6:51 PM in response to the razor

Hi David,
OK, I investigated the new format and treatment. It appears that the first entry in the property list file that matches the file being opened wins. The Finder may still honor the new format; when I moved my old Panther file into Tiger it seemed to work. However, in other ways, the Finder doesn't recognize the old format, which is the format of the FileMaker entries you posted.

The Finder puts all of the new format entries at the beginning of the property list file. If you change a preference that was controlled by an old format entry, the Finder won't edit that entry. What it will do is to create a new entry in the new format closer to the beginning of the file than any of the old-format entries. SInce the new entry comes first, the old one doesn't apply. If you then edit the preference and change it back to what it was before, the Finder will edit the new entry. Consider the following sequence of events and commands:

% cp ~/Library/Preferences/com.apple.LaunchServices.plist ~/Library/Preferences/com.apple.LaunchServices_copy.plist

I change all GIF files to be opened by GraphicConverter instead of Preview, which is an old setting in the old format.

% plutil -convert xml1 ~/Library/Preferences/com.apple.LaunchServices.plist
% diff --text ~/Library/Preferences/com.apple.LaunchServices.plist ~/Library/Preferences/com.apple.LaunchServices_copy.plist
404,409d403
< <dict>
< <key>LSHandlerContentType</key>
< <string>com.compuserve.gif</string>
< <key>LSHandlerRoleAll</key>
< <string>com.lemkesoft.graphicconverter</string>
< </dict>

Then I change all the GIF files to be opened by Preview again.

% plutil -convert xml1 ~/Library/Preferences/com.apple.LaunchServices.plist
% cp ~/Library/Preferences/com.apple.LaunchServices.plist ~/Library/Preferences/com.apple.LaunchServices_copy2.plist
% diff --text ~/Library/Preferences/com.apple.LaunchServices.plist ~/Library/Preferences/com.apple.LaunchServices_copy.plist
404,409d403
< <dict>
< <key>LSHandlerContentType</key>
< <string>com.compuserve.gif</string>
< <key>LSHandlerRoleAll</key>
< <string>com.apple.preview</string>
< </dict>
% diff --text ~/Library/Preferences/com.apple.LaunchServices.plist ~/Library/Preferences/com.apple.LaunchServices_copy2.plist
408c408
< <string>com.apple.preview</string>
---
<string>com.lemkesoft.graphicconverter</string>


Note that I had to change the file to text after every change because the Finder converted the file to binary format every time it edited it. I didn't have to logout or reboot; the property list file change less than a second after the preference was set. I'm guessing that because you were just looking at the old-format entry, you missed the real changes to your property list file. Did it change from text to binary format? Try the command line tools that I used above.

Note that this means that it matters where you put your entries in the property list file if you edit it.
--
Gary
~~~~
There are only 10 kinds of people in the world. Those
who understand binary numbers and those who don't.

May 3, 2007 12:21 PM in response to Gary Kerbaugh

gary,

i've been trying for two days now to understand what you wrote. and i'm sorry, but you're well over my head - i'm an advanced beginner at coding. but, thus far, i know you're making a copy of the plist file and then comparing that copy to the original. i got that. only two questions:

#1) making a copy of something and then immediately copmaring that copy to the original won't do anything. doesn't one need to change the original before running a comparison?

#2) the numbers in your code are foreign to me: diff --text ~/Library/Preferences/com.apple.LaunchServices.plist ~/Library/Preferences/com.apple.LaunchServices_copy.plist 404,409d403 <---- what do those numbers mean? you've got them after each entry in your example and i'm lost here.

lastly, as for your question that i needed to change from binary to text: i don't need to. everytime i make a change and then open the file with textedit, it's still in XML format after running the plutil conversion.

and the question remains: if i use the GUI to affect my changes to all FMPro 5.5 docs, that converted XML file doesn't change. not at the top, not at the bottom. and we are - so we're clear - talking about the file located at: ~/Library/Preferences/com.apple.LaunchServices.plist

May 3, 2007 2:50 PM in response to the razor

Hi David,

> #1) ... Doesn't one need to change the
original before running a comparison?


Of course. Maybe you missed the changes as they are not done in the command line. The changes are made in the two sentences beginning with:

[Then] I change all ...

There is a "cp" command prior to each of the above changes and the correspond "diff" comes after.

> #2) the numbers in your code are foreign to me:

This is a problem with the fact that the browser wraps the lines so it's hard to tell when a line is actually a new line or a wrap from the previous. The numbers are the first line of the output of the "diff" command. The ones you posted basically say that lines 404-409 are in the original file but not the copy. That indicates that those lines were inserted by the Finder when I changed the "open with" preference.

> lastly, as for your question that i
needed to change from binary to text: i don't need to.


That's the most worrisome information. Either your machine doesn't work like mine, you're not actually changing the "open with" preference globally or you're looking at the wrong file. Are you aware of the fact that the tilde, '~', in a UNIX path refers to your home directory? You posted the correct UNIX path to the file but I suppose you could still be unable to find the file in the Finder.

There's one way to find out what file you are modifying, if any. If the file is in your home directory and you run the command below immediately after making a global change to one of the "open with" preferences, the command below should find the file that was modified by LaunchServices. (and possibly other files) This command finds every file in your home directory that was modified less than 5 minutes before the command is run.

find ~ -newermt "5 minutes ago"

It might find numerous files but only one should be a com.apple.LaunchServices.plist property list file. If you don't find anything pertinent with the above command, try the one below.

sudo find / -newermt "5 minutes ago"

This will search your whole drive. If you still don't find a com.apple.LaunchServices.plist file then your machine is behaving completely differently from mine and my advice won't mean much. In fact, I wouldn't have a clue as to how to make the system behave that way.
--
Gary
~~~~
Use common sense in routing cable. Avoid wrapping coax around sources of strong electric or magnetic fields. Do not wrap the cable around flourescent light ballasts or cyclotrons, for example.
-- Ethernet Headstart Product, Information and Installation Guide,
Bell Technologies, pg. 11

May 3, 2007 4:11 PM in response to Gary Kerbaugh

gary,

no worries. we're talking about the same .plist in the same location. and here's the order of what i do:

#1) copy the plist
#2) use the GUI to make the changes using the "open with" button.
#3) test the changes via the GUI by double-clicking a file in FMPro 5.5 i wish to have open in v8.5
#4) covert the pilist to XML
#5) compare the XML'ed plist to the backup copy
#6) the result spits out this: "1,393c1,8".

i don't know what this means. and that's where i'm at.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to change "open with" defaults via command line?!?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.