How do I refer to an Application by a variable?

This won’t compile. The first Tell works allright, but the second bombs.

Any ideas please.

Regards

Santa


set MailClient to {name:"Mail"}

tell application (name of MailClient)
	activate
end tell

tell application (name of MailClient) 
		set ExistingUnread to unread count of inbox
		set ExistingOutbox to count of messages in outbox
		check for new mail
		delay 0.2
		repeat until background activity count = 0
			delay 0.1
		end repeat
		set NewUnread to (unread count of inbox) - ExistingUnread
		set NewMailFlag to NewUnread > 0
		set NewOutbox to count of messages in outbox
		set SentOutbox to ExistingOutbox - NewOutbox
end tell

Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

When script editor tries to compile the script, it needs to know what application the terms you’re using in your code are from. All applications respond to the “activate” command, so the first block compiles just fine. But the terms in the second block are only valid when the compiler knows where to look for definitions for the commands you’re using. There are two places you can let the compiler know about where to find the dictionary that contains the commands…

  1. In a “using terms from…” block…
using terms from application "Mail"
	tell application (name of MailClient)
		--> Do some cool stuff
	end tell
end using terms from
  1. In the application tell block itself…
tell application "Mail"
	--> Do more cool stuff
end tell

Actually there are there options, but the third would require using the “run script” command and executing all of the code in a script object. That hardly seems reasonable, though, and I fail to see the advantage of doing it this way. In fact, I don’t see any reason (considering the criteria you’ve given), why you would want to use a variable name in this case anyways. There’s no real likelihood that you’ll be able to use the same terms for any other app, so why not just hard-code it? This would spare you the complexity of doing it dynamically and eliminate the risk of throwing an error if your ‘MailClient’ variable is not declared properly for some reason. Don’t make something easy more difficult that it has to be.

j

Thanks Jobu, I’ll stick with hard coding as you suggested.

Regards

Santa

Ciao Santa,
it is possible to use the value of a variable to launch an application; the following is part of a script I use from inside iPhoto to open images from my library with various applications:

property appList : {"Adobe ImageReady 7.0", "Adobe Photoshop 7.0", "GraphicConverter", "Preview", "QuickTime Player", "Finder"}

choose from list appList
set nameApp to the result as string
set myApp to application nameApp

tell myApp
	activate
end tell

Good scripting
Farid

Hey santa

You can use something like this also…

set x to application "QuickTime Player" as string
tell application x to activate

or this

set x to application "QuickTime Player" as string
tell application x
	activate
end tell

cheers

pidge1 & chebfarid,

You guys are missing the point. If you’d bother to read St. Nicks’ post and maybe try his code… and THEN offer your solutions, you’d find that what you posted doesn’t help one bit. The problem is that if you call a tell block on a reference to the app name, and not DIRECTLY to the APP ITSELF, the compiler does not evaluate the name when it compiles. It does NOT create a reference to the application via the string you provided, or open the application’s directory and validate the commands you’re trying to execute. It simply doesn’t understand what the commands are and it throws an error. Yes… your solutions do work if all you want to do is tell the apps to activate. But the problem santa was having is specifically that he wants to do more than just activate the app, he wants to call commands that are specific only to the mail app.

For example, let’s pretend we want to get our groove on with itunes. All we need to do is play a song using the ‘play’ command…

tell application "iTunes"
	play
end tell

While there are probably many apps that respond to a play command, standard additions is not one of them. If you put a “play” command outside of a tell block that directly references an app which supports a play command, you’ll get an error. Try typing “play” anywhere in your script or inside of a Finder or System Events tell block… it’s-a-not-gonna-work. It’s evaluated as a variable, not as a command

The problem is, that a reference to an application is is only valid when hard-coded… except as I mentioned in my original post. If you try either of the suggestion you guys offered, they simply won’t work…

set iTunes to application "iTunes"
tell iTunes
	play
end tell

-- OR --

set iTunes to "iTunes"
tell application iTunes
	play
end tell

It’s all in the details, guys. :wink:
j

Hey Jobu

I’ll consider myself well and truly told off.
Where i do appreciate your advice! i don’t appreciate your tone and don’t think it will encourage fairly new
scriptors like myself and cheb, who in essence where only trying to help.

I’m sure cheb like myself DID bother to read the post, however due to our lack of experience maybe didn’t fully understand!

:slight_smile:

Thank you pidge1 for including me in your answer :slight_smile:
Well, jobu is right with his observations on AppleScript’s non capability to interprete the dictionary of applications defined by a variable - as you already pointed out the tone of his correction could have been a little bit more friendly, but never mind, the substance of his critic is correct…
Just a little detail - and this is also the reason why I didn’t catch the problem: AS, further to the simple activate instruction, does compile correctly all the commands of the Standard Suite, like “open”, “close” and so on, but as explained by jobu it won’t compile application specific commands.

Ciao
Farid