osascript

execute OSA scripts (AppleScript, JavaScript, etc.)

osascript [-l language args] [-i] [-s flags] [-e statement | programfile] [argument]

Executes OSA script, which may be plain text or a compiled script (.scpt) created by Script Editor or osacompile(1).
By default, treats plain text as AppleScript, but -l option. osalang lists the OSA languages installed .

AppleScript
JavaScript
Generic Scripting System

Scripts are located :

Any arguments following the script will be passed as a list of strings to the direct parameter of the run handler.
For example, in AppleScript:

           a.scpt:
           on run argv
               return "hello, " & item 1 of argv & "."
           end run

           % osascript a.scpt world
           hello, world.

           % osascript -e 'tell application "System Events"' -e 'key code 145' -e ' end tell'    # increase brightness
           % osascript -e 'tell application "System Events"' -e 'key code 144' -e ' end tell'    # decrease brightness
           % osascript -e 'tell application "finder" to sleep'          # Sleep the mac
-e statement Enter one line of a script. osascript will not look for a filename in the argument list.
Multiple -e options may be given to build up a multi-line script.
Because most scripts use characters that are special to many shell programs (for example, AppleScript uses single and double quote marks, ``('', ``)'', and ``*''), the statement will have to be correctly quoted and escaped to get it past the shell intact.
-i Interactive mode: prompts for one line at a time, and output the result, if applicable, after each line.
Any script supplied as a command argument using -e or programfile will be loaded, but not executed, before starting the interactive prompt.
-l language Override the language for any plain text files. Normally, plain text files are compiled as AppleScript.
-s flags Modify the output style.

flags is a string consisting of any of the modifier characters e, h, o, and s. Multiple modifiers can be concatenated in the same string, and multiple -s options can be specified.
The modifiers come in exclusive pairs; if conflicting modifiers are specified, the last one takes precedence.

  • h output values in human-readable form (default).
  • s output values in recompilable source form. osascript normally prints its results in human-readable form: strings do not have quotes around them, characters are not escaped, braces for lists and records are omitted, etc.

    This is generally more useful, but can introduce ambiguities. For example, the lists `{"foo", "bar"}' and `{{"foo", {"bar"}}}' would both be displayed as `foo, bar'. To see the results in an unambiguous form that could be recompiled into the same value, use the s modifier.

  • e script errors to stderr (default).
  • o script errors to stdout.

When running automated tests, using the o modifier lets you distinguish script errors, which you care about matching, from other diagnostic output,

See also

osalang(1), AppleScript Language Guide


osacompile

osacompile [-l language] [-e command] [-o name] [-d] [-r type:id] [-t type] [-c creator] [-x] [-s] [-u] [-a arch]                                         [file]

Compiles the given files, or standard input into a single output script. Files may be plain text or other compiled scripts.

-l language Override the language for any plain text files. Normally, plain text files are compiled as AppleScript.
-e command Enter one line of a script. Script commands given via -e are prepended to the normal source, if any. Multiple -e options may be given to build up a multi-line script. Because most scripts use characters that are special to many shell programs (e.g., AppleScript uses single and double quote marks, ``('', ``)'', and ``*''), the command will have to be correctly quoted and escaped to get it past the shell intact.
-o name Place the output in the file name. If -o is not specified, the resulting script is placed in the file ``a.scpt''. The value of -o partly determines the output file format; see below.
-x Save the resulting script as execute-only.
The following options are only relevant when creating a new bundled applet or droplet
-s Stay-open applet.
-u Use startup screen.
-a arch Create the applet or droplet for the specified target architecture arch. The allowable values are ``ppc'', ``i386'', and ``x86_64''. The default is to create a universal binary.
The following options control the packaging of the output file. only needed for compatibility with classic Mac OS or for custom file formats.
-d Place the resulting script in the data fork of the output file. This is the default.
-r type:id Place the resulting script in the resource fork of the output file, in the specified resource.
-t type Set the output file type to type, where type is a four-character code. If this option is not specified, the cre- ator code will not be set.
-c creator Set the output file creator to creator, where creator is a four-character code. If this option is not specified, the creator code will not be set.

If no options are specified, osacompile produces a Mac OS X format script file: data fork only, with no type or creator code.

If -o is specified and the file does not already exist, osacompile uses the filename extension to determine what type of file to create.
If the filename ends with .app, it creates a bundled applet or droplet.
If the filename ends with .scptd, it creates a bundled compiled script.
Otherwise, it creates a flat file with the script data placed according to the values of -d and -r .

EXAMPLES

To produce a script compatible with classic Mac OS:

osacompile -r scpt:128 -t osas -c ToyS example.applescript