AppleScript Programming/Shell Programming

From Wikibooks, open books for an open world
Jump to navigation Jump to search

AppleScript can interact with UNIX shell scripts in a couple of ways. AppleScript can call UNIX shell scripts and utilities using the "do shell script" command. UNIX shell scripts can call the "osascript" utility to execute AppleScript code.

AppleScript command "do shell script"[edit | edit source]

To run shell commands from Applescript:

do shell script "afplay /System/Library/Sounds/Glass.aiff"

The user would hear a sound.

Shell command "osascript"[edit | edit source]

The osascript utility allows shell scripts to execute Applescript code in three different ways.

  1. Line by line from within a shell script using the osascript -e
  2. Specify a plain text file or compiled script file using osascript filename.
  3. Through the standard input.

For example, the following command in a shell script or at a prompt, will open a Mac OS X dialog box and wait for the user to respond:

osascript -e 'tell app "System Events" to activate' -e 'tell app "System Events" to display dialog "Hello, world!"'

The user would see:

Mac OS X hello world dialog

Next Page: Script Libraries | Previous Page: System Events
Home: AppleScript Programming