Passing variables into 'tell application' commands

WHY WILL THIS NOT WORK?? (It is giving a syntax error…)

set appname to “Internet Explorer”
set webaddr to “http://www.google.com
tell application appname
launch
GetURL webaddr
quit
end tell

IF I DO THIS IT WORKS FINE
tell application “Internet Explorer”
launch
GetURL webaddr
quit
end tell

However I need to pass in variables because I am using a list of browsers, any ideas what may be the problem here?

I don’t have Internet Explorer installed so I can’t test your script, but you could bypass the problem by using “open location” from the Standard Additions dictionary. It should work with any browser.


set appname to "Firefox"
set webaddr to "http://www.google.com"
tell application appname to open location webaddr

Because the AppleScript compiler pulls all sorts of sneaky tricks to support those fancy “English-like” keywords we all know and love. Chief amongst these tricks is looking for ‘tell application “…”’ lines so it knows what application-specific keywords to look for when compiling statements within the tell block.

Mostly this works well enough for simple code, but once you push the language a bit harder such clever-cleverness can start to bite you instead. Because your script doesn’t provide the application name till runtime, the compiler doesn’t know to look up additional terminology so makes do with only those keywords defined in AppleScript and any loaded osaxen. And ‘GetURL’ is not amongst these default keywords, as you’ve discovered.

There are a few options open to you:

  1. Use the raw Apple event code in place of the ‘GetURL’ keyword; in this case «event GURLGURL».

  2. Enclose the relevant code in a ‘using terms from application “Internet Explorer”’ block. This explicitly informs the compiler where to obtain additional terminology from when compiling the enclosed code.

  3. Standard Additions also happens to define a keyword for this particular event, ‘open location’, which you can use in place of ‘GetURL’. (Standard Additions also defines its own «event GURLGURL» handler that opens the URL in the system’s default browser, but as long as your target applications define their own «event GURLGURL» handlers they’ll handle it themselves.)

HTH

has

Are you noting the errors returned? Are you also Looking in the “events” window to see whas happening? Those two things give your answer:

A prompt showed me the error and took me to edit, where “GetURL webaddr” was highlighted. As Esme pointed out above, the correct syntex for IE is “open location”. Got that to work, but IE would open and then close. So I watched the events window-the “Launch” command was causing a problem. Scrap that. What I was left with launched Google from a variable with IE as a variable, “SurfApp”


set SurfApp to "Internet Explorer"
set UrlVar to "http://www.google.com" as string
tell application SurfApp
	open location UrlVar
end tell

If you want to break it down a little further, you could


set SurfApp to "Internet Explorer"
set SiteAddy to "google"
set UrlVar to ("http://www." & SiteAddy & ".com") as string
tell application SurfApp
	open location UrlVar
end tell

But I think the first one suits your needs. Post back give report :slight_smile:
SC

By the way if you use IE and pull scripts off the bulletin board, try this script I wrote that “opens selected code text” in a post “directly into a script editor window”:

http://scriptbuilders.net/category.php?id=2164
BBSpost2script
absolutely freeware