11

I have a custom Service that I've created in Automater (I'm on OS X 10.6.5). The Service executes an AppleScript which in turn executes a shell script. Here is the AppleScript:

on run {input, parameters}
    -- do shell script "/usr/bin/find " & input & " -type f -name .DS_Store -delete -print"
    display alert "Path: " & input
end run

The problem is, the path passed by the Service (in the variable input) looks like:

Macintosh HD:Users:Matthew:Documents:Programming:Apple Scripts:

And for the command to execute properly, the path needs to look like:

Macintosh\ HD/Users/Matthew/Documents/Programming/Apple\ Scripts/

So, is there a way to translate the first path to the second path? I was hoping for a core function which handles this. But I suppose some kind of regex/find/replace/etc would work? I'm familiar with HTML/CSS/Javascript/PHP, not with Cocoa/AppleScript/C. Any direction or suggestions would be great!

0

2 Answers 2

9

Use quoted form of the POSIX path of input. Without the quoted form of part, it won't handle funny characters (like spaces) in filenames correctly. See this Apple technote.

0
6

Ok...this was easier than I had realized. After some Googling, it looks like what I need here is the POSIX path of command. This converts a given path to a Unix style path, which is the type of path you would see/use in Terminal. So:

POSIX path of input

Gives me a path like:

/Users/Matthew/Documents/Programming/Apple Scripts/

It seems to take into account that you just need a / at the beginning of the path, rather than the hard drive name. It does not, however, escape \ spaces (or handle special characters) in the path. So, as the other answer here mentions, you need to place the path in quotes ( " or ' ) in order for those spaces (or special characters) to be interpreted literally. You accomplish this by using quoted form of. Here it is:

quoted form of the POSIX path of the input
-- gives a path like: '/Users/Matthew/Documents/Programming/Apple Scripts/'
1
  • 2
    The / at the beginning of the path is the boot volume. Other hard drives are under /Volumes/
    – Josh
    Commented Dec 14, 2010 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .