Mac Finder - Is There a Way to Capture the File Path of the Currently Highlighted File?

In the Mac Finder - Manually you can control click on a particular file – And then hold the option key down in addition and it gives you the ability to copy "xx" as pathname.

Using keyboard Maestro, is there a way to capture the file path of the current highlighted file?

I personally use a single KM macro with an βŒ₯⌘M keystroke to copy a [label](link) MD link from the front application.

The macro delegates to specialised sub-macros for some apps, and to a generic macro for others.

If you look in the com.apple.finder sub-macro here:

MD link tools Macros.kmmacros (192.7 KB)

you will see it:

  • clearing the clipboard
  • and then copying a link to the clipboard using an Execute JS action.

Others will be able to show you an AppleScript route.

An example AppleScript for you to experiment with:

tell application "Finder" to set the varName to (name of (selection as alias ))

tell application "Finder" to set the varPath to (POSIX path of (selection as alias ))

set the clipboard to "- " & varName & " β†’ " & varPath as string

2 Likes

Yes, and you don't need a KM Macro. Just use the Finder Shortcut ⌘βŒ₯C

3 Likes

Thanks for these great replies. I will check them all out.

hi @ComplexPoint

i downloaded and tried using your macros but i always get a JS error in any of the macros i run

image

Are there additional configuration steps I need to setup before this works?

thx

Z

Execute JavaScript for Automation actions can, of course, be run in two modes:

  • from JS source pasted as text into the action
  • from a path to a script file

That error message suggests that:

  • you are using the script file option,
  • and no file can be found at the file path supplied.

Perhaps try:

  • using the Execute text script option,
  • and pasting the JS source code into the action ?

thx @ComplexPoint!

im using your macro you posted so it’s a text script

anything else i should test?

Z

Ah, I see the problem.

That's not an autonomous macro - that's a sub-macro (hence the odd name, and the absence of any trigger) called as a library resource by:

Copy front document as Markdown link

If you select a contact in macOS Contacts.app, and run Copy front document as Markdown link (default trigger on my system is βŒ₯⌘M) then it should copy a url for you.

(behind the scenes it will draw on the com.appleAddressBook sub-macro, which only functions in the context of main macro)

Similarly, in Finder, select a file or folder, and run Copy front document as Markdown link, and it should copy an MD link to the file (making use of the com.apple.finder function in the process).

All of those functions need to be together in the same macro group, with the name MD link tools.

  • If there are any that you will never need, you can prune them out.
  • If any are missing (the main macro reports that a special module would be needed), then I may be able to add one.

thx a lot!

@dealtek and @zeltak:

As I stated above, if all you want is the POSIX Path of the file:

If you want a MarkDown link that will open the file, then this very simple AppleScript (which is easy to read and modify) will do the job:

tell application "Finder"
  set {oFile} to selection
  tell oFile
    set fileName to its name
    set fileURL to its URL
  end tell
end tell

set mdLink to "[" & fileName & "](" & fileURL & ")"
return mdLink

Here is a Macro that uses that script:

Example Output

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Get Markdown Link for File Selected in Finder [Example]

-~~~ VER: 1.0    2020-11-06 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Get Markdown Link for File Selected in Finder [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


1 Like

thx @JMichaelTX

Z

You can also do it in KM Action without programming.

1 Like

@ JMichaelTX. Thank you. I used to keep up with Apple shortcuts. But not so much anymore. A lot easy than the other way which I've been using.

No doubt, there are loads of keyboard shortcuts, more than anyone can be expected to remember. Perhaps worth knowing, however, is that the shortcut JMichaelTX shared is displayed when holding the Option key down while selecting the Edit menu in the Finder. The broader value in that tidbit is that any of the added shortcuts available by holding Option down when selecting a menu are visible this way, in any application.

So many great suggestions - Thanks so much!