Copy Path of Finder Selection

I've used a Services extension to copy the path of the Finder selection for eons, so I never noticed it is not installed by default in OS X. I received a recent support question asking for this, so here is the resulting macro.

Select a file in the Finder, and then select Copy Path from the Keyboard Maestro status menu and the selection's POSIX path is copied tot he clipboard. If you select more than one, then they are copies as multiple lines (if its only one, there are no return characters),

Copy Path.kmmacros (3.9 KB)

4 Likes

Thanks Peter, I too have been using this for a while, since I am a new convert to Keyboard Maestro this is the AppleScript that I had been triggering in the Finder with QuicKeys.

tell application "Finder"
  set theWin to window 1
  set thePath to (POSIX path of (target of theWin as alias))
  display dialog thePath buttons {"Clipboard", "OK"} default button 1
  if the button returned of the result is "Clipboard" then
    set the clipboard to thePath
  end if
end tell

Neat to see another method Keyboard Maestro can do, plus this includes the file itself and not just the folder the file exists in.

This will handle:

One or more items selected in the front window of the Finder (which can be the Desktop).

If there is no selection then the path of the front window if it exists.

If no windows are open then the path to the Desktop.

-ccs

-----------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2010/10/06 06:43
# dMod: 2015/04/27 18:38
# Appl: Finder
# Task: Get posix path of selected items or if none the front window and copy to clipboard.
# Libs: None
# Osax: None 
# Tags: @Applescript, @Finder, @Posix, @Path, @Selection
-----------------------------------------------------------------------

try
  
  tell application "Finder"
    set fileList to selection as alias list
    if length of fileList = 0 then
      set fileList to insertion location as alias as list
    end if
  end tell
  
  if length of fileList > 0 then
    repeat with ndx in fileList
      set ndx's contents to POSIX path of ndx
    end repeat
    set AppleScript's text item delimiters to linefeed
    set fileList to fileList as string
    set the clipboard to fileList
  end if
  
on error e number n
  stdErr(e, n, true, true) of me
end try

-----------------------------------------------------------------------
--» HANDLERS
-----------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
  set e to e & return & return & "Num: " & n
  if beepFlag = true then
    beep
  end if
  if ddFlag = true then
    tell application "Finder"
      activate
      set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
    end tell
    if button returned of dDlg = "Copy" then set the clipboard to e
  else
    return e
  end if
end stdErr
-----------------------------------------------------------------------
1 Like

Even better, thanks!

Or using Yosemite JXA Javascript, perhaps something like:

osascript -l JavaScript << JXA_END
	lstSeln = Application("Finder").selection();
	lstSeln.length ?
		decodeURI(lstSeln[0].url().slice(7)) : '';
JXA_END
1 Like

Or if you want multiple selections --> one path per line, perhaps:

osascript -l JavaScript << JXA_END
	Application("Finder").selection().map(function (f) {
		return decodeURI(f.url().slice(7))
	}).join('\n');
JXA_END
1 Like

Is there a KM method to test or determine when a Finder copy file action is completed?

Sorry, I realize this is an old thread, but I’d like to pause a macro until a single file copy (or multiple files copy) has completed.

You can detect the presence of the Copy window in the Finder (presuming you never have any windows open for folders named "Copy").

Or if you expect the file to take a long time, then you might want to reduce the load with something like this:

Note that the Finder must remain at the front during this operation as the condition does not allow you to specific the target application.

I use ⌥⌘C to copy a pathname. If no files are selected then it copies the path of the open window otherwise it copies the path of one or more selected files.

It shows in the Edit menu of Finder when the alt key is pressed.

I’m new to KM so apologies if this isn’t what you are trying to do.

2 Likes

Peter,

My apologies as I believe I've unintentionally misled you and Emyr! I've
not actually used the prior thread's approach to complete the file copy and
perhaps your responses were provided based upon those actions since the
responses don't quite meet my needs.

I'm simply testing the attached macro to eventually use it to copy movie
files (~ 1 GB) to the "Automatically Add Files To iTunes" library folder.
The problem is the window is still open after the copy completes. Maybe I
should try to use the actions in the original thread to complete my copy
along with your proposed solution or do you have an alternative suggestion?

Thanks for you support
Test Pause Until Copy Complete.kmmacros (5.85 KB)

Peter,

I now also realize that I am more accurately interested in pausing the
macro until the paste of the file is completed!

Peter,

Please disregard all my previous responses to your suggestion and forgive
all my confusion!!

Your “Pause While Window->Copy” suggestion works perfectly! I simply
needed to add a bit of delay (5 seconds) for the copy (i.e. paste action)
to begin before testing for the (Window->Copy enabled) condition.

Many thanks

1 Like

^ this! is exatly what I was looking for Thank you for that shortcut @Emyr!!