get default mail client (does work this code for you?)

Hi all!

Can anybody confirm if this works in every system? (I’m specially interested about Panther).

This code teorically will return the creator code of the default mail client in your system:

getDefaultMailClient()

to getDefaultMailClient()
	set dfb to read ((path to preferences as text) & "com.apple.LaunchServices.plist" as alias)
	set x to offset of "<key>U:mailto</key>" in dfb
	set dfb to text x thru (x + 800) of dfb
	set x to offset of "<key>LSBundleSignature</key>" & (ASCII character 10) & tab & tab & tab & tab & "<string>" in dfb
	return text (x + 41) thru (x + 44) of dfb
end getDefaultMailClient

It returns “emal” in Mac OS X 10.2.8 Jaguar, I’m using Apple’s Mail.app.

OK, thanks!

It also returns “OPIM” (Entourage) for me in 10.2.8…

Panther anyone?

Returns “BLTO” (Mailsmith) for me in Panther (7B85)

You can couple your code with this routine for returning an application path from the creator code:

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

Hello.

I was after a guaranteed way to open up a new mail message on a clients computer when finding this post.

The attempts above doesn’t work any more, and I didn’t have the patience to peruse through the preferences files
to find one. What I came up with was this:


do shell script "open \"mailto: whomitconcerns@somewhere.com ?subject=Some subject \" "

If somebody out there knows what property file, domain and key to get the default mail client I’d be very happy.

does this help…
How To: Change Default Mail Client on OSX — http://www.1nova.com/blog/?p=472

Tom

I think this may be one of those cases where the Apple product is assumed to be the default and isn’t listed in “com.apple.LaunchServices.plist”. If you change the default, the ID of the rival product is listed in the file. If you then revert to the Apple product, its ID does then appear in the file.

I’m not sure if this works with a non-specified default as I use PowerMail, but you should be able to work it out from here:


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

Hello.

Thank you very much both of you.

Everything is in order if I get an error! :slight_smile:

@Nigel. I will edit and make all the copyright statements for the things I post which is intended to be units of functionality with a referral to the post below your comments about long copyright notices.

Hello.

I came by this post about mail and default mail clients which may be helpful to some.

I recently updated a command line tool of mine and it now can get the default application for url’s in addition to other things… meaning you can pass the url “mailto:” to get a user’s default email application. You can find it here.

Hello.

:slight_smile:
It’s the “other things” parts that worries me; if I have writing this snippet in vain. I don’t need the path, I need the LSRoleAll attribute. I posted it anyway, it works fairly well.
Edit:
Luckily they don’t compete, so I didn’t make it in vain.
Edit: I moved this and my whole concept of the bundle identifier usage over at CodeExchange

Nigel’s script in post#8 does actually the same. It is not possible to write an universal script for that purpose because the default values are not saved in com.apple.LaunchServices.plist. For example, if there is no entry for mailto in the plist file Mail.app is supposed silently to be the default mail client

FYI: I got an error when trying to run your script. The script erred before “tids” was defined so you should fix that.

error "The variable tids is not defined." number -2753 from "tids"

Also, from looking at LSRolesAll it seems you want the bundle identifier for the found default application. So using my tool you can get that like this…

set daPath to (path to home folder as text) & "UnixBins:DefaultApplication"
set theURL to "mailto:"

set appPath to do shell script quoted form of POSIX path of daPath & " -url " & quoted form of theURL
tell application "Finder" to set appBundleID to id of application file ((POSIX file appPath) as text)
return appBundleID

Note that my tool won’t error if the default application hasn’t been changed. It always returns the application that will launch when you open a file. Your script will error and thus you have to guess at the answer. Of course I understand you’d rather not use an external tool but I don’t see any applescript way to do it without errors and guessing. The tool is flexible enough to handle file paths, file extensions, or urls… that’s the other stuff it does.

Hello.

I have fixed it.

This is a problem area, I know. I really only need to get the bundle identifier for a known role. To be specific: I will use this for getting the default editor for applescript. It is only for such purposes. Then I just have a list and checks which bundle id is used, and which that is one I can support.
To use mine, somebody would have to know what the assumed application is, because it will return empty.

So it was only for such purposes Hank. the defaults is an external tool as well as any. But the extra work for users to get all the tools is another issue. I will try to restrain my self to not have to use too many external tools.

I also try to restrict my self from using System Events whenever I can, it is really slow on Snow Leopard, I didn’t ever think about that under Tiger, so I guess it’s performance has fallen relatively to the rest of the system.

Nobody want’s to use a script if it takes a whole day to set it up. :slight_smile:

I will write an installer though, maybe one that fetches tools from the net, - but then we are having it going with changing url’s and such.

This is for the ScriptLibrary Loader btw. I’m currently working on a feature that lists the contents of a Library, and lets you jump directly to that in the file. It shall also work on the current file.

Thanks to both for your insights. And we might share a bajer one day Hank!

Maybe.:smiley: I see you noticed I’m in Denmark. I just updated that today so I’m surprised. I moved here recently but I’m American. Not sure how long I’ll be here. I spent some time in Helsingborg, Sweden and now Denmark… never Norway so that would be new.

Hello.
I’m sorry to say that it is a “no can do” right now, I have to finish off some work before heading on holiday with my family.

I would have been fun though.

Not that you are going to notice that at all. :slight_smile:

I posted my whole concept of the bundle identifier usage over at CodeExchange