Wednesday, December 7, 2022

Announcement will transition to a new Discourse server soon. Watch this space for the date and time of the transition. Please expect the site to experience periods of outage during this transition. See this and this for more details.
  • Index
  •  » unScripted
  •  » Managing Numerous Scripts using Services in Snow Leopard

#1 2009-08-29 06:22:08 pm

Craig Williams
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Managing Numerous Scripts using Services in Snow Leopard

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.

1. Create one Service for each context that lists its available scripts
2. Put all the scripts for this context in a specific folder
3. Choose script from a list and execute it

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"
       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 & "\""
       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.


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
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.

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!

Filed under: Finder, leopard, services, snow


  • Index
  •  » unScripted
  •  » Managing Numerous Scripts using Services in Snow Leopard

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)