Team LiB   Previous Section   Next Section
osascript

Syntax

osascript [-l language] [one or more files or standard input]

Description

The osascript command attempts to execute the files that are passed to the command as arguments. If you do not use the -l switch and the arguments do not look like filenames, then osalang attempts to execute the text arguments as standard input, dynamically run as an AppleScript. Now where else can you generate an AppleScript like that?

By default, osascript runs the text files or standard input as an AppleScript, but if you use the -l switch, you can specify another OSA language for it to use. Like osacompile and osalang, you have to use the syntax /usr/bin/osascript to call this command. Figure 34-3 is a Terminal window in which the following command has been entered:

/usr/bin/osascript "return (65 * 87)"

The osascript command runs this code phrase just as if you had entered the script into Script Editor, compiled it, and run it. The expression return (65 * 87) returns the value 5655—the product of 65 and 87—to the Terminal window as standard output.

Figure 34-3. Dynamically running standard input using the osascript command
figs/ascr_3403.gif

Examples

You can pass more than one script as arguments to the osascript command. If they are valid AppleScript code, then each script will be run, but I have found that only the return value from the second script is returned to the Terminal window. Unless you have navigated to the directory where the scripts are located, you have to identify the complete script path as arguments to osascript. For example, to run a newscript script located in the /users/bruceper/documents directory, use the syntax:

/usr/bin/osascript /users/bruceper/documents/newscript

The three OSA shell commands described in this chapter are finicky about any user interaction; a lot of scripts that display dialog boxes cause errors with these shell commands. However, The following example, when run from the command line with osascript, did successfully display a dialog window after activating the Finder and making it the frontmost application on the desktop:

tell app "Finder"

   activate

   display dialog "Hi there"

end tell

The following example describes two separate scripts and the osascript command sequence that ran them. Only the return value from the second script—called roundit (all it does is round a real number)—is displayed in the Terminal window:

(* command line input in Terminal window: 

[localhost: ~] bruceper% /usr/bin/osascript/users/bruceper/documents/getitems/users/bruceper/documents/roundit

*)

(* source code of first script: getitems *)

tell app "Finder"

   return (count items) (* return the number of files, folders, other desktop items *)

end tell

 (* source code for second script:roundit *)

round 3465.6

(* return value from Terminal window: 3466 (the integer returned from the second script) *)

The OSA-related Terminal commands, particularly osascript, hold much potential for integrating AppleScript with more complex shell scripts, such as passing the value of variables formed using other languages like Perl to osascript and running these values as though they were compiled AppleScript.

    Team LiB   Previous Section   Next Section