dougscripts.com

Using Script Editor 1 2 3 

Saving As "Application"

When a script is saved as an "Application" in Script Editor's Save As... dialog it behaves just like any other application. To run it you double-click its icon in the Finder, or activate it from some kind of launcher, or the Dock, or a Finder window's toolbar. (It will also appear and can be activated in iTunes' Script Menu if you saved it in iTunes' Scripts folder.) This can be a dandy way to run a script without iTunes having to be the front application. Try saving the "Play Top 25 Most Played Playlist" script as an application and running it. For now, leave the "Run Only", "Startup Screen" and "Stay Open" checkboxes unchecked.

The snippet above creates a list of all running applications (even hidden ones) to see if iTunes is listed among them. If it is, then the okflag is toggled to true. If the okflag is false, the script ends.

Saving As A "Droplet"

A droplet is an AppleScript application that performs various tasks on the file or files you drag-and-drop on to it. There are two important things to remember when writing and saving a droplet. First, they must be saved as "Application" (or "Application bundle") rather than as a compiled script in the Script Editor's Save As... dialog. Secondly, they must contain The Magic Words that enable files dropped on them to be processed. These are The Magic Words:

on open fileList
	repeat with thisFile in fileList
		-- some processing would go here
	end repeat
end open

The "on open fileList" line is telling the droplet to activate when some files have been dropped on to it and to make a list of references to those files, here called "fileList"—you can give it your own name, if you like. The closing line "end open" must be included at the end.

The second line begins a repeat loop. On each loop, each file in "fileList" will be referred to as "thisFile" in whatever scripting commands occur within the loop. The "end repeat" line is the end of the loop.

Look at the following script. It is a droplet that adds all the files dropped on it to iTunes' "Library":

on open fileList
	repeat with thisFile in fileList
		
		-- step through each file that was dropped on the droplet
		tell application "iTunes"
			add thisFile
		end tell
		
	end repeat
end open

Saving As A "Stay Open Application"

A Stay-Open Application is a script saved as an application that doesn't quit when it has completed its commands. The user must quit it from its menu or through some other means. Usually, a Stay-Open script application is used to "watch" for a particular condition to occur. For instance, various system events can be watched for and if one occurs the script will act accordingly. While a full discussion is beyond the scope of this article (but not this one), you will want to note that most often a Stay-Open script uses what's called an idle handler. A handler is a piece of code that performs a scripting routine; and an idle handler performs the routine when the system is idle, which occurs when its not doing anything else important. When the System is idle it sends an idle event which AppleScript can intercept.

To designate an Application script as "Stay Open" you must check the "Stay Open" box in the Save As... dialog.

Writing and debugging an idle handler can be very tricky, but quite rewarding when they work properly. You can find more information on writing handlers in the AppleScript Language Guide and this Bill Brigg's tutorial. We also have some details on this site at this page.

Saving As "Run-Only"

This option merely makes your script un-openable in Script Editor. If you were to create a script application which you felt contained some proprietary code, or code you wouldn't want users to alter (eg, other users on a network) you might want to save it as Run-Only. This option is available in the Format: menu in Script Editor's Save As... dialog.

Saving As "Text"

Sometimes you may prefer to save your script as text. This can be beneficial if your script won't compile correctly but you want to be able to save it so you can quit Script Editor and go out to buy a Pepsi. You can also save a script as text when you want other users to compile your script on their machines to be able to resolve various initial properties...(bzzt! advanced topic! beyond our current scope! skip it!). When you save a script as text, your script cannot run under any circumstances. It's just saved as an ordinary text document.

< prev | 1 2 3 
Site contents © 2001 - 2022 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.