I know where you’re comming from but such a simple thing as executing shell scripts, apple events or changing a user preference I think AppleScript fits perfectly. You or I could write an complete Cocoa application in Objective-C for this but isn’t that a bit overkill? For developers with not too much spare time, me included, AppleScript can be an outcome for such simple tasks. If you want to create commercial software, AS would be the last choice. But I think nobody here have those intentions. If that’s the case I write mostly Objective-C++.
(path to preferences as text) & "com.apple.LaunchServices.plist"
tell application "System Events"
try
value of property list item "LSHandlerRoleAll" of (first property list item of property list item "LSHandlers" of property list file result whose value of property list items contains "public.xml")
on error
"???????"
end try
end tell
returns the correct result : “com.barebones.textwrangler”
Am’I making something wrong when I repale the string ‘mailto’ by the string ‘public.xml’ ?
Yvan KOENIG (VALLAURIS, France) mardi 9 octobre 2012 12:12:07
As far as I’ve been able to analyse DJ’s code, it assumes that the relevant property list items are ordered, whereas in fact they’re labelled, as in records. The code gives the wrong result when the actual order is different from the assumed one.
Here’s a first attempt at parsing each group of labels using “sed”. Basically, it puts each group onto a single line and, if the line contains the search term, the contents of the quotes following “LSHandlerRoleAll” are printed to the output. At the end, the default result is appended to whatever’s been produced and a separate invocation of “sed” returns the first line of the one- or two-line result. I’m hopeful it’ll work correctly with your “public.xml” input:
set searchTerm to "public.xml"
set defaultHandler to "???????"
do shell script "defaults read com.apple.LaunchServices | sed -En '/LSHandlers =/,$ { /{/ h ; /{/ !H ; /}/ { g ; /[[:space:]]" & searchTerm & "[[:punct:]]/ { s/^.*LSHandlerRoleAll = \\\"([^\\\"]+).+$/\\1/p ; q ; } ; } ; } ; $ a\\'$'\\n''" & defaultHandler & "'"
Minor code edit: It’s not actually necessary to reduce each group to a single line. It’s enough for all its lines to be in the pattern buffer together. Two more: The identification of the group containing the search term is now more robust and the first “sed” script now quits immediately if it prints a hit, which does away with the need for the second “sed” call.