Get default mail program

Hi all

long time ago Nigel Garvey helped me with this script to get the default mail program

I notice that now I upgrade to Mavericks the script is not working anymore when I switch between apple mail and Outlook. Looks like the plist is different in Mavericks. What can I do yo always get the correct result ?

set prefPath to (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 prefPath whose value of property list items contains "mailto")
on error
"com.apple.mail"
end try
end tell

You can call an ASObjC-based library containing this:

use framework "Foundation"
use framework "AppKit"

on pathToEmailApp()
	set theNSWorkspace to current application's NSWorkspace's sharedWorkspace()
	set theNSURL to theNSWorkspace's URLForApplicationToOpenURL:(current application's NSURL's URLWithString:"mailto:")
	return theNSURL's |path|() as text
end pathToEmailApp

Hi Shane

Thanks for your answer but it is not working for me to get some response from the script, or maybe it is me

I found this script from DJ Bazzie Wazzie that is working correct for me in Mavericks

do shell script “defaults read com.apple.LaunchServices | (grep -B1 ‘mailto’ || echo ‘"com.apple.mail"’) | awk -F ‘"’ ‘{print $2;exit}’”

You guys know this stuff, is this a good one or ???

Thanks for any suggestions, remember that I am VBA guy so I am not so smart in this area

Shane’s answer is for AppleScriptObjC which can be used in Script Libraries. His code is the best solution.

My code will always return a value. It will read the LShandler for mailto url scheme when it’s overruled. When it’s not overruled it will return the default which is com.apple.mail. However, I’m not sure if the LSHandlers are set the same way in Mavericks as the OS where I originally wrote the code for. The best way is to overrule the mailto url scheme handler and look if it returns something else than com.apple.mail.

I saved the code posted by Shane as : whichMailApp.scptd
I checked its box entitled : AppleScript/Bibliothèque Objective-C (maybe different when you use the system in English)
I stored it in “~/Library/Script Libraries”
I’m puzzled because if I store it in “/Library/Script Libraries” it failed.

I wrote the huge caller script :

use theLib : script "whichMailApp"

theLib's pathToEmailApp()

When I ran it I got the correct answer : “/Applications/Mail.app”

When I ran the shell script borrowed from DJ Bazzie Wazzie, it failed because there is no entry named “mailto” in my com.apple.LaunchServices.plist file (10.9.3)

Yvan KOENIG (VALLAURIS, France) jeudi 19 juin 2014 23:37:54

Like I said, the code doesn’t fail in worse case it will always return “com.apple.mail”. However I went to my mail application and changed the default mail application into another application and ran my code again on Mavericks. And guess what, the code is still working properly in Mavericks :D. For the record: mailto url scheme doesn’t need to be set in the file in order to work properly.

To Rob: Yes it’s safe to use the code. LShandlers are still set the same as in previous systems. However there is no guarantee that it will work on newer systems.

It’s not you. See macscripter.net/viewtopic.php?id=41638 for details of how to use a library.

I’m puzzled too, because that worked for me.

Even more puzzling : this morning it works.
I’m wondering if it’s the reboot which was required to allow AppleScript to « see » the global folder.

Yvan KOENIG (VALLAURIS, France) vendredi 20 juin 2014 10:35:40