Switching Desktops, AppleScript Style
AppleScript shines best when fitted into your workflow. One of the things a writer for InformIT needs to do is create screenshots that conform to a 550 x 420 pixel image size standard. By creating a desktop pattern with a black 550 x 420–pixel rectangle against a plain white background, it is easy to arrange or resize objects so that they fit as closely as possible to the required size. This minimizes the amount of scaling and cropping that needs to be done to the screenshot afterward.
The annoying part of the process is that you need to go through several steps to change the desktop pattern first to the one with the background guide and then a few more steps to get back to the normal OS X desktop pattern the publisher prefers. AppleScript to the rescue!
The following script tells the Finder to change the desktop pattern to the one with the background guide on it ("Informit Window.jpg"). There is then a routine that loops until told otherwise (everything between "repeat" and "end repeat"). The routine starts with a five-second delay, which gives the user time to resize and arrange the objects on the screen ("delay 5"). A dialog then appears with two buttons ("set the_dialog to..."). If the Yes button is pressed, the routine stops ("exit repeat") and reverts the desktop pattern to the Aqua Blue desktop pattern.
tell application "Finder" set desktop picture to document file "Informit Window.jpg" of folder "User Pictures" of folder "Desktop Pictures" of folder "Library" of the startup disk repeat delay 5 set the_dialog to display dialog "Done yet?" buttons ["No", "Yes"] default button 2 if button returned of result is "Yes" then set desktop picture to document file "Aqua Blue.jpg" of folder "Desktop Pictures" of folder "Library" of the startup disk exit repeat end if end repeat end tell
Figure 3 In this case, an AppleScript is used to toggle between two backgrounds—the first making resizing windows for screenshots easier before reverting to the standard desktop pattern in preparation for the actual screenshot itself.