terminal

Run AppleScript From the Command Line

This is a Terminal Tip on how to run AppleScript from the command line. This is possible via the osascript command.

Running AppleAcript greatly increases what you can do remotely from the command line, and enables a lot of cool tricks that are hard to do otherwise. While you can run whole scripts in the command line, this guide is only about running short one line commands.

By using this guide you can also log into a remote computer (using ssh) and run the AppleScript on that computer, via the osascript command. Osascript can also run any other Open Scripting Architecture language using the -l modifier, but we will only be covering its use with AppleScript here.

Requirements
    • Terminal
    • Script Editor

Procedure

1. Determine What Script You Want to Run

Before you can run anything in the command line you need to know what you are gonna run. There are several ways to come up with applescript to run. Of course you can always write your own, if you’ve never used applescript before I recommend taking a look at this article . You can also use the record tool in Script Editor.

Depending on what version of OSX you run, Script Editor can be found in a different place and is called by a different name.

In 10.5 you can find it in /Applications/AppleScript/Script Editor but if you are running 10.6 you can find it in /Applications/Utilities/AppleScript Editor

In the top left corner of the AppleScript Editor there will be a button marked “record“. When you click it, AppleScript will be generated based on your actions.

For example, if you right click (control+click) the Finder icon in your dock and select “New Finder Window”, AppleScript Editor will output:

tell application "Finder"
     Activate
     make new Finder window
end tell

2. Reformat the AppleScript

The AppleScript that we got out of Step 1 is not going to be parsed correctly by osascript. We need to “rephrase” the AppleScript into something that can be run in the command line. The previous code example would be rephrased to:

tell app "Finder" to make new Finder window

Depending on what your trying to do, this step may be easy or hard. There is an element of trial and error involved in the rephrasing, so if the command you try at first doesn’t succeed, try phrasing it differently.

3. Open Terminal

The first thing you need to do in order to run AppleScript is Terminal. You can find it at /Applications/Utilities/Terminal.app

If you are going to be running these commands on another computer, login via ssh now.

4. Enter your command

Now at the command line prompt, enter:

osascript -e 'applescript command'

Replace applescript command with whatever command you are planning to run. So if I was trying to open up a new Finder window I would type:

osascript -e 'tell app "Finder" to make new Finder window'

When you hit enter a new Finder window will pop up behind your Terminal window. This is a very simple command and is by no means the limit of what you can do with AppleScript.

Some Additional Commands

osascript -e 'tell app "applicationname" to display dialog "Hello World"'

This code makes applicationname (an application of your choice) open up a dialog box containing the text “Hello World”

osascript -e "set volume number"

This sets the computers volume to number. The number can be any value from 0 to 7, with 7 being the highest possible volume and 0 being the lowest (muted).

Have any other awesome AppleScript commands?

Let us know in the comments section below! We’d love to check them out!

7 Comments

  1. bluedisc

    05.23.2011

    Reply

    How can i activate the finder?
    osascript -e ‘tell app “Finder” to display dialog “Hello World”‘ (works)
    osascript -e ‘tell app “Finder” to activate’ (dont work)

    have someone a suggestion how to activate the finder over terminal?
    I need it for CronniX (cronjobs)!!

    Thank you very much!

    • Jeff Browning

      05.23.2011

      No need for AppleScript for that one! Try:
      open -a Finder

      For dialogue boxes and the like, I would recommend calling on “System Events” instead of Finder, as it’s intended for these things and is a constantly running background process.

      If you noticed, when you run the command:

      osascript -e ‘tell app “Finder” to display dialog “Hello World”‘

      Finder bounces in the menu bar, but if you have another application open the message will stay below and you won’t see it. If you use System Events, the alert box simply appears over anything that’s there.

      Here’s an example of a “warning” dialogue box that uses System Events (I also included a few other options to set the icon and the text on the button):

      osascript -e 'tell app "System Events" to display dialog "30 seconds until server shutdown. Please save your work quickly. The system will resume shortly after." with icon 0 with title "Save Your Work" with text buttons {"I Will Save My Work"}'

      Hope this helps!

    • bluedisc

      05.24.2011

      thanks a lot!!

    • Joey

      06.23.2011

      Hey Jeff, I was trying to make my own warning message like that, and it worked a couple of times, but now it just times out. Any help? Do you have to make the icon number and the text buttons and title? Also, it says that the icon number O is not defined.

  2. Mark

    07.17.2011

    Reply

    here is a good applescript that I found:

    with timeout of 300 seconds
    tell application “Finder”
    activate
    set Input to display dialog “Please enter your password:” with title “Password” with icon 0 default answer “” buttons {“Cancel”, “OK”} default button 2 giving up after 295 with hidden answer
    return text returned of Input as string
    end tell
    end timeout

    I put osascript -e ‘ code ‘ into terminal, note I replaced code with the code reformatted. When I pressed enter I got this “8:32: syntax error: Expected end of line but found “tell”. (-2741)”

    any help?
    thanks

  3. Joshua Paul

    12.26.2011

    Reply

    multiline scripts require -e each time

    example

    osascript -e “line 1″ -e “line 2″ -e “line 3″

  4. cathy

    02.27.2012

    Reply

    mac 10.1.5 is read only i have no disc no disc with it , i need to upgrade but it wont let me do anything cant set up email, need to update browser to get into face book, help i can not make this mac work

Leave a Reply