This is a website by Willem van Zyl

I'm a project manager, software developer, Apple evangelist and geek from South Africa. I'm passionate about web and mobile application development, usability, productivity, physics, astronomy, science fiction and fantasy.

If you would like to contact me, message me on Twitter or send me an email.

How to launch the Mac OS X screen saver from the command line with an AppleScript

13 Feb 2009

Mac OS X has a built-in command line utility named "osascript" that can read and execute AppleScript files or commands passed as arguments. By using osascript to launch the Screen Saver engine, you can lock your screen from the command line:

osascript -e 'tell application id "com.apple.
  ScreenSaver.Engine" to launch'

To shorten this command, you can save it as a bash script (named "launchss.sh" in this example) in your Home folder:

#!/bin/bash

osascript -e 'tell application id "com.apple.
  ScreenSaver.Engine" to launch'

... and run it by entering:

sh ~/launchss.sh

Alternatively, you can make the script behave like an application by saving it with a ".command" file extension instead of ".sh". To execute it, double-click it in the Finder or drag it to your Dock and launch it from there.

More details about osascript's usage can be found in its man page:

man osascript
Do you like this? Share it:

Copyright © Geekology 2011. All Rights Reserved.

Hosted by Code. Like. Clockwork.