turn an applescript into a service

the applescript outlined in email file from finder II takes selected items in the finder and attaches them to a new outgoing email message. it allows for multiple recipients from your address book and automatically adds a subject line and some basic content to the email.

there are a few ways to make that applescript accessible in the finder but the most convenient is to turn it into a service. this method is for OS X 10.6 and later — you can see a similar procedure for earlier operating systems in the post get file path of finder items.

Automator Iconapplescript services can be created through automator — the automation program that comes standard on every mac since OS X 10.4 (you’ll find it in your applications folder).

when you first open automator you’ll see this screen — choose service :
screen grab of automator startup screen

this next screen grab shows a couple of different things you need to do. in the main window on the right are two dropdowns — set the first to files or folders and the second to finder. in the search field on the left type ‘applescript’ to easily find the run applescript action. drag and drop this action into the right window :
screen grab of automator with service being built

then paste your applescript into that action window — you can entirely replace the default script. this is what it should look like after you hit the compile button (hammer) :
screen grab of automator service window with applescript in place

save that out and you’re done. now, whenever you want to email something from the finder just select it and right-click — you’ll see your service near the bottom of the contextual menu :
screen grab showing the service as a contextual menu item

you can also activate it under finder > services :
screen grab of services menu

under that menu you can also access the services preferences. this allows you to even set a keyboard shortcut for your new handy service :
screen grab of services preferences window

of course, services aren’t just restricted to running your applescripts. investigate the full power of automator … and get grunting.

macgrunt icon

get file path of finder items

here’s a handy trick based on an original post you can find here at Macworld.

There may be times—such as when you’re trying to convey to someone where a file can be found on their machine, or maybe when you’re writing an OS X hints column—when you need to put the path to some file or folder into another document. — Rob Griffiths, Macworld.com

Applescript Iconthe original script does something very simple — a basic task which is missing from OS X — copying the filepath of a selected finder item into a text format. the original script looks like this :

running this script will place text on your clipboard — ready to paste into an email or whatever. the text would look something like this :
/Applications/AppleScript/Script Editor.app
this shows that ‘Script Editor’ can be found in the folder ‘Applescript’ which is in the folder ‘Applications’ — a slash delimited filepath.

you may prefer the more common colon delimited filepath which also shows the home volume — MacGrunt:Applications:AppleScript:Script Editor.app
if so, you’d replace those two lines in the middle with this :

set the clipboard to the selection as text

but here’s another variation which does two additional things :
allows for copying filepaths for multiple items
changes the output to an arrow delimited format

results look more like this :
MacGrunt > Applications > AppleScript > Script Editor.app
MacGrunt > Users > macgrunt > Desktop > Picture 1.png

you can download CopyPath as a finished script here.

now for the cool bit …

Automator Iconhaving to launch an app every time you want to copy a filepath would be a right pain. but you can turn this script into a contextual menu item with automator. here’s how.

prior to OS X 10.6 (Snow Leopard) :
open automator and search for the action “Run Applescript”
drag this action into your workspace
replace the default script with the downloaded one :

then choose File > Save As Plug-in… :

now whenever you want to copy a filepath to your clipboard, simply right-click (or control-click) on the file/s and navigate to the bottom of the contextual menu, like so :

slightly different for OS X 10.6 (Snow Leopard) :
automator changed in 10.6 to include the ‘Service’ option. using this option you’ll see the top of the workspace looks more like this (click to enlarge) :

set the service to allow input from files and folders in the finder.

and for Lion users … :
you guys are so advanced, you’re sure to work it out yourselves.

you’re bound to find many other uses for this handy feature of automator.
have fun.

macgrunt icon

renaming finder items

sometimes you need to rename a whole bunch of files. sometimes you find you’re doing the same kind of renaming time and time again. sometimes this gets exceedingly tedious. automator and applescript are perfect for this kind of monkey-work.

Automator Icon in automator, investigate the various options in the rename finder items action. you can add dates and times, renumber files sequentially, change case, all sorts of stuff. the action window also shows you an example of what the final filename will look like — this is a big help when looking at all the different date/time options.

the example below shows how to replace all spaces and hyphens in filenames with underscores (click to enlarge the screen grab). the first ‘find’ field has a space typed in it. after you set it up, save the workflow as an application and drag it onto your sidebar for easy access.

unlike the mounting servers example, this workflow doesn’t need a get specified finder items action — because it works as a droplet instead, which means you just drag-and-drop files onto the app and they’ll be renamed in less time than it takes to say “wow, that’s fast!”. The screen grabs below show filenames before and after conversion (click to enlarge).

Applescript Icon but some renaming tasks are too complex for automator and that’s when you need applescript. for example, this script truncates filenames by stripping out everything after and including the first space, hyphen or underscore. perfect for renaming product images for a webshop (and a million other household purposes). download and drag it to your sidebar — it works as a droplet. The screen grabs below show filenames before and after conversion (click to enlarge).

caution: this script works exactly as described, but make sure you’re happy with how the script renames your files before running it on a big batch — you can’t ‘undo’ file renaming in the finder (unless you have a time machine).

you can get a copy of the TruncateFilenames app here

for those interested in this sort of thing…
applescript droplet icon when an applescript is saved as a droplet, its icon includes a downwards pointing arrow. the on open mgItems line at the start of this script is what allows the saved application to run as a droplet. without that on open command you’d need to find some other way to reference the files to be changed.

you could select all the files first then click the app in the sidebar. if that’s the way you’d like it to work … open TruncateFilenames in your script editor, delete the ‘end open’ line and replace ‘on open mgItems’ with this :

set mgItems to selection of application "Finder"

or, you could click the app in the sidebar first, which would then prompt you to choose the files. if that’s the way you’d like it to work … open TruncateFilenames in your script editor, delete the ‘end open’ line and replace ‘on open mgItems’ with this :

set mgItems to choose file with multiple selections allowed

but surely you’d have to agree that saving it as a droplet is probably the best solution in this case (?).

• related post : renaming finder items II : renaming by list.
• related post : renaming finder items III : renaming by creation date.

macgrunt icon

mounting servers

applescript is a great way to automate repetitive and tedious tasks. you can find an applescript solution for connecting to smb (or afp) servers here. however, if connecting to servers is the only thing you want the workflow to do, it’s just as easy, if not easier, to use automator. here’s one way.

first open the automator application and drag these two actions into the workspace : get specified finder items and open finder items (it’s easy to find an automator action by typing just a few letters into the search field).

next, create aliases of your servers in an appropriate place (you can usually do this by simply dragging the volume into a folder — you may need to hold down command-option as you click and drag). then drag the aliases into the get specified finder items window in your automator workflow.

save it as an application and you’re done. then you just need to decide where you’re going to launch it from. the dock is handy for this one.

mount servers at login …
if you prefer to automatically mount the servers at startup, you can add the app to your login items (system preferences > accounts > login items — click the ‘+’ button and navigate to where you saved the app). NOTE: if you are going to launch at startup, you may need to add a pause to the beginning of the workflow to allow other services to finish starting up first. if your mac tries to connect to the servers too soon, you’ll get an error.

macgrunt icon