Get file path easier on a mac with applescript | Copy Path script droplet

An issue I’ve had with managing multiple files and versions of files and different servers and emailing files around to different people throughout a process… you get the idea.

I looked for a way to get the path to a file like a url online. that would be easy for anyone to navigate to, while not having to type it manually. I know it’s a small thing, but these small things add up…

So first I was surprised that mac os x doesn’t do this out of the box. I’m pretty sure it was easy enough to do in windows. Although when you control-click and “Get Info” the location of that file is displayed in the window, you can’t select it. So if you really want to type it this is your solution. I found it really annoying that you couldn’t select the text and copy it.

Then I read somewhere that you could open a terminal window and drop a file into it, then the path to the file would be printed in the terminal. Awesome, but not very useful for me, it is rare that I have a terminal window open and the process is a bit lengthy since my main drive for this is to save time (and typos) from having to type the path myself of take a screen shot of the path in either the finder or the get info window. I thought about venturing into apple script, a realm I’ve heard a bit about but never ventured alone.

After some initial tuts and intros to applescript it seems like a very feasible task. I want to either set up a shortcut so I can select the file and hit a couple keys and have the path copied to my clipboard or have a droplet in my dock to drop a file into which will copy the file’s path to my clipboard and then I can easily paste the file path into an email in a matter of seconds and trust that they will be able to navigate to the correct file.

Thanks to this great forum post I was able to find what I needed!

droplet in dock
I created a droplet with applescript to copy a files path to my clipboard (code below) and then put it into my dock for easy access! Create your own “Copy Path” droplet with this code or download mine here

[cc lang=”applescript”]
I use this script to show people where to find files on our LAN.
Just drop a few files/folders onto this droplet and their pathes will be copied and put on the clipboard.

on open these_items
set myItems to itemSort(these_items) — v1.0.5 ASCIIsort failed on older versions of applescript. — CBT; 10/5/99

set path_str to “” as string

repeat with i from 1 to the count of myItems
set this_item to item i of myItems
set posix_path to POSIX path of this_item

— For now I’m stripping the preceding ‘/Volumes’ string for external
— drives so I don’t clutter my build logs.
set shortened_path to do shell script ”
echo \”” & posix_path & “\” | sed ‘s|/Volumes||’

set path_str to path_str & shortened_path & return
–set path_str to path_str & posix_path & return
end repeat

— Copy plain text to the clipboard.
set the clipboard to «class ktxt» of ((the path_str as text) as record)
end open

on itemSort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to “”
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list
if the low_item as text is “” then
set the low_item to this_item
set the low_item_index to i
else if this_item as text comes before the low_item as text then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end itemSort

context menu ui nightmareAlso I was able to use automator to create a context menu item – although I prefer the droplet since I don’t have to fool with the accuracy required to navigate through nested context menu items. I’m still yet to create a shortcut that will make it the fastest, but this is a big step in the right direction. It’s one thing I love about computers, and I guess life in general, where there’s a will there’s usually a way (especially when forums are involved). Figured I’d post this for posterity and in case any of you are wishing your mac could do this too.