Home > Power User >
Scripting & Workflow Automation

Overview

Both operating systems provide scripting languages. Mac OS X's primary scripting language is called AppleScript, while XP's is called WSH (Windows Script Host).


You can hear the newest music CDs and online music on your PC today. We also have great information on streaming music for your Mac OS X

OS X provides three methods for creating scripts/automating workflows:

  1. Automator (novices): Create an automated workflow by dragging and dropping pre-made "actions"

  2. Script Editor (intermediate): Create faceless workflows with all of the versatility of a scripting language

  3. AppleScript Studio (advanced): Create full-fledged AppleScript-based applications

Automator

Apple's
Automator lets users who know nothing about coding create complex workflows that can speed up common tasks. Automator provides hundreds of pre-built "actions" that each perform a specific task. To create a script, drag your choice of actions into a workflow. When ran, Automator will execute the string of actions in order, and as each action completes its work on a file or set of files, the results are passed along to the next action. If you like, you can set certain actions to prompt you with a dialog when the workflow is run, asking for further information.


Automator. Choose actions from the library at left, then drag them into your workflow at right

When running a workflow (either from within Automator, or once saved as a standalone application), a status pinwheel appears in the menu bar, alongside a description of the step in the workflow that is currently running, and a stop button to stop the workflow:

In addition to saving your workflow as a standalone application, it can also be saved as:

  • Finder plugin (makes your workflow accessible from the contextual menu of a selected file or files)
  • Folder Action
  • iCal alarm
  • Image Capture plugin
  • Print workflow plugin
  • Script menu item

This lets you use your workflow in the way that is most appropriate for the files you will be using with it. For instance, you can create a workflow that resizes images to a certain size, and then save it as a folder action. Attach it to a folder, and any images that you drop in the folder will automatically be ran through the workflow and resized appropriately.

Taking this even further, developers can extend Automator's abilities by creating actions for their own applications. Workflows can also be saved and shared with others, and Apple's own downloads page lists over 130 workflows built by other people that can easily be downloaded and used.

Script Editor

Script Editor allows you to write AppleScripts directly (rather than using Automator's drag and drop actions).


Script Editor

Script Editor supports syntax coloring, syntax checking, Script Assistant (essentially command auto-complete), event logging, and user-modifiable context menus to aid in creating common subroutines. Script Editor has a Record button that will allow you to record actions from recordable applications. This is a great way to learn AppleScript — have scripts write themselves!

Script Editor is best for making faceless scripts, or scripts that require minimal user interaction. For full-fledged applications with a rich interface, use AppleScript Studio.

You can use AppleScript to script any applications that support it. Each scriptable application comes with its own built-in dictionary so you can learn how to automate it.

Apple's Script Menu is available system-wide as a menu extra (next to the clock), letting you access scripts from any place at any time. Place scripts in ~/Library/Scripts/Applications/[Application Name] to create application-specific Script Menu items.

For die hard programmers, Unix shell and Perl scripting come preinstalled in Mac OS X, and both can be integrated with AppleScript. AppleScript's Open Scripting Architecture allows alternate scripting languages to be installed, like JavaScript. For Visual Basic fans, Microsoft has incorporated Visual Basic for Applications into its Office suite for Mac OS X, so Office applications can be scripted using VBA and/or AppleScript.

One nice aspect of AppleScripts is that they don't need to be recompiled each time they're edited. In fact, you can edit an AppleScript application while it's running. Click Save, and the running script will be updated — no need to restart it.

AppleScript Studio

OS X also comes with AppleScript Studio for creating AppleScript-based applications with user interfaces.


Using AppleScript Studio within Xcode

AppleScript Studio is part of Apple's Xcode developer tools, and is supplied on the Developers Tools CD that ships with OS X. It's also a free download from Apple's Developer site (membership to the site is free as well).

Windows XP

Microsoft's scripting languages hook directly into Windows XP's underpinnings. The idea with this approach is that if you can hook directly into Windows, your scripts should be able to do just about anything.

Annoyances.org has a decent introduction to Windows Script Host. According to Annoyances.org:

"The cool thing about the Windows Script Host is that it is language-independent, meaning that it will work with any modern scripting language. It has built-in support for JavaScript and VBScript, but can be extended (with third-party add-ons) to use almost any other language, such as Perl, TCL, Rexx, and Python."

XP does not ship with a "true" script editor. Since scripts are plain text, they can be created with NotePad; however, there is no syntax coloring, syntax checking, event logging, etc.

Scripters looking for a full fledged development environment will need to download a freeware or shareware solution (such as Gvim), or purchase Microsoft's Visual Studio.

XP does not ship with a debugging tool, but Microsoft provides them for free on its website, alongside the Microsoft Script Debugger. XP also lacks built-in dictionaries for learning scripting commands. Microsoft, however, provides extensive online documentation.

Learn more about XP scripting at Microsoft TechWeb.

Example Scripts

Let's look at the differences between AppleScript and Microsoft's scripting languages.

Example 1: Subroutine to download an image to your hard drive

AppleScript: (Using URL Access Scripting, displayed using Script Editor's syntax coloring)

on downloadImage()

   set web_URL to "http://www.xvsxp.com/images/xvsxp.gif"

   set file_path to "Macintosh HD:xvsxp.gif"

   tell application "URL Access Scripting" to download the web_URL to file file_path

   display dialog "Done"

end downloadImage

Visual Basic: (Using Microsoft Internet Transfer Control displayed using Microsoft Visual Basic 6.0's syntax coloring)

Private Sub cmdWriteFile_Click()
  Dim b() As Byte
  Inet1.Protocol = icHTTP
  Inet1.URL = "http://www.xvsxp.com/images/xvsxp.gif"
  b() = Inet1.OpenURL(Inet1.URL, icByteArray)
  Open "C:\xvsxp.gif" For Binary Access
  Write As #1

  Put #1, , b()
  Close #1
  MsgBox "Done"
End Sub

As seen in the above example, AppleScript is a very easy scripting language to learn, especially for novices. You can use expressions like "is greater than or equal to" and "isn't equal to" in AppleScript, or, if you prefer, you can use mathematical operators like ≥ or ≠. Windows users can't use either of these, so they must rely instead on the popular (yet more cryptic) programmer's comparisons, such as >= and !=. This is fine for experienced scripters, but novices may find it discouraging.

Example 2: Scripting speech

Mac OS X:

Add the following to any of your scripts:

say "Is there something I should say?"

Replace "Is there something I should say?" with whatever phrase your want the computer to speak. In addition, you can specify the voice to use:

say "This is cool" using "Kathy"

And, you can even save the output to a file:

say "This is cool" using "Kathy" saving to "someText.aiff"

This simple script harnesses AppleScript's power to make a bunch of Macs literally talk to each other! While it's a somewhat goofy example, it does help to illustrate AppleScript's power.

Windows XP:

This excerpt from CMP's TechWeb (September 1, 2000) outlines how to script speech in Windows XP:

"Sometimes, you need your PC to tell you what's wrong. The little script below lets you have the PC speak a message of your choice over the speakers. It uses the same ActiveX control as the Zinger ISP monitor, although I was using Perl for that project. You'll need to install the files for Microsoft Agent core components and text-to-speech engine (link no longer available) if you don't have them already.

var vt = WScript.CreateObject("Speech.VoiceText"); vt.Register("", WScript.ScriptName); var phrase = "Is there something I should say?"; if ( WScript.Arguments.length ) phrase = WScript.Arguments(0); vt.Speak(phrase, 1); while ( vt.IsSpeaking ) WScript.Sleep(100); WScript.Quit();

"Just take the lines above, save them in a file named SAYIT.JS, and run the file. You should hear it say "Is there something I should say?" (If you get an error message instead, you probably need to install Microsoft Agent [this was a free download from MS's developer site, however the link has disappeared].) To customize the message, you can edit the phrase in the SAYIT.JS file. However, the script also checks its argument list to see if anything has been passed in. If there's an argument, the script speaks its argument instead of the built-in phrase. To use this feature, create a shortcut (Right-click the desktop, select New > Shortcut) and enter a command line like:

  path\SAYIT.JS "I want to say this"

Where path is the folder where you saved the file. Launch the shortcut and it will say "I want to say this". Cool, eh?"

Example 3: Display free disk space

Mac OS X:

tell application "Finder" to display dialog (free space of startup disk) as string

Windows XP:

From Microsoft's own TechNet web site:

Set objWmiService = GetObject("winmgmts:")
Set objLogicalDisk = objWmiService.Get("Win32_LogicalDisk.DeviceID='C:'")
WScript.Echo objLogicalDisk.FreeSpace

One nice feature of AppleScript is how it lets the user use "natural" language. You can use "the" and other similar words to make it easier to read your code; AppleScript will simply ignore them. For example, the following script will display a dialog of all the first names of people in your address book whose last name contains the letter "e":

tell application "Address Book"

   set theFirstNameList to the first name of every person whose last name contains "e"

   display dialog (theFirstNameList as string)

end tell

Notice how the second line in particular almost reads like a regular English sentence. You can even use the word "whose" when referring to people. This makes AppleScript relatively easy to learn, yet still fairly powerful.
Copying code from Script Editor maintains its styling, which is useful if you want to quote pieces of code in another document (such as this very page!). Code copied from Visual Basic loses its styling, however.

Power user tip: Hard core coders have pointed out that XP's WSH may make scripts run faster than their AppleScript equivalents, due to the fact that they run at a "lower level" of the operating system.

Web Applications

Mac OS X

OS X comes with Apache, loaded with the widely popular and open source Python, Perl, and PHP for web application scripting languages. Updates and patches are managed automatically through OS X's Software Update.

Windows XP

XP comes with with IIS and ASP. XP users can download and install open source environments such as Perl and PHP (though they would have to keep track of updates/patches manually).

Microsoft's .NET strategy has provided Windows users with a rich array of tools from which to develop both desktop and web applications. Microsoft does offer ASP.NET Web Matrix for free. This is really a scripting aid for ASP.NET web sites, and is of little value unless you have purchased the .NET server package.


Conclusion

For those who are averse to code, OS X provides Automator for drag-and-drop scriptability. Even though Automator uses a "dumbed down" interface (in comparison to writing actual code), it is surprisingly powerful, thanks to the way that workflows can be saved as plugins for various uses. In addition, it is highly extendable, letting users download new workflows and actions. XP does not have a utility that even approaches this level of ease of use. That said, more ambitious users will want to learn their operating system's scripting languages, as this will let them harness its full power and potential.

XP's support for its wide array of scripting languages makes it versatile, but not necessarily easy, and essentially unattainable for those who are unwilling to learn code. AppleScript's plain English syntax, built-in dictionaries, syntax coloring, code auto-complete, code context menu, and script recording make it significantly easier to learn and use; however, one could argue that this is at the expense of power and speed.

Mac OS X and Windows XP have comparable built-in support for developing web applications.

Mac OS X: 8
Windows XP: 3

Back Scheduling Automated Tasks | Next Network Installs