[SOLVED] Find opened Photoshop file

Is it possible to identify which file is opened in Photoshop and grab its Finder location?

Context: Photoshop doesn't allow Undo after a file is closed, which can sometimes be a problem when I flatten all layers for a specific reason and by accident Save and Close the image (I won't be able to edit the layers anymore).

So my goal was to have KM find the file in Finder when I hit the Save shortcut (so it won't save it right away), it would create a copy of that file to a specific location (backups folder) and only then it would save it. So even if I accidentally flatten layers, save, close, the original file will always be copied to my backups folder.

When I right click the tab of the open image, I can see the Reveal in Finder, so I would assume that this would be done via AS?
image

If this can be done via AS, it would have to know which image is the front image, because it's possible to have more than 1 image open, just different tabs

Found this topic, but my AS knowledge is not that advanced yet:
https://discussions.apple.com/thread/4478122?sortBy=best

Try using the FrontDocumentPath token. token:FrontDocumentPath [Keyboard Maestro Wiki]

1 Like

Thank you.
I tried that, but for some weird reason (and this is the first time I'm experiencing this) once I add the token, the "beach ball" shows up, then goes away for like 2 seconds, then back, in a loop. Any idea what could cause this?

It only goes away when I delete the token, action, or macro (when I can do it in those 2 second window)

I just did this
image

First off, you might want to give your variable a valid name since Local__ doesn't qualify I don't think!

As for the beach ball thing - I have an idea but only @peternlewis could say definitively what's going on.

Yes I was going to give it a name, always do, but I started with adding the token first (I have a favorite action and by default it's already set to Local__ so I can then just type the name) and then I couldn't even type anything without it taking forever. For example I tried adding "window" (so Local__window) but it would type "w", then beach ball for like 5 seconds, then "i", beach ball for 5 seconds, etc...

Is it something I could test?

By the way, that token worked like a charm (when I was able to wait until it added the variable name). Thanks

One thing I noticed is that once I select another macro, it goes away.

I found this topic that uses AppleScript instead (in case I can't solve the issue with the token). Would this be an option if I then save the result to a KM variable?

This is to do with what I was wondering...

When you use a KM token, KM tries to evaluate it whether you're in the KM Editor or just running your macro. In your case you are in the editor in a macro that's got that token in it and therefore KM is trying to get the path of the document in the frontmost app - which is KM. I don't actually know if KM ever has a "document" so I was wondering if KM is struggling to find the document path resulting in the beach ball.

This may be a complete red herring and that's why I referred the discussion over to @peternlewis

(I can't do any testing myself as I'm away from my office right now.)

Thanks for clarifying. It makes sense what you said, even though it makes it impossible to work if that's the case.

I was thinking of a workaround (besides the AS version), but not sure if this is possible?
Could I set 2 variables and then "merge" them to create the token? Something like this, but when I run this, it shows me the actual token, not the path
image

Scrub what I said about that token and the KM Editor. This is what you get for that situation:

image

I'm surprised you didn't notice that yourself.

As for "constructing" the token - I'd stick with the real thing or head over to your AppleScript alternative if there's no answer forthcoming.

BTW - I've never had any problems with this token once it had been released.

In my case, it doesn't show that. It shows it's empty:
image

(it's actually on the image I shared on my first reply. I'm surprised you didn't notice that yourself :laughing: )

I ended up going with the AS solution, with the help of someone at the MacScripter forum. I would still like to understand why I have this issue, but you for example, don't.

So the final script is

tell application "Adobe Photoshop 2020"
	set thefile to (file path of current document) --> get the file path (an alias)
end tell
set thefile to POSIX path of thefile

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine" to setvariable "Local__file" instance kmInst to thefile

And thanks for sharing that token, anyway. Maybe I will be able to use it in the future when this issue isn't there on my computer.

2 Likes

The FrontDocumentPath token should work in general, but it relies on the application setting the AXDocument accessibility attribute of the window, and it is possible that Photoshop does not do that.

Note that this happens when the editor is at the front. If you switched to the target application, with the Keyboard Maestro editor window still visible, it will show the result.

I'll change the text for that case to read “Not Available for Editor Windows” as it is different to most cases that use the text “Not Available in Editor”.

1 Like

The issue is not with the token not working. It does, with Photoshop.
The issue now is that when a variable is set to %FrontDocumentPath%, KM lags like crazy to a point where it's impossible to work. Just to change the name of the variable by adding the word "window" it takes like 1 minute or more, because it shows the beach ball for like 5 seconds or more, then stops for 2 seconds, types the letter "w", then beach ball for 5 seconds or more, types the letter "i", etc, etc, etc.

To being able to use it, I had to remove the last % for KM to see it as normal text, not a token, and only when I was 100% sure that the macro was done, I added the last % again and quickly clicked another macro to avoid the beach ball again.

In the editor?

Do you have Keyboard Maestro 11.0.2? There was a performance issue with the token in the editor prior to 11.0.2.

1 Like

That was the issue. I updated to 11.0.2 and it's working now. No lag and I can see the text when KM is the front application (Not available in the editor).

2 questions:
1 - Is there a way to make KM check for updates regularly, other than creating a macro to click the menu to check updates? I don't think I got the notification that a new version was available. It would be good to have that setting in the Settings panel.

2 - I found this topic where you mention that holding shift will show the token, which is useful and I didn't know about it. But I think the search menu could have a few improvements:

  • Ability for us to search for the token itself, meaning that the list would be sorted alphabetically with the tokens. Right now when I open the search > ALL, the titles are sorted, but when I hit Shift, the tokens are not (of course), so it makes it hard to find by token. For example right now if I know that a particular token starts with %Front but I don't remember the rest, it's really hard (or impossible) to find it, because typing %Front in the search field, shows no results. For the %FrontDocumentPath% the title is not next to the list with the other "Front" tokens
    For example the same type of token is described as:
    image
    Whereas the front document path is:
    image

So we would either have
The path of the front application
The path of the front window's document

or
Front Application Path
Front Document Path

At the same time, when I go to each category, the titles are not sorted, but the tokens are, which is confusing because it's the opposite of the All category
image
image

  • Ability to search for the token by typing % in the search field and it would automatically switch to the token list (sorted alphabetically), similar to when we hit Shift. Right now, nothing is listed:

Another suggestion would be a "token auto complete". For example if I'm creating a new variable and I type a certain combination of characters, for example %% (we could change this to whatever we want) it would trigger an auto completion window. So if I type %%front it would show me a list of tokens that have "front" in it (in any position), but in token form, not title. This would make the process so much faster.

Anyway, thanks for suggesting the updated. The token is working now.

I'm guessing you have this box checked in the Editor's General settings panel:

image

I get notified whenever a new update comes out, and I have that box unchecked. If you're not getting notifications even within the editor, though, then something else is going on.

-rob.

It's also unchecked

image

Very strange … @peternlewis may have to chime in on why you don't see notifications. I don't think it's related to your Notifications settings for Keyboard Maestro, though. But I do know I see notifications when there's an update available.

-rob.

1 Like

For now I set a monthly macro to check it, just in case. But yes, I would like to get them without relying on the macro.

Keyboard Maestro checks for updates once per week normally, so I don't understand why you aren't seeing them unless you have some sort of firewall (Little Snitch for example) blocking the checks.

The Shift key just changes the name of the token, so the order remains unchanged.

Use Insert Token by Name if you want to do more searching.

Token Auto-completion is already supported with F5.

No firewall. Any other app I have shows me notifications when there's a new update.
I will stick to the monthly macro to check this.