Get the path for a file in OS X with an AppleScript

I frequently need to type in references to file locations.  In OS X if I Get Info on a file or folder I can get a path, but I decided to write a script to widen my options and make things a little faster.

The script allows me to choose whether to get the path as a Mac path (: based) or a Posix path (\ based).  It automatically copies it to the clipboard and gives me a notification.  The notification in this case is just a dialog box, but if you use Growl, you could change this to register and display an appropriate notification.

The script will pick out any selected files or folders in Finder and copy & display them in the specified format.

    -- The user gets to choose which type of file path to return (default is Posix)
    set mode to (choose from list {"Mac", "Posix"} with title "File Path Type" with prompt "Choose the type of file path to retrieve:" default items {"Posix"}) as text
    --If the user doesn't cancel then get the path
    if mode is not equal to "false" then
        --Get the paths of all selected files
        set strFilePath to {}
        tell application "Finder"
            repeat with objItem in (get selection)
                if mode = "Posix" then
                    set end of strFilePath to POSIX path of (objItem as text)
                    set end of strFilePath to objItem as text
                end if
            end repeat
        end tell
        --Copy the file paths to the clipboard
        set {strDelimeter, text item delimiters} to {text item delimiters, return}
        set the clipboard to strFilePath as text
        set text item delimiters to strDelimeter
        --Display a dialog for a couple of seconds that lists the file paths
        display dialog strFilePath as text with title "Path(s) copied to clipboard" with icon note giving up after 2 buttons {"Close"} default button "Close"
    end if
end try
Author: Stephen Millard
Tags: | applescript |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read