Capture Path in a MacOS "Save As" or "Open" Dialog

Does anyone here know a way to programatically get the Finder path from an open Save As or Open dialog?

In other words if I am saving a file and I navigate to the place I want to save the file, before clicking Save, I want to capture the path to where I am in a Keyboard Maestro variable so that I can navigate back here in Finder later using that variable.

I have found that when in a Save As dialog box, a right click context menu can "Open in Finder" and from there I can get the path. But I haven't found a way to automate this.

Are you talking about the KM actions for files (I don't think so), or are you talking about macOS dialog boxes for opening/saving files (I think so), and if so, which apps? It's likely that most apps are using file selection windows provided through APIs by macOS, so perhaps in theory macOS can provide this information to other processes, but I can't see why macOS would bother.

However, some apps have the full path of the current folder displayed in the bottom line of the window (I saw it happen earlier today.) So I would consider using OCR to read that line to get the location.

Even so, how would you know that this window is even open? How would you know when to check if the window is open? Are you going to be polling the screen all day long? I don't understand how you would know when to "fetch" the data from these windows.

If the Save/Open boxes are generated by Keyboard Maestro, then there's hope. You could write your own Save/Open boxes using a Custom HTML Prompt window and then you could save any or all paths.

Now that I've googled it, I found that if a user holds the OPTION modifier down while those sorts of windows are open, the full path of the selected folder name appears near the bottom of the window. I never knew that. There's a chance that you could use this trick to start a macro that uses OCR to read the path from the screen. Maybe that's not what you want, but it would seem like a possible solution.

1 Like

Thanks @Airy

Yes. I've altered the title of this Topic to make that clearer.

I would press a hot key (i.e. I would have to be thinking ahead that I might want to save this path)

That's good. So, MacOS has the path somewhere :smiling_face:︎ I just need to work out how to extract it... I wonder if AppleScript can do it?

At least back here on 10.14 Mojave, Quick Actions shown when right clicking are available in Open/Save dialogs, so that makes me wonder if a Quick Action/Service could be made with AppleScript to get the path displayed in a dialog box.

1 Like

Ah! I've got it!

Pressing ⇧⌘G when in the dialog brings up the current path and a simple "copy" copies that path to the clipboard. And of course that can be automated in Keyboard Maestro to make a simple Macro with hot key. I'll leave this post up in case this is useful to anyone else... it had me stumped :flushed_face:

EDIT - apologies. This does not work. It just brings up the latest folder navigated to before. It does not reflect the folder navigated to in the dialog... :flushed_face:

1 Like

I'm glad you found a solution (I did not know about that shortcut key) and I'm glad you came up with a trigger idea.

1 Like

All (or nearly all) of the keyboard shortcuts shown in the Go menu of the Finder are also available in Open/Save dialogs.

EDIT - apologies. Pressing ⇧⌘G does not bring up the current path in the dialog. It just brings up the latest folder navigated to before. It does not reflect the folder navigated to in the dialog... :flushed_face:

Okay. After a bit more experimenting, this seems to work:

When in the Open or Save As MacOS dialog, pressing this combination of keys will copy the current path to the Clipboard:

Copy Path in MacOS Dialog v02.kmmacros (3.4 KB)

6 Likes

Hello Aurio (@Zabobon) :waving_hand:

I encourage you to use either the Execute AppleScript or JXA Action and take a look on the Standard Addition‘s choose File and choose Folder Commands.

Going this route you’ll be able to save either a single File/Folder or even multiple Files/Folders into a KM Variable.

I think this might be the reason why Peter hasn’t made a decision to integrate this into the standard actions. If you want more functions in these dialogs - there is a way to script it using AppleScript or JXA.

Greetings from Germany :germany:

Tobias

1 Like

Does command option C copy the path?

1 Like

No. That works in Finder but not in an Open/Save As dialog. It would be nice if it did.

1 Like

That’s neat. The . shortcut there was new to me. What a useful function that is, and it has a sensible choice of key, too… I guess it must have been around from the old days and has just not yet been removed… Oh, I have become so cynical. :wink:

Any fellow users of Default Folder X should bear in mind that they can set a shortcut for “Copy folder path to clipboard”, so a macro would not be needed, unless of course copying the path were part of a larger workflow.

1 Like

Hardly -- . is Unix for "the current working directory", so the dialog is evaluating that for you.

@Zabobon -- if you're a Default Folder X user you could use AppleScript and the GetCurrentFolder verb, which will "get the folder shown in an already-open file dialog".

Thanks @Nige_S

No I don’t use Default Folder X. Just regular Mac.

Do you know if there is any AppleScript that could do what I am trying to do in regular MacOS “Save As” or “Open dialogs”?

Short of using System Events to click the pop-up, gathering all the menu item names, then reconstructing the path from those, e.g.

{"01-css-methods", "foundations", "css-exercises", "repos", "Programming", "Documents", "nigel", "Users", "Macintosh HD", "Nigel's MacBookAir", missing value, "Recent Places", "For Forum", "tmp", "KM Sync", "01-css-methods", "VirtualBox VMs", "02-margin-and-padding-2", "Test ", "Templates", "Mail Export Test", "Preferences"}

for

/Users/nigel/Documents/Programming/repos/css-exercises/foundations/01-css-methods

...I don't know of anything.

Your . method is much cleaner!

1 Like

I know what . means in Unix; that’s why I said . was a sensible choice of key. However I don’t think it follows that . would necessarily work in the “Go to folder…” function of the Finder and the file selector.

To take another Unix example, .. will not show the folder outside the current folder within “Go to folder…”, which suggests to me that elements such as . and / (for top level) were specifically implemented rather than being a natural consequence of having the underlying Unix layer. I could certainly be mistaken on this point, but that was the line of thought that lead me to regard . as being something that Apple had added.

Two references already to that application might be enough, but in case it’s of interest to you or anyone else: other relevant options that DFX offers include an “open in Finder” shortcut from within the file selector and an “add menu to Finder toolbars” feature that provdes access from the Finder to folders that have been recently accessed from the file selector.

1 Like

I'll admit I'd never tried that (after all, that's what ⌘↑ is for :wink: ), but ~ does work.

But I don't think "has just not yet been removed" is correct -- ., / and ~ have worked in "Go to Folder" since it was introduced and it's the continued absence of .. that's odd.

And since it's such an obvious omission I can only assume that there's good reason for it.

Fair enough; either way, it was simply an excuse to poke fun at Apple for sometimes removing functionality from MacOS. I don’t actually expect the feature to disappear!

More striking than a mere omission of implementation would be if such shortcuts were indeed inherited from the Unix layer; then the absence of .. would, I think, suggest that Apple had blocked it, and that would suggest that there had been a vital reason to do so. Anyway, that’s enough conjecture from me on the point!