Sunday, April 23, 2006


Running an inline script

What follows is an extract from an article I'm writing called How to Pass Information from One Script to Another:

You can if wish use doScript to run a script contained in a variable. For example:
myScript = "'hello'"
This trivial example is equivalent to:
and so there doesn’t seem to be much point to the exercise. However, notice the two sets of quotes in the assignment to myScript. With just one set of quotes:
myScript = "hello"
The script is comparable to:
which results in a run-time error to the effect that hello is undefined.

Notice that the error-free versions I dubbed “equivalent” whereas the versions with the error are only “comparable.” The difference is that in the error situations, the call to the alert will immediately give you an error message that hello is not defined whereas the doScript version first gives you that error but then gives you an alert that says “undefined”.

The reason for this is that the call to doScript creates a new script environment to run the script contained in myScript—you can see this very clearly if you run the script in ESTK—and that script is the one that hits the error condition and throws up the error alert. When you dismiss that error, the first script picks up again and completes the original alert command. Because the called script failed, the result is undefined and so that’s what the second alert says.

But enough of trivial examples. If all you could do with this were trivial there’d be no point in having the feature. The power of this kind of doScript is that you can construct scripts from data and run them easily. This is not something that comes up all that often, and I do not propose to construct a fabricated example at this point because it too would likely be trivial.

One not so trivial use for this feature is to provide a JavaScript with a mechanism for contacting the outside world via AppleScript or Visual Basic (depending on the platform in use).

Here’s an example of constructing a script to get the name of the current track being played by iTunes on your Macintosh computer:
function getCurrentSong() {
  var myScript = "tell application \"iTunes\"\n"
  var myScript = myScript + "get name of current track\n"
  var myScript = myScript + "end tell\n"
  return myScript
Of course, it goes without saying that this AppleScript could as easily have been in an external file and have been called from there. But non-trivial examples of constructing scripts inside scripts are hard to come by.

This is pretty interesting. I'm working on something that is perhaps similar. Where my javascript in InDesign will call a VBscript to open word and and file, and perfom a task. Then I will bring that file into InDesign. I will try to do it using your approach here.
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?