[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

I’m very newbie when it comes to AS so I have no idea if/how this can be done…

UPDATE: I’m using Keyboard Maestro and there’s a token that does this, but for some reason it’s causing the app to lag to a point where it’s impossible to work (maybe a bug?)

Then I found this topic where it uses AS, but again, my knowledge in AS is limited so I don’t know if this would be an option? If this works, I can then use KM to run the AS script and save the result to a KM variable (so I won’t need the token anymore):

Can anyone help me with this?
If I’m saving the result to a KM variable, do I need this block?

tell application "Finder" to set containingFolder to container ¬
    of documentPath as alias

Thank you. That indeed works with TextEdit, but with Photoshop I get this:

The Keyboard Maestro token is able to get the path, so I’m assuming that it’s possible via AS. Weird that it works for TextEdit, but not Photoshop…

I don’t know if this helps, but I’m using UI Browser and I get this:
image

It’s weird that the app’s name in the Applications folder is “Adobe Photoshop 2020” and when I try to run the script it automatically changes “Photoshop” to “Adobe Photoshop 2020”, but in UI Browser it shows as “Photoshop”

Any idea how to fix this?


By the way, instead of asking this later on, in case you figure it out, how can I then save that path as a variable so I can use it with Keyboard Maestro?
I just need to know how to convert the path to a variable. The Keyboard Maestro part I know how to convert the AS variable to a KM variable.

Would it be something like this?

tell application "TextEdit"
	tell front document
		set myPath to its path
	end tell
end tell

My issue is not how to get the path back into KM.
My issue, since I’m new to AS, is how to set a variable as the document’s path (or in the case of the clipboard, how to set the clipboard as the document’s path)?

Is the script I shared the right one? Using set myPath to its path?
Is that how you would set the variable based on the path?

Do you know where I can find this information? I’m still not familiar with what dictionaries in apps are and what they do (or how to access them)

According to the linked Apple discussions article it’s file path not just path and the result is an AppleScript alias. And as far as I can remember Photoshop has a current document property.

tell application "Adobe Photoshop CS5.1" -- replace this with the proper Photoshop version
      set thefile to (file path of current document) --> get the file path (an alias)
end tell
set thefile to thefile as text
1 Like

Thanks! That’s working and closer to what I would like it to do.
When I run this script, I get the path as
Macintosh HD:Users:tiago:My Files:Inbox Global:2024-01-13:SCR-20231230-jgoj.png

How can I make it the “normal” path like
/Users/tiago/My Files/Inbox Global/2024-01-13/SCR-20231230-jgoj.png
?

I can use KM to convert it, but if AS can do that automatically, even better.
Really appreciate your time and help!

Just replace

set thefile to thefile as text

with

set thefile to POSIX path of thefile
1 Like

If I understand you well, you want to save a flatten version of the front doc in Photoshop, while keeping the original intact.
If it’s ok for you to use AppleScriptObjC, here is a simple solution to achieve it:

use framework "Foundation"
use scripting additions

tell application id "8BIM"
	activate
	
	set thePath to POSIX path of (get file path of current document)
	set theURL to current application's NSURL's fileURLWithPath:thePath
	set theString to current application's NSString's stringWithString:thePath
	set theDest to ((theString's stringByDeletingPathExtension()'s stringByAppendingString:" -flat")) as string
	
	duplicate current document
	flatten current document
	save current document in theDest appending lowercase extension
	
end tell

No, it’s the opposite.
Let’s say I’m working on a file with layers. Sometimes I have to flatten the image temporarily, but that’s not the state I want to save. So while the image is flatten, if I accidentally hit save and close the project, now I have a flatten image that I can’t manipulate anymore.

So what I’m building is a macro in Keyboard Maestro that when I hit the Save shortcut, it first finds the file’s path and creates a copy to another location, and because the saved version is still not flatten, I get a version with layers as a backup. After copying, it then saves the new file in place. So even if I accidentally save and close that flatten image, it saved a non-flatten copy to a different location.

The simple script shared by @StefanK does the trick. Everything else is done inside Keyboard Maestro. I have all of that set up already. I just needed to find a way to get the file’s path. And because I also need to add the current date and stuff like that, doing that in KM is easier and faster, having AS just for the file’s path.

Thanks though for sharing your script :slight_smile:

Great! Works like a charm! :raised_hands:
I still need to fully understand that whole POSIX thing, when and how to use it.
I tried to read about it, but then when I look at some scripts, sometimes they use it, sometimes they don’t; sometimes it’s before a certain part of the script, sometimes at the end, sometimes it includes “alias” sometimes it doesn’t…

Thank you so much for your time and help.

In addition to PDF link provided by @Fredrik71, just drop Photoshop on Script Editor (or File > Open Dictionary in Script Editor, select Photoshop).