Run AppleScript from the Command Line in Mac OS X with osascript

Aug 19, 2016 - 3 Comments

Terminal in OS X

Mac users can run AppleScript from the command line if desired, either by running a script file directly or by giving the osascript command direct plain text script statements. This can be useful for many purposes, but should be particularly nice for users who spend a lot of time in the command line or who perform remote administration tasks with ssh.


The osascript command will execute any OSA script, we’re focusing on AppleScript here but you can actually use osascript to execute Javascript as well if you use the -l flag to adjust the language.

Running AppleScript Script Files from the Command Line

To run an AppleScript script file from the Terminal in Mac OS, simply point the osascript to the .scpt command file path like so:

osascript /example/path/to/AppleScript.scpt

For example, if you saved this script to automatically connect to a VPN as a script file rather than an application, you could point the osascript command directly at the file to execute it. Any .scpt file can be launched simply by pointing the osascript command at the proper path, whether it was created in Script Editor of AppleScript or from a plain text file does not matter as long as the syntax is correct.

Running AppleScript Script Statements Directly from the Terminal

To run a specific AppleScript script or statement without having it saved as a .scpt file, you can simply use the -e flag and then the necessary single and double quotes to properly quote and escape the script.

For a few examples:

osascript -e 'display dialog "Hello from osxdaily.com" with title "Hello"'

Will display a dialog box saying “Hello”

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

Will open a new Finder window

osascript -e "set volume 0"

Will mute the system volume.

We have covered numerous short AppleScripts using the osascript command before, including gracefully quitting applications in Mac OS from the command line, setting Mac wallpaper from the command line, ejecting all mounted volumes, muting or changing system volume, and more. Anyone interested in learning more about AppleScript can find a significant amount of information, syntax, commands, and helpful guides in the ‘Script Editor’ application bundled with MacOS and Mac OS X.

Know of any particularly interesting tricks for using AppleScript from the command line? Let us know in the comments below.

.

Related articles:

Posted by: Paul Horowitz in Mac OS, Tips & Tricks

3 Comments

» Comments RSS Feed

  1. Karen says:

    In Mac OS High Sierra (Mac OS 10.13), can I use the terminal to add double arrows at both the top and bottom of the scroll bar using a script written for much earlier versions of Mac OS?
    The script was called DubDub Scroll Bars and is as follows:

    defaults write “Apple Global Domain” AppleScrollBarVariant DoubleBoth

    Up to Mac OS 10.6, it works, although you may then need to logout and login (or, perhaps, simply force quit the Finder) to see the effect … but once you’ve done so, you should have double-scroll arrows at both ends of your scroll bars. Will the script above work in Mac OS 10.13 using the terminal?

  2. Matteo says:

    Anyone know of any changes in Yosemite that would break Applescript/Terminal Command? Had one I used frequently in our little lab to login via ARD as admin to multiple machines at once to do certain items when no one was around. Hasn’t worked since Yosemite (to my knowledge). Here’s the command:
    osascript -e ‘tell application “System Events” to keystroke “admin”‘; \
    osascript -e ‘tell application “System Events” to keystroke tab’; \
    osascript -e ‘tell application “System Events” to delay 0.5’; \
    osascript -e ‘tell application “System Events” to keystroke “PASSWORDHERE”‘; \
    osascript -e ‘tell application “System Events” to delay 0.5’; \
    osascript -e ‘tell application “System Events” to keystroke return’

    and ARD kicks back:
    CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data for each event

    Whats my obvious overlook?

  3. olmo47 says:

    Hey I have a question: I use ‘AirVPN’ i would like to make it start when boot the Mac, I have try the script you have (or rather) you post in here, but will not work, also to start ‘AirVPN’ I have to sing in to this small window before starts, so not sure if a script (am sure some know how to) you post will work and by pass the sing in little window?

    any help be most appreciate, or a respond, Please

    Kind Regards

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site