dougscripts.com

AppleScripts As Automator Plug-Ins

Automator Plug-Ins are accessible via a Control-Click in the Finder or from the system-wide Script Menu. Using existing AppleScripts, or those you want to write yourself, you can use Automator's "Run Script" Action to create Workflow Plug-Ins. Here's how.

As of Snow Leopard (OS 10.6), Script Editor.app has been renamed AppleScript Editor.app and is located in your /Appplications/Utilities/ folder. This article refers to Script Editor.app but AppleScript Editor.app also applies.

Introduction

If you want to access AppleScripts easily from anywhere, without neccessarily bringing iTunes to the front to activate them, you can put your scripts in the system-wide Script Menu. However you can access Automator Plug-Ins by control-clicking in the Finder and selecting a Plug-In from the "Automator" sub-menu. Using the Automator Action "Run AppleScript", you can copy AppleScript code from existing AppleScripts and save the Workflow as a Plug-In. This article will give you some pointers.

I am not certain that accessing AppleScripts in this way is always easier than selecting them from the Script Menu or from iTunes' Script menu. However, there are certainly some situations where it is easier or more advantageous to Control-click in the Finder.

Creating an Automator Plug-In

Open Automator and drag the "Run AppleScript" Action to the Workflow window:

automator plug-ins


All you have to do is replace that (* Your script goes here *) with new AppleScript code. In this example, I'm going to use this script, which toggles iTunes' mute setting:

tell application "iTunes"
	set mute to (not mute)
end tell

Copy the code into the Action window:

automator plug-ins


Make sure you don't delete any of the other code in the Action. You can test the script by clicking the "Run" button in the upper-right of Automator, or by clicking the green "Run" button in the Action. If it does as you expect, you can now save it as a Plug-In.

Select "Save As Plug-In..." from Automator's File menu. Give the new Plug-In a name, in this case something like "Mute-Unmute"—don't use slashes in your Plug-In name! Then, in the "Plug-In for" pop-up, select "Finder". Choosing "Finder" will enable you to Control-click in the Finder and select the Plug-In from the "Automator" sub-menu that appears in the contextual menu pop-up. Selecting "Script Menu" will put the Plug-In in the system-wide Script Menu.

Things to think about

You can't use AppleScript properties in a Plug-In. If necessary, change a property to a set statement. For example, to use this script—skip ahead 30 seconds in the current track—which originally contained a property:

property differential : 30 -- seconds

tell application "iTunes"
	if current track exists then
		if player position is greater than start of current track ¬
			and player position is less than ((finish of current track) - differential) then
			set player position to (player position + differential)
		end if
	end if
end tell

...just set the variable differential within the tell statement:

tell application "iTunes"
	set differential to 30 -- seconds
	if current track exists then
		if player position is greater than start of current track ¬
			and player position is less than ((finish of current track) - differential) then
			set player position to (player position + differential)
		end if
	end if
end tell

To bring iTunes to the front, use the command activate. The following script activates iTunes and selects the currently playing track in its playlist:

tell application "System Events"
	tell application "iTunes" to activate
	key code 37 using command down
end tell

You can't use handlers! Something like this won't work:

tell application "iTunes"
	set t to name of current track
	my show_name(t)
end tell

on show_name(t)
	display dialog t
end show_name

Site contents © 2001 - 2024 (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.