tell application <identifier> problem

Hi all, I’m an occasional Apple Scripter “ which means I always seem to be starting from scratch.
I may be doing something here that is fundamentally wrong but seems to half work. I need/(would like) to automate an application by simply using an identifier as the app name - is this possible?


set appname to "Mail"

tell application appname
	set msg to make new outgoing message
	tell msg
		set visible to true
	end tell
	activate
end tell

I recieve an error “Expected end of line but found identifier.” at the word “message”.

The reason I need to do this (before you ask) is I have a script that returns the name of the default mail application.
Therefore - if I keep the script simple - one should be able to automate any mail app.

For all it’s worth, here is the script to get the default mail app name.


display dialog defaultEmailApp()

on defaultEmailApp()
	set p to POSIX path of (path to preferences) & "com.apple.LaunchServices.plist"
	tell application "System Events" to tell property list item "LSHandlerRoleAll" of (property list item 1 of property list item "LSHandlers" of property list file p whose value contains "mailto") to if exists then
		set v to value
		tell application "Finder" to set {name:n, name extension:e} to application file id v
		tell (count e) + 1 to return n's text 1 thru -(1 mod it + it)
	end if
	"Mail"
end defaultEmailApp

I’m currently running 10.4.7.
Thanks in advance!
Bill

Hi.

I like the idea behind this, but I haven’t found a solution yet - if there is one. It seems odd, because the two following scripts work.


set appname to "Mail"
tell application appname
	activate
end tell


set appname to "Safari"
tell application appname
	make new document
end tell

I don’t know if a shell script uses the default maill app (I haven’t read the mail manpage yet.) It might be worth considering.

j

Hi,

If you want to oen a new message in the default mail app, this works also.

open location “mailto:”

You can also add subject, from, to, etc.

Edited: and BTW, in the tell block, the compiler doesn’t know what dictionary to look for the keywords. You usually only use dynamic app names with just the basic commands (activate, launch, open, save, quit). I think these were the basics.

gl,

Thanks for the response.

I added one line to your Safari script to break it:


set appname to "Safari"
tell application appname
	set page to make new document
	set URL of page to "http://www.apple.com"
end tell

It dies on the ‘set URL’ line but works fine if appname is replaced with “Safari” - just like my Mail script.

I think the problem may be that the Application (or Applescript) doesn’t know what tyoe of data ‘appname’ is ?..
Although I’ve tried:
set appname tp “Mail” as String
set appname tp “Mail” as Text
set appname tp “Mail” as Unicode Text

to no avail.

Maybe I will look into shell scripting - or javascripting?
I thought this was something stupid on my part…

I think that the problem is that AppleScript doesn’t know how to compile the tell lines in your mail application without knowing which mail application when you compile it since the application specific commands need the applications dictionary to compile. You might try something like this:


set appname to defaultEmailApp()
if appname is "Microsoft Entourage" then EnterageMail()
if appname is "Mail" then AppleMail()

on EnterageMail()
	tell application "Microsoft Entourage"
		activate
		--do stuff here
	end tell
end EnterageMail

on AppleMail()
	tell application "Mail"
		activate
		--do stuff here
	end tell
end AppleMail

on defaultEmailApp()
	set p to POSIX path of (path to preferences) & "com.apple.LaunchServices.plist"
	tell application "System Events" to tell property list item "LSHandlerRoleAll" of (property list item 1 of property list item "LSHandlers" of property list file p whose value contains "mailto") to if exists then
		set v to value
		tell application "Finder" to set {name:n, name extension:e} to application file id v
		tell (count e) + 1 to return n's text 1 thru -(1 mod it + it)
	end if
	"Mail"
end defaultEmailApp

There is more code to write, but the code will correctly address the application specific commands so that the script and the application communicate correctly.

That is correct kel (and Jerome).

Thanks Jerome, you’re right.
I tried 'using terms from application “Mail” ’ but of course it doesn’t work for Entourage.
In fact the Standard Suit is slightly different for both apps anyway.

So it seems I was headed in the "if appname is " direction.

I also like the eloquent “open location” from Kel - which pretty well solves all my problems.
I can also add body text with it as well as use an identifier - but I guess I’ll have to majorly url encode it.


set mailto to "mailto:?subject=this is a subject&body=this is the body"
open location mailto

Thanks all.