Search code examples
applescriptphotoshop-script

Applescript: Photoshop: How to find out whether the image is really open


We have an automation tool for Photoshop, using a control app, which calls Applescripts controlling Photoshop.

One situation is that we must open a RAW image in the CameraRAW plug-in, and then open it in Photoshop. This part is handled, via an applet using System Events. When that applet terminates, we run the processing script for Photoshop.

As some pictures take quite a bit of time to open, we have to make sure that the picture is really open before the script can run. … and that's where I am stuck.

At the moment, I am using the following code which is intended to wait until the image is open (the criterion for "open" is correct (and tested manually), so that's not the issue here).

    tell application "Adobe Photoshop CC 2015"
        activate

        tell application "System Events"
            tell application process "Photoshop CC"

                --
                display alert "Waiting for Window"
                --

                repeat
                    try
                        set wn to name of window 1 as text
                        try
                            if (wn contains "(RGB/16") then
                                set wn to "image is open: " & wn
                            end if
                        end try
                        if (wn contains "(RGB/16") then
                            display alert "We are there, quitting now…  " & wn
                            exit repeat
                        end if
                    end try
                    delay 1
                end repeat


            end tell
        end tell

        --
        display alert "Ready for process"
        --
-- and here comes the processing code

end tell

I also tried to set a variable which is tested as argument for repeat, and changed when the exit condition is fulfilled.

Trying to create even alerts within the repeat loop, does not lead to any effect; the script ends up in an infinite loop.

It is well possible that I miss the obvious… So, I am grateful for any helpful hint.

Thanks in advance.


Solution

  • I think you have a few small issues with your script that are causing your problem.

    1. You are using name of window 1 when I believe you need name of document 1. With your first try block structured as it was you weren't realizing it was actually giving an error on name of window 1
    2. The name that is returned doesn't contain the color space and bit count, so I've changed the result test to an empty string
    3. Notice the modifications to the try block around getting the document name
    4. I don't believe it's necessary or see a reason to use "System Events" in this case, so I've modified the version below without it.

    Example Script

         tell application "Adobe Photoshop CC 2015" 
            display alert "Waiting for Window"
            repeat
                try
                    set wn to name of document 1 as text
                on error
                    set wn to ""
                end try
    
                try
                    if wn is not equal to "" then
                        set wn to "image is open: " & wn
                    end if
                end try
                if wn is not equal to "" then
                    display alert "We are there, quitting now…  " & wn
                    exit repeat
                end if
                delay 1
            end repeat
    
            display alert "Ready for process"
    
        end tell