Search code examples
automationapplescriptphotoshoplayer

Create a new PSD file and copy/paste new layer with AppleScript


I try to make a code assembly to create my own applescript to create an image in Photoshop but unfortunately I have an error and I can't find where it comes from.

The first part of the script works, the random image is well copied to the clipboard but the rest is problematic.

-- Select random image
tell application "Finder"
    set bg_img to file (random number from 1 to (count files of folder "HD:Works:Club:LAYERS:0-BG")) of folder "HD:Works:Club:LAYERS:0-BG"
end tell

tell application "Preview"
    activate
    open bg_img
end tell

-- Select and copy image to clipboard
tell application "System Events"
    tell process "Preview"
        keystroke "a" using command down
        keystroke "c" using command down
    end tell
end tell

-- Quit Preview Application
tell application "Preview" to quit



tell application "Adobe Photoshop 2022"

-- Create a new document
set docRef to make new document with properties {width:1200, height:1200, resolution:72}

tell docRef
-- Unlock the background layer and fill it with gray color
set background layer of layer 1 of docRef to false
fill selection with contents {class:RGB color, red:200, green:200, blue:200}
end tell
end tell

-- Paste image to new layer 
tell application "System Events"
    tell process "Adobe Photoshop 2022"
        set newLayer to make art layer with properties {name:"LayerA"}
        keystroke "v" using command down
    end tell
end tell

Solution

  • Ok, a compilation error; I got it now. Try this:

    -- Paste image to new layer 
    tell application "Adobe Photoshop 2022"
        activate
        set newLayer to make art layer with properties {name:"LayerA"}
    end tell
    
    tell application "System Events"
        tell process "Adobe Photoshop 2022"
            keystroke "v" using command down
        end tell
    end tell
    

    Remember, you use a tell application "NAME" … end tell block to 1. tell AppleScript which app to send commands to, and 2. the app from which to get the custom terminology (keywords) for commands and objects.

    Thus your tell application "System Events"…end tell block uses SE terminology and commands. The tell process "Photoshop"…end tell block inside it is still addressed to System Events. process is an SE-defined keyword for an SE-specific object. Likewise keystroke is an SE-specific command.

    To make a new art layer in Photoshop, you have to put that command inside its own tell application block which is targeted at PS.

    Yeah, AppleScript is often confusing. And System Events is 10× more confusing than that, thanks to it being not-obvious what is GUI Scripting and what isn’t. Good luck.

    --

    p.s. I’ve a feeling the tell process block is totally redundant, and is only necessary when using GUI Scripting to manipulate an app’s windows. IIRC the keystroke command doesn’t know or care what app receives those keys, in which case what’s important is that you activate Photoshop before you start “pressing” keys. I’ll leave it to you to determine if you can discard the tell process.

    p.p.s. AppleScriptable apps usually don’t include cut and paste commands, usually ’cos they already provide far better ways of getting and setting content. Photoshop, however, does. So if you’ve not already tried it, try inserting a paste command at the end of your “tell Photoshop” block, then comment out the “tell System Events” block, and see if that works for you. If so, you can get rid of that System Events block entirely!

    p.p.p.s. It’s also worth investigating PS’s dictionary further, to see if it can open and place your “bg_file” directly. If so, you can eliminate the “tell Preview” and the rest of that System Events horridness too!!