Friday, October 1, 2010

osascript

I've recently become aware of the power of osascript. It is a command line tool, or program, that can be used to perform many tasks on OS X. My first encounter with osascript was to use it to change the volume as follows:

osascript -e 'set Volume 4.5'

You can use a basic integer of 1 through 10; or use the decimal system for a more accurate volume.
This is particularly useful when your child is playing an obnoxiously loud game and you have a desire to adjust the volume without interrupting them. Or perhaps you are just hopelessly lazy and don't want to get up to change that volume for iTunes. All easily accomplished through a secure shell connection in the command line.
I've also successfully used this while I was away from home, with an internet connection, but without a phone. I was able to get Share to email me a much needed file.

Perhaps you would like to play an April Fools joke on someone running OS X?

osascript -e 'tell application "System Events" to sleep'
osascript -e 'tell application "Finder" to sleep'

Nighty night remote mac.


osascript -e 'say "Hello, $name! I am your mac." using "Zarvox"'

What your computer has somehow attained artificial intelligence using a very computer sounding voice! For a really lively conversation type out a good number of responses and questions to easily cut and paste into the command line. To have everything said to the tune Night on Bald Mountain, use cellos instead of Zarvox!


osascript -e 'tell application "[Open Program Name]" to quit'

You know that the application did not crash, but will they?


How about a list of iTunes controls?
open /Applications/iTunes.app
osascript -e 'tell application "iTunes" to play'
osascript -e 'tell application "iTunes" to stop'
osascript -e 'tell application "iTunes" to pause'
osascript -e 'tell application "iTunes" to next track'
osascript -e 'tell application "iTunes" to previous track'
osascript -e 'tell application "iTunes" to set shuffle of current playlist to 0' (Shuffle OFF)
osascript -e 'tell application "iTunes" to set shuffle of current playlist to 1' (Shuffle ON)
osascript -e 'tell application "iTunes" to sound volume as $integer' ($integer being a number as previously mentioned.)

All very nifty. I still haven't figured out how to populate the playlist with desired tracks, albums or artists. Apparently there is a way if a playlist exists but it should be able to do it without one.

There is a plethora of things to do with osascript. I've just begun delving into it. All potentially useful if you are a command line junkie or if you just cant be physically next to your mac for some reason. Like perhaps you want your mac to loudly play music while you are away on vacation? A few hours of that may give the impression to passers by that you are at home. All of that could even be timed and executed by a cron job.

0 comments:

Post a Comment