DekGenius.com
Team LiB   Previous Section   Next Section
osacompile

Syntax

osacompile [-l language]  [-o name] [-e save file as execute only] [-d 
place the resulting script in the data fork of the output file] [-r 
type:id place the resulting script in the resource fork of the output 
file, in the resource specified by type:id] [-t the four-character file-type 
code for the script (the default is "osas")] [-c the four-character 
creator  code for the script (the default is "ToyS")] [one or more files 
or standard input]

Description

The osacompile program is located in your startup disk:usr:bin directory (or, /usr/bin/ ). You have to call osacompile using the /usr/bin/osacompile syntax.

When you enter a new shell or window with Terminal, the default working directory is the home directory of the user that is logged in. For example, my home directory is called bruceper, so when I enter a new shell, the working directory is /users/bruceper/. You can find the name of the working directory from the command line by using the pwd BSD command.

You can provide a filename for the new script by using the optional -o switch, as in:

/usr/bin/osacompile -o newscript scripttext.txt.

This command-line sequence would attempt to compile the file scripttext.txt, located in the current working directory, into a compiled script called newscript. The Terminal depicted in Figure 34-1 did not provide a new filename, so the new script received the default filename of a.scpt. Type the filenames or paths inside the Terminal window without quotation marks (e.g., /users/bruceper/newscript instead of "/users/bruceper/newscript"). In another example, let's say you want to compile a text file in another directory and save the new file in a folder other than the current working directory. You can use syntax such as:

/usr/bin/osacompile -o /users/bruceper/desktop/script2 /users/bruceper/documents/rawscript

This command-line sequence takes a text file rawscript located in the documents folder of user bruceper and compiles a new script called script2 in the same user's desktop directory. Are you getting the impression that it is much easier to create and compile scripts inside of a development program like Script Editor?

The osacompile command attempts to compile the text file as an AppleScript unless you specify another OSA language with the -1 switch, as in:

/usr/bin/osacompile -l JavaScript rawscript.txt

(assuming that a JavaScript OSA scripting component exists on the system). Use the osalang command (described later in this chapter) to get information on all of the system's OSA languages. You can specify more than one file argument for osacompile, which will attempt to compile all of the given files into one script. For example, one file could be a collection of subroutines, and the other could be a script that initiates some task by calling those defined routines.

You can also pass standard input or typed AppleScript code as opposed to a filename to the osacompile command. The typed standard-input code has to be enclosed in quotation marks (""). For example, the code:

/usr/bin/osacompile -o /users/bruceper/documents/script3 "return (2 * 50)"

will cause osacompile to create a compiled script called script3 in my documents folder. If you run this script inside Terminal with the osascript command, for instance, the return value of 100 (the value returned from the expression (2 * 50)) will show up in the Terminal window.

Standard input is command-linese for characters fed to the shell or command line from an input device such as a keyboard. Standard output is the opposite—characters such as an English phrase that are displayed to the user in the Terminal window. So the scripter can interpret standard input in part as text that they type at the Terminal window prompt.

Examples

You can compile and run the following example from the Terminal command line. It starts with a text file called rawscript.txt, which contains a tell statement that targets the Finder. The script just returns the number of items (count items), such as files and folders, contained by the logged-in user's desktop folder. This text file is compiled into an AppleScript called newscript, which is located in the /users/bruceper/documents/ directory. If you run this script on the command line with input such as usr/bin/osascript newscript (assuming that the current working directory in the Terminal window is /users/bruceper/documents/ ), then the script's integer return value (e.g., 8) will be displayed as standard output on the command line:

(* command line input in Terminal window: 
[localhost: ~] bruceper% /usr/bin/osacompile  -o /users/bruceper/
documents/newscript  /users/bruceper/library/desktop/rawscript.txt 
*)
(* contents of rawscript.txt *)
tell app "Finder"
   return (count items)
end tell
(* Example return value in Terminal window: an integer like '8' *)
    Team LiB   Previous Section   Next Section