I want to use the command line to find out which application opens http and https links. I need to do this in a script. I can see the answer by executing 'defaults read com.apple.LaunchServices' but I don't want to parse that dictionary myself.
2 Answers
Apple Script has built-in capabilities to parse property list files. StefanK on macscripter.net provides a snippet which already helped me some time ago. You can save and execute it easily as a script, I stored it in my user bin directory (which I added to my $PATH
):
#!/usr/bin/osascript
tell (system attribute "sysv") to set MacOS_version to it mod 4096 div 16
if MacOS_version is 5 then
set {a1, a2} to {1, 2}
else
set {a1, a2} to {2, 1}
end if
set pListpath to (path to preferences as Unicode text) & "com.apple.LaunchServices.plist"
tell application "System Events"
repeat with i in property list items of property list item 1 of contents of property list file pListpath
if value of property list item a2 of i is "http" then
return value of property list item a1 of i
end if
end repeat
return "com.apple.Safari"
end tell
Other options:
VERSIONER_PERL_PREFER_32_BIT=1 perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]'
tell application "System Events"
try
value of property list item "LSHandlerRoleAll" of (property list item 1 of property list item "LSHandlers" of property list file ((path to preferences as text) & "com.apple.LaunchServices.plist") where value of property list items contains "http")
on error
"com.apple.safari"
end try
end tell
http://www.hamsoftengineering.com/codeSharing/defaultApplication/defaultApplication.html:
$ DefaultApplication -url http:
/Applications/Safari.app