System Interaction

These commands and functions allow your script to interact with the system where the script is running, to launch other programs, open files in other programs, or run system commands through the UNIX shell.

  • open command

What it Does

Launches another program, or opens a file with another program on the machine where your script is running.

When to Use It

Use the open command when you want to open a file with another program or launch a particular program on the machine where your script is running. The most common use of this command is probably to open a file generated by the script, such as opening a text file in a text editor, or a tab-delimited file in a spreadsheet program, so the user can make further changes to it, print it out, etc.

Examples

open "iTunes" -- launch iTunes on this machine

open "/tmp/myFile" with "TextEdit"

Tech Talk

Syntax: open application
open
file with application

If the requested application is already running on the machine where the script is executing, it will be brought to the front, otherwise it will be launched.

Note: Opening files

To open a file for reading or writing its contents within a script, use the open file command, documented in Working with Files and File Systems.

  • shell command and function

What it Does

Executes a command in the command-line shell on the local machine and returns the output.

When to Use It

Use the shell command when you want to launch a command-line program, or the shell function when you also want to obtain the output that it generates.

Examples

shell "rm /tmp/testfile"

put shell("cd /; ls -l") into rootDirectoryList

Tech Talk

Syntax: shell command {, optionalParameters}...
get shell( command {, optionalParameters}...)

The shell command and function both set the result to the exit code of the command that was executed. Typically the exit code is 0 when a command runs successfully and some other value when there is a problem, but the exact meaning of the exit code depends on the command that was run.

The command is executed on the machine where the script is running. To execute a shell command on another machine, you can use one of the shell commands that provides remote access, such as rsh or ssh.

The shellCommand global property controls which shell program is used to run the command. By default, on Mac and Linux the shellCommand is set to “/bin/sh” (the Bourne shell), and on Windows it is set to "ShellExecute" (see below). To execute commands in a different shell, set the shellCommand to the full path of the shell you want to use before calling the shell function, or to "ShellExecute" or empty.

On Windows the default setting of the shellCommand global property is "ShellExecute". This causes the Windows ShellExecute() function to be used to run the specified file:

set the shellCommand ton "ShellExecute"

shell "example.bat" -- run the indicated batch file

When using ShellExecute on Windows, several additional parameters besides the command or file may optionally be passed. The second parameter, if given, specifies any parameters to be passed to the command being run. The third parameter specifies the default working directory for the action. The fourth parameter should be a number specifying any optional flags to pass to the underlying ShellExecute() function. Finally, the fifth parameter, if given, specifies an explicit verb to use (such as "open", "explore", "edit", "find", or "print"). Otherwise the default verb defined in the registry (or "open") will be used. (When using ShellExecute, no output is returned.) If an error occurs, the result will be set to a number, otherwise it will be empty.

When the shellCommand is not empty and is not set to "ShellExecute" (the usual case on Mac and Linux, where it is set to "/bin/sh" by default), if multiple parameters are passed to the shell function they are treated as separate commands to each be executed in sequence within a single shell context.

When the shellCommand is empty, shell treats its first parameter as the command to execute and any additional parameters as parameters to be passed to that command. If only a single parameter is passed, it is split by any spaces that are present in the parameter to derive the command and its parameters.

set the shellCommand to empty

put shell("ls -l") -- runs the 'ls' command with '-l' parameter

put shell("ls", "-l") -- does the same thing