Determine default email program

How would one determine which program the user has set as their default email program?

Potential answers would be “Mail” or “Outlook” or “other”

I’ve fiddled around looking where this might be stored but no luck so far.

Thanks

Hi,

maybe there’s a shorter and faster shell version, this is a pure AppleScript version.
It parses the preference file of LaunchServices


set bundleIdentifier to missing value
set LSPrefFilePath to (path to preferences folder as text) & "com.apple.LaunchServices.plist"
tell application "System Events"
	set LSPrefFile to property list file LSPrefFilePath
	tell property list item 1 of contents of LSPrefFile
		repeat with anItem in (get property list items)
			if exists property list item "LSHandlerURLScheme" of anItem then
				if value of property list item "LSHandlerURLScheme" of anItem is "mailto" then
					set bundleIdentifier to value of property list item "LSHandlerRoleAll" of anItem
					exit repeat
				end if
			end if
		end repeat
	end tell
end tell
if bundleIdentifier is missing value then
	set defaultMailClient to "Mail.app"
else
	tell application "Finder" to set defaultMailClient to name of application file id bundleIdentifier
end if


It may depend on your system version but here it is stored in:

$HOME/Library/Preferences/com.apple.LaunchServices.plist

Open the file in a text editor and search for “mailto”. The name of
the mailer is on the line above.

Actually parsing the file to get the mailer name is not really a job for
AppleScript but it could be done.

JD

Try this. it works here:


do shell script "defaults read com.apple.LaunchServices | perl -e '
while(<>) {push @lines, $_; m~mailto~  and last}
$_ = $lines[-2]; s~[^\"]+\"~~;s~\";~~;print'"
--
--=> "org.mozilla.thunderbird"

Hi Guys

I am using script from Nigel Garvey now that he posted here on this forum
Works OK for me but I believe not for the OP, can it be that this not is working in Leopard ?
I have a OK test in Snow ?

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’re kidding right? The plist parser in System Events is especially written for AS to parse plist files no matter how they are encoded. How would you parse binary plist files with an clumsy perl script, at least system events can because it’s designed for it. AS is the perfect solution and isn’t the purpose of AS to avoid manually opening the file by hand and change it.

Your script doesn’t work because when mail.app is the default mail application nothing in launch service URL handlers is stored but the defaults are used from system’s launch services. Therefore I prefer Stefan’s script in this case

Much better, I was looking for the proper whose filter syntax without avail

Hi DJ Bazzie Wazzie

thanks for your reply

Working here OK when I switch between mail programs, always the correct answer.
I use Moutain Lion and another tester use Leopard Snow

But I believe you so I will rebuild my code to use Stefan’s script

I hope Mike can tell if that is working for him

Thanks

Hi rdb1968,

Your code is perfect! I was referring to john’s post. I was already editing my post when you posted yours and haven’t seen your post after i submitted min.

Hi Stefan

Now I am confused what I must use

Important that it is working for me in Leopard and higher because
office 2011 can’t be installed in older versions.

I am no Mac scripter so I is it possible that Nigal’s script not work and yours is working in some cases.

Sorry I am a stupid VBA guy

Sorry guys, glad that Nigel’s code is OK

I hope Mike(OP) can tell us if it is working for him.

You’re definitely not Ron! You’re my VBA Hero, used a lot of code lately from your blog to embed code inside spreadsheets which isn’t possible with AS.

WOW! So many replies. Thanks.

I’ll test these when I get back to my Mac.

One question, since this script will be distributed to many users, which is more robust in terms of stability across different versions of the OS?

You can use Stefan’s or rdb1969’s because they’re both good

Thanks

I published a mail add-in(version 2.0) for Excel with code from Nigel in it to check which mail macro to run
Outlook or Apple Mail

If you have time take a look
http://www.rondebruin.nl/mail/helprdbmailmac.htm

As far as I remember it does not work in Tiger, but as mentioned it works in Leopard and higher

Hi, Stefan.

On my Tiger System, your script errors on this line:

repeat with anItem in (get property list items)

But with ‘its’ inserted after ‘get’, it returns the file name of my e-mail client (“PowerMail.app”).

Ron’s quotation of (apparently) my script works as posted and returns the bundle identifier

John’s doesn’t work on this system.

More accurately, there’s nothing stored if the default mail application has never been changed since the system was installed (or com.apple.LaunchServices.plist tampered with). In this case, the default application is Mail.app. But if you change the default and then revert to Mail.app, there’ll be an entry for Mail.app in the plist. (I think that’s still true. :rolleyes:)

Exactly.

I’m quite sure that Apple has changed something significant in the structure of the LaunchServices pref file.
I thought it was in Leopard but it might be earlier

Thanks Nigel

I add a VBA example of your script on my site.
http://www.rondebruin.nl/mac.htm#Default

Have a great day

Well here’s another solution you won’t like then:


set _default_MUA to do shell script ¬
	"defaults read com.apple.launchservices LSHandlers | grep -B1 'mailto' | grep 'LSHandlerRoleAll' | perl -e '$_=<>; s~.+?([^\\.]+)\";$~$1~; print'"
if _default_MUA = "" then set _default_MUA to "mail"
return _default_MUA

For me, AppleScript is purely for inter-application communication. For other
purposes I find it very defective and impossibly verbose. I know this is not
the best place to express such heresy.

JD