For those of you who have not upgraded to Snow Leopard you can still create Services using ThisService. Kevin Bradley has an article here showing you how.
Using Services in Snow Leopard
Snow Leopard just came out and one of the coolest new features is Services. We have had Services for a while now but they have been difficult and confusing to use and difficult to create yourself. Snow Leopard changes all that by organizing Services in System Preferences, making them context sensitive and giving us the ability to create our own using Automator.
I will not be covering the System Preferences side or how Services work. You can find out all you want to know about Services at the Mac OS X Automation website.
What I am covering is a technique to make using lots of scripts easy to implement and maintain.
When I first discovered we have the ability to create Services I got very excited and started looking at all the functionality I wanted to implement. The sheer number of scripts I want to convert to Services is overwhelming and I do not want to clutter up the UI like it was before. With a little thought I came up with this solution.
Let’s get Started
We’ll begin by creating the folder and putting some scripts inside it.
Create a new folder inside the Scripts folder (/Users/user_name/Library/Scripts/) and call it “_Services”
Inside this folder you will make folders for each context, eg. “Finder”, “Safari”, “Mail”, etc.
For now just make the one for “Finder”
Add Some Scripts
Next add some scripts to this folder. Since this is in the context of files and folders you should choose scripts that relate to these areas. If you don’t have any in mind I am posting two here to get you started.
The first one will open two Finder windows and set the location and view of each. The second will copy the selected item(s) path(s) to the clipboard.
set locationOne to ((path to scripts folder from user domain as string) & "_Services:Finder:")
set locationTwo to (path to desktop as Unicode text)
tell application "Finder"
set bounds1 to {81, 100, 831, 495}
set bounds2 to {81, 536, 830, 950}
set window1 to make new Finder window to locationOne
set window2 to make new Finder window to locationTwo
tell window1
set current view to column view
set bounds to bounds1
end tell
tell window2
set current view to column view
set bounds to bounds2
end tell
end tell
tell application "Finder"
activate
try
set theSelection to a reference to the selection
on error
display dialog "You must have something selected." buttons {"OK"} default button 1
error number -128
end try
set thePath to theSelection as alias
set theChosenPath to button returned of (display dialog "HFS or POSIX." buttons {"POSIX", "HFS"} default button 2)
if theChosenPath = "POSIX" then
set the clipboard to "\"" & POSIX path of thePath & "\""
else
set the clipboard to "\"" & thePath & "\""
end if
end tell
On to Automator
Launch Automator and choose “Service” as the template.
Choose Service Template
Set the input as “files and folders” and the application as “Finder”
Files and Folder - Finder
Add a “Run AppleScript” step.
Run AppleScript Step
Paste in the code below.
AppleScript
Save the file and give it the name “Finder - Choose Script to Execute” or something else if you like.
Close the Automator file.
Run AppleScript Script Step Code
set scriptFolderPath to alias ((path to scripts folder from user domain as string) & "_Services:Finder:")
tell application "Finder"
set theScripts to name of every file of folder scriptFolderPath whose name extension is "scpt"
end tell
set scriptList to {}
repeat with i from 1 to count of theScripts
set thisScript to item i of theScripts
set thisScript to text 1 thru -6 of thisScript
set end of scriptList to thisScript
end repeat
set scriptChoice to choose from list scriptList without multiple selections allowed
if scriptChoice is false then
return
end if
set scriptToRunPath to scriptFolderPath & scriptChoice & ".scpt" as string
run script file scriptToRunPath
Back in the Finder
Right-click on any file or folder in the Finder at the bottom of the contextual menu you will see the Service you just created. If all goes as planned, when selected, you will be presented with a list of the scripts you added to the “Finder” folder. Choose one, click “Ok” and the script will execute.
Conclusion
This is a simple technique but hopefully you see the power in using it. Now when you want to add new functionality to the files and folder context just place the script into the “Finder” folder and it is automatically available to you without cluttering up your contextual menu. Add additional contexts the same way.
If you are supporting other users you might want to use a centralized location for your scripts. This way, it will be easy to add new functionality and you only have one place to keep up and maintain code. A good option for this is Dropbox. You can share scripts with your office staff and your friends around the world from the same place. How cool is that!
I believe Services has incredible potential and I can’t wait to see all the cool ways people use it.
Until next time, happy coding!