Get default mail program

Hi all

Long time ago with help from this group I get this script to get the default mail program

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

But now with the new OS it always say Apple Mail even if Outlook is the default.
I am no expert in script so I hope you can help me to fix it so it also works in the new OS

Thanks for reading and have a nice day

Hi,

try this, its uses the property list suite of System Events rather than the shell.
Maybe the key “mailto” is different, I can’t test it because using default Mail.app the entry is missing


set mailTo to "com.apple.mail"
tell application "System Events"
	set plistFile to (POSIX path of file "com.apple.LaunchServices.plist" of preferences folder)
	tell contents of property list file plistFile
		repeat with anItem in (get property list items of property list item "LSHandlers")
			if exists property list item "LSHandlerContentType" of anItem then
				if value of property list item "LSHandlerContentType" of anItem is "mailto" then
					set mailTo to value of property list item "LSHandlerRoleAll" of anItem
				end if
			end if
		end repeat
	end tell
end tell


Thanks Stefan, got this error

error “System Events got an error: Can’t get file "com.apple.LaunchServices.plist" of preferences folder.” number -1728 from file “com.apple.LaunchServices.plist” of preferences folder

must go now so I look at it again this evening

Hi.

In Yosemite, the relevant file appears to be ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist:

do shell script "defaults read com.apple.LaunchServices/com.apple.launchservices.secure | (grep -B1 'mailto' || echo '\"com.apple.mail\"') | awk -F '\"' '{print $2;exit}'"
--> "com.ctmdev.powermail"

PS. Some of the plist labels have changed too, so Stefan’s script would have to be:


set mailTo to "com.apple.mail"
tell application "System Events"
	set plistFile to (POSIX path of file "com.apple.launchservices.secure.plist" of folder "com.apple.LaunchServices" of preferences folder)
	tell contents of property list file plistFile
		repeat with anItem in (get property list items of property list item "LSHandlers")
			if exists property list item "LSHandlerURLScheme" of anItem then
				if value of property list item "LSHandlerURLScheme" of anItem is "mailto" then
					set mailTo to value of property list item "LSHandlerRoleAll" of anItem
				end if
			end if
		end repeat
	end tell
end tell

return mailTo

Hi Nigel

You are correct, in Yosemite when I add this it is working

/com.apple.launchservices.secure

Do you know if Mavericks is using the old path or also this new one

Thanks

On my machine, the new file was created one second before the old one was last modified, on the evening I installed Yosemite. This suggests Mavericks used the old path, but it’s hardly conclusive proof!

I was guessing the label name, because as I never changed the default mail application I don’t have “mailto”.
I’m still on Mavericks, the script using System Events works fine with with the randomly picked “public.html” value.

You could also use this under Mavericks and Yosemite:

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

It would have to be in a script library under Mavericks. OTOH, it’s massively faster.