Mar 28 2008
Copy Finder Path to Clipboard - Tip 2
Yesterday I wrote a short AppleScript application that copied the current path of the Finder window to the clipboard. A rather unassuming, yet handy application. One downside to this application is that if you want the path of a folder selected in Finder, you would have to double-click on the folder to change the Finder path to that folder location, and then you could engage the AppleSscript application to copy the path.
For example, in the figure below clicking the Script Editor icon that we created in yesterday’s post (to the right of the terminal window with green arrow) will copy the path /Applications/ to the clipboard. However, what if you intention was to copy the path of the highlighted folder, that is, /Applications/Utilities/Java/ ?

I’ve reworked the original example to add support for dragging a folder onto the icon and also moved the code for displaying a dialog box when an error occurs into a subroutine. Take a look at updated application below:
Click here to paste the code below into the Script Editor
-- When clicking on the icon
try
tell application "Finder"
set currentPath to (POSIX path of (target of front window as alias))
set the clipboard to currentPath
end tell
on error
my finderErrorMsg()
end try
-- When dropping a folder onto the icon
on open {droppedFolder}
try
tell application "Finder"
set the currentPath to (POSIX path of droppedFolder as text)
set the clipboard to currentPath
end tell
on error
my finderErrorMsg
end try
end open
-- Error msg when unable to copy a path to the clipboard
on finderErrorMsg()
display dialog "Unable to copy a path to the clipboard.
Make sure Finder is referencing a directory/folder
within the file system."
buttons {"Ok"} with icon caution with title "Error"
end finderErrorMsg
With the addition of the on open reference (line 12) you can now drag/drop a folder in Finder onto the application icon and its path will be copied to the clipboard. Before you can use the script, you will need to save the AppleScript code as an application and drag the script onto the toolbar (remove any previous version of the program by right clicking and selecting ‘Remove Item’).
As a quick debugging tip, you can add a dialog box as shown below to display the path that was copied into the clipboard:
tell application "Finder"
set currentPath to (POSIX path of (target of front window as text))
set the clipboard to currentPath
display dialog currentPath buttons {"ok"}
end tell
...
Now you can view the clipboard contents (through the dialog box) which makes the debugging process a little easier should you decide to tweak this application.

Another good example of the power of scripting on a Mac.


This application works quietly in the background, saving each text copy into an internal buffer. When you want to paste information, click the icon on the menu bar and you’ll see a list of recently copied text, choose the one you like and you’re good to go…
Tips by RSS
Tips by Email
