Sharing a script - Set Outlook as default email/contacts/calendar app

gknacks
New Contributor III

This script uses plistbuddy to create com.apple.LaunchServices.plist in the /System/Library/User Template/English.lproj user template. Run this as after in imaging post-flight.

If anyone has any feedback to improve the script I'de love to hear it.

Enjoy!!

#!/bin/bash

# Create the LSHandlers array
/usr/libexec/plistbuddy -c "add:LSHandlers array" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist

# Set default for mailto links
/usr/libexec/plistbuddy -c "add:LSHandlers:0:LSHandlerURLScheme string mailto" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:0:LSHandlerRoleAll string com.microsoft.outlook" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist

# Set Outlook as default for email
/usr/libexec/plistbuddy -c "add:LSHandlers:1:LSHandlerContentType string com.apple.mail.email" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:1:LSHandlerRoleAll string com.microsoft.outlook" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:2:LSHandlerContentType string com.microsoft.outlook14.email-message" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:2:LSHandlerRoleAll string com.microsoft.outlook" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist

# Set Outlook as default for .vcard
/usr/libexec/plistbuddy -c "add:LSHandlers:3:LSHandlerContentType string public.vcard" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:3:LSHandlerRoleAll string com.microsoft.outlook" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist

# Set Outlook as default for .ics files
/usr/libexec/plistbuddy -c "add:LSHandlers:4:LSHandlerContentType string com.apple.ical.ics" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:4:LSHandlerRoleAll string com.microsoft.outlook" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist

# Set Outlook as default calendar app
/usr/libexec/plistbuddy -c "add:LSHandlers:5:LSHandlerContentType string com.microsoft.outlook14.icalendar" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
/usr/libexec/plistbuddy -c "add:LSHandlers:5:LSHandlerRoleAll string com.microsoft.outlook" /System/Library/User Template/English.lproj/Library/Preferences/com.apple.LaunchServices.plist
9 REPLIES 9

makander
Contributor

Thanks for the script @gknacks][/url!

Just one question, have you tried it with Mavericks?

mostlikelee
Contributor

for Outlook 2015, the LSHandlerContentType seems to be com.microsoft.outlook15.xxxxx

jwojda
Valued Contributor II

I was recently tasked with doing this, is this still valid for use or is there a different better way? What about changing for existing users?

Mauricio
Contributor III

@jwojda You can use Composer to create a dmg of the file (~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist) and then use Casper to install it in the Users Template and existing users. ( 2 tick boxes feu/fut, if I recall correctly. No using Casper currently.)
Another option, the one I'm using here, is to include the setup in what we call 'Office First Run' which is a LaunchAgent that runs a script at log in to setup several Office 2016 and 2011 preferences.
Same thing can be done with Casper.

The script I have for log in is this:

#!/bin/bash
#set -x
plistbuddy="/usr/libexec/plistbuddy"

launchServicesSecure="$HOME/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"


# Create the LSHandlers array
$plistbuddy -c "add:LSHandlers array" "${launchServicesSecure}"  > /dev/null 2>&1

# Set default for mailto links
$plistbuddy -c "add:LSHandlers:0:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:0:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:0:LSHandlerURLScheme string mailto" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:0:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default for email
$plistbuddy -c "add:LSHandlers:1:LSHandlerContentType string com.apple.mail.email" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:1:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:1:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:1:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default email from Outlook 2011
$plistbuddy -c "add:LSHandlers:2:LSHandlerContentType string com.microsoft.outlook14.email-message" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:2:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:2:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:2:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default for .ics files
$plistbuddy -c "add:LSHandlers:3:LSHandlerContentType string com.apple.ical.ics" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:3:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:3:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:3:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default calendar from Outlook 2011
$plistbuddy -c "add:LSHandlers:4:LSHandlerContentType string com.microsoft.outlook14.icalendar" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:4:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:4:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:4:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default for .vcard
$plistbuddy -c "add:LSHandlers:5:LSHandlerContentType string public.vcard" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:5:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:5:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:5:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

exit 0

Hope helps.
Mauricio

apizz
Valued Contributor

This was helpful.

I made a couple of modifications for situations where you are deploying this after imaging, such that it properly appends to the com.apple.launchservices.secure.plist file if it already exists. If the file doesn't exist, then it creates it with the numbers 0 thru 5, as @Mauricio has in his script

#!/bin/bash

plistbuddy="/usr/libexec/PlistBuddy"
USER=$(/bin/ls -l /dev/console | /usr/bin/awk '{print $3}')
launchServicesSecure="/Users/$USER/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
PLIST_ENTRIES=$(/usr/bin/defaults read $launchServicesSecure | /usr/bin/grep LSHandlerPreferred | /usr/bin/wc -l | /usr/bin/sed 's/ //g')

if [ "$PLIST_ENTRIES" = "0" ]; then
    ONE="0"
    TWO="1"
    THREE="2"
    FOUR="3"
    FIVE="4"
    SIX="5"
else
    ONE=$(/bin/echo $(($PLIST_ENTRIES+1)))
    TWO=$(/bin/echo $(($PLIST_ENTRIES+2)))
    THREE=$(/bin/echo $(($PLIST_ENTRIES+3)))
    FOUR=$(/bin/echo $(($PLIST_ENTRIES+4)))
    FIVE=$(/bin/echo $(($PLIST_ENTRIES+5)))
    SIX=$(/bin/echo $(($PLIST_ENTRIES+6)))
fi

# Create the LSHandlers array
$plistbuddy -c "add:LSHandlers array" "${launchServicesSecure}"

# Set default for mailto links
$plistbuddy -c "add:LSHandlers:${ONE}:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${ONE}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${ONE}:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${ONE}:LSHandlerURLScheme string mailto" "${launchServicesSecure}"

# Set Outlook 2016 as default for email
$plistbuddy -c "add:LSHandlers:${TWO}:LSHandlerContentType string com.apple.mail.email" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${TWO}:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${TWO}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${TWO}:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default email from Outlook 2011
$plistbuddy -c "add:LSHandlers:${THREE}:LSHandlerContentType string com.microsoft.outlook14.email-message" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${THREE}:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${THREE}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$USER $plistbuddy -c "add:LSHandlers:${THREE}:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default for .ics files
$plistbuddy -c "add:LSHandlers:${FOUR}:LSHandlerContentType string com.apple.ical.ics" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${FOUR}:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${FOUR}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${FOUR}:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default calendar from Outlook 2011
$plistbuddy -c "add:LSHandlers:${FIVE}:LSHandlerContentType string com.microsoft.outlook14.icalendar" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${FIVE}:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${FIVE}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${FIVE}:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

# Set Outlook 2016 as default for .vcard
$plistbuddy -c "add:LSHandlers:${SIX}:LSHandlerContentType string public.vcard" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${SIX}:LSHandlerPreferredVersions dict" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${SIX}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}"
$plistbuddy -c "add:LSHandlers:${SIX}:LSHandlerRoleAll string com.microsoft.outlook" "${launchServicesSecure}"

exit

dstranathan
Valued Contributor II

Related info: Microsoft just relased a tool "SetDefaultMailApp": https://blogs.technet.microsoft.com/office_for_mac_support_team_blog/?p=285

SetDefaultMailApp (Download Here): This utility allows you to easily make Microsoft Outlook the default mail application. To use:

Unzip SetDefaultMailApp.zip.
Control-click on SetDefaultMailApp and click Open. If you are asked, “Are you sure you want to open it?” click the Open button.
Choose “com.microsoft.outlook” in the Default Mail Application dialog and click the “Make Default” button. Once set, you can quit Default Mail Application.

rqomsiya
Contributor III

@aporlebeke Your script doesn't seem to be working if that .plist has already been created. I'm getting the following error when running your script:

ml0170318:~ root# /Users/ryq/Desktop/set_outlook2016_default.sh 
Add: ":LSHandlers" Entry Already Exists
/Users/ryq/Desktop/set_outlook2016_default.sh: line 43: ryq: command not found
ml0170318:~ root#

Thoughts?

Thanks,
R.

apizz
Valued Contributor

@rqomsiya I looked at line 43 in the script ($plistbuddy -c "add:LSHandlers:${TWO}:LSHandlerPreferredVersions:LSHandlerRoleAll string -" "${launchServicesSecure}") , but that's just one of the several commands setting Outlook 2016 as the default for email. Looking at the error, it seems like there's a space somewhere, because if you're logged in as user "ryq", that's what would be substituted for launch services plist path.

Did you make any alterations in the script? Being able to see your version would be helpful.

There's also a lot of assumptions that the script makes about what's in the com.apple.launchservices.secure.plist file. It's meant to be run immediately after a user logs in. If you're running it well after the fact, there's probably a lot of other data in there and why when PlistBuddy tries to insert the lines into the PLIST it's failing.

rqomsiya
Contributor III

@aporlebeke Yep. That would explain it. I'm running this on an existing box that has already ran Outlook 2016 for sometime.

Thanks for the quick reply!

-R