Change file association.

dlondon
Valued Contributor

Currently we have either Preview or Acrobat opening pdf documents by default.

I'm trying to see what I can do to automatically associate pdf documents with a new app called Nitro on the command line.   Can anyone point me in the direction?

4 REPLIES 4

shannon_pasto
Contributor

You'll want to look at LSHandlers. These are what associate a file type/extension to an app. The file it updates is "~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist". I just installed Acrobat and a new handler was added after I launched it and set the app as the default for PDFs. There might be others you'll need to add for Nitro so try it on a clean/new build and see how you go. You can add to the existing plist or if you're deploying on a new build you can pretty much just delete the file and write out a new one. I do have some shell script that I use to add a bunch of handlers for Outlook and Chrome.

/usr/libexec/PlistBuddy -c "Print" ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist

Amongst all the ones I already have set this one was added...

Dict {
            LSHandlerContentType = com.adobe.pdf
            LSHandlerRoleAll = com.adobe.reader
            LSHandlerPreferredVersions = Dict {
                LSHandlerRoleAll = -
            }
        }

I generally find that a log out/in (or reboot) is needed to reload the handlers but there is also a command you can run but I find that that doesn't always work. From memory the command you need to "lsregisters".

Thanks @shannon_pasto - would you mind sharing one of those scripts for adding the handler?

shannon_pasto
Contributor

sure. This is what I use to set Chrome as the default browser. Hopefully you can work out your URLHandlers and the app bundle identifier...

 

#!/bin/sh

CurrentlyLoggedInUser=$(/usr/bin/stat -f %Su /dev/console)

setLSHandler() {

  # function to setup any default apps, LSHandlerURLScheme or LSHandlerContentType
  # usage: setLSHandler LSHandlerURLScheme mailto com.microsoft.outlook
  _lsHandlerType="${1}"
  _lsHandler="${2}"
  _appID="${3}"

  sudo -u "${CurrentlyLoggedInUser}" /usr/libexec/PlistBuddy -c "Add :LSHandlers:0 dict" "${LSfile}"
  sudo -u "${CurrentlyLoggedInUser}" /usr/libexec/PlistBuddy -c "Add :LSHandlers:0:${_lsHandlerType} string ${_lsHandler}" "${LSfile}"
  sudo -u "${CurrentlyLoggedInUser}" /usr/libexec/PlistBuddy -c "Add :LSHandlers:0:LSHandlerRoleAll string ${_appID}" "${LSfile}"
  sudo -u "${CurrentlyLoggedInUser}" /usr/libexec/PlistBuddy -c "Add :LSHandlers:0:LSHandlerPreferredVersions dict" "${LSfile}"
  sudo -u "${CurrentlyLoggedInUser}" /usr/libexec/PlistBuddy -c "Add :LSHandlers:0:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${LSfile}"

  unset _lsHandlerType
  unset _lsHandler
  unset _appID
}

# set up for LaunchServices configuration
LSfile="/Users/${CurrentlyLoggedInUser}/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
LSdir=$(dirname "${LSfile}")
if [ ! -d "${LSdir}" ]; then
  /bin/echo "Creating the LaunchServces path"
  sudo -u "${CurrentlyLoggedInUser}" /bin/mkdir "${LSdir}"
fi

# create then LS Handler file
/usr/libexec/PlistBuddy -c "Add :LSHandlers array" "${LSfile}" >/dev/null 2>&1

/bin/echo "Setting Google Chrome as the default browser"
URLhandlers=(public.url public.html public.xhtml http https)
URLhandlerCount=$(( ${#URLhandlers[@]} - 1 ))

i=0
while [ $i -le "${URLhandlerCount}" ]; do
  setLSHandler "LSHandlerURLScheme" "${URLhandlers[$i]}" "com.google.Chrome"
  i=$(( i + 1 ))
done

sudo -u "${CurrentlyLoggedInUser}" /usr/bin/plutil -convert binary1 "${LSfile}"

exit 0

 

Hope that makes sense.

Cheers,

Shannon

dlondon
Valued Contributor

Thanks - there's some good stuff in that.  Much appreciated