Capture a File Path

I searched and cannot find an answer to this. The nearest I came was: [File Paths Not Working, Why?](http://this post). That post was about writing out the file path apparently, not clicking on a file and creating the path with correct formatting (escaping the spaces).

I use an app regularly that has been having inconsistencies in dragged in file or folder paths. It may be fixed soon (or not). I am wondering how to set up a macro from either a right click or a keystroke where I can copy the file or folder path so that I can then go to a text editor and past the path so that it can be clicked on later?

If you’re talking about Finder, and El Capitan, you can right-click on a file, then hold down the Option key, and it gives you an option to Copy as Pathname.

Does that help, or do you need something more automated?

Yes, there is something like this in my Services menu. Truth is, Services menu is a real mess. I will look into paring it down when I get back (have to go now). If I can find that in services menu then my question would still be how to set that service to a KM macro I could execute in a single keystroke and not have to navigate sub-menus (which I really don’t do well)


hold down the Option key

This doesn’t escape spaces.

I was always using this little Workflow:

Copy Escaped (66.9 KB)

not have to navigate sub-menus

You can launch Service Workflows also from the context menu.

Services menu? In El Capitan, it's right there in the standard context menu.

Anyway, try this:

You can use this to get there from here. If you need help, let me know.

1 Like

I had just started to respond with the same point. Taking it to the next level, if you need both the name and the path of every item of the selection, this script will do it:

tell application "Finder"
	set theItems to {}
	repeat with theItem in (get selection)
		copy {name of theItem, URL of theItem} to end of theItems
	end repeat
	set theLinks to {}
	repeat with thePair in theItems
		copy ("<a href='" & ¬
			the second item of thePair & ¬
			"'>" & ¬
			the first item of thePair & ¬
			"</a>") ¬
			to the end of theLinks
	end repeat
end tell

What you get back is a list of HTML links. If you don't want the links, cut out the second repeat and return theItems, the first item of each will be the Finder name and the second the path (as a URL). Your macro would then have to continue the script to paste into your document.

How you paste the link or the name/URL pair into your document is, I think, going to depend on how your application pastes links. If it is scriptable, there may be a way to do that, but probably not. One common way applications paste links is to select text, press ⌘K, and paste the URL

Of course, you can do this all with standard KM actions also.

Or an automator workflow.

Well, yes, but this is the Keyboard Maestro forum, so…

If Automator is the best solution to the challenge posed by the OP, then mentioning Automator is appropriate. I don’t know that that is the best solution, since I haven’t tried it and others are easy enough to write.

In case you missed it, you can try the Automator Workflow from above.

Thanks. We were all posting at the same time. Your post wasn’t there when I made mine.

Not in my version of El Cap

To recap, thanks for all the fast responses. Yesterday got away from me and I am just back to this now. First, the developer released an update so the problem I was having no longer needs an independent solution as I was asking for here. It is still a good question and when I figure it out I will enter it into my own personal notebook of tips for the next time.

I looked in KM for any command that seemed relevant before I posted here. I didn’t see one.

Not interested in Automator, thanks. Not a fan of pure AppleScript solutions. Will use AS inside of a KM solution or where needed - IF needed but it is definitely not my first choice.

I learned a lot from Dan’s example. Thanks all for the fast response and the great support.

Oops, I spoke too soon, this does not convert a file path so that it may be clicked to launch the file. It does not convert/escape the spaces. Somewhere in this process there needs to be a replace spaces with a "/ " or some such.

Trying this but getting the variable is not defined.

1 Like

Make sure, that the Service is activated in System Preferences > Keyboard > Shortcuts > Services.

Edit: Also, the Context Menu is context-sensitive. So not all Services will always be there.

The AppleScript in your macro doesn’t make any sense to me, and/or parts of the macro seem to be missing.

Why don’t you try it with @Dan’s example from above?

You can do this by adding a Search/Replace action to the macro.

I did and it worked perfectly - except the result was not clickable. The solution was something I copied from an example on Stack Overflow. I may have misunderstood the example. It was given in context of solving this problem and I assumed the finder command was something built in to set a path to a correct POSIX value. Apparently not though.

Thanks, for some reason "restore defaults" puts in all sorts of what I consider to be non default junk, items from Graphic Converter, Devon Think and other apps that don't use very often.

Clearing the clutter except for the copyfilepath put the command on the option popup menu again. Thanks.

For “clearing the clutter” use the Services Manager app. Gives you a big window and some more options.

Once the context menu is up, hold down the Option key. It changes the menu form this:

to this:

Notice the "as Pathname" at the end.