Photoshop applescript, create new document with size, BG color etc.

Hello.

So i got the hang of applescripting in keynote, mainly due to all the great post around the internet explaining it in detail.

Now want to do some scripting in photoshop, but i can’t really find anything good to go with.
I downloaded the Photoshop scripting manual, but it didn’t help me as of now, cause i’m stuck with the very first line of code.

tell application "Adobe Photoshop CC 2018"
	activate
	make new document
	set myFile to "Data:docsamples:testfiles:TiffSave.tif"
	set myOptions to ¬
		{class:TIFF save options, embed color profile:true, interleave channels:true, save alpha channels:true, transparency:true, image compression:none, save layers:true, save spot colors:true}
	tell current document
		resize canvas width 1920 height 1080 anchor position top center
	end tell
	
end tell

I wan’t to make a document, with a given Width X Height from my previous inputs in my applescript.
Right now, the only way do modify the canvas, that i have found, is to resize it. But in this way, i can’t get it to use pixels instead of cm.

The Question
is there a way to lets say, create new document with canvas size X,Y and background color black?

just so you know where it is going.
The script, when done should:
copy all shapes from a keynote document,
create new photoshop document,
paste keynote shapes to new document
export TIFF with black background color to folder A, with name B
export PNG with black background color to folder A, with name B+PNG (no compression)
delete background layer
export TIFF without background as alpha with transparency to folder A with name B+1
export PNG with black background color to folder A, with name B+PNG+1 (no compression +alpha)

Hi Markus

try this for doc size and black background

tell application "Adobe Photoshop CC 2018"
	set DocHeight to "1000" as number
	set DocWidth to "1000" as number
	make new document with properties {height:DocHeight as pixels, width:DocWidth as pixels}
	set background color to {class:RGB color, red:0, green:0, blue:0}
end tell
tell application "Adobe Photoshop CS6"
	set newDocWidth to 1920 as pixels
	set newDocHeight to 1080 as pixels
	set newDocBackgroundColor to {class:RGB color, red:0, green:0, blue:0}
	set initalBackgroundColor to the background color
	set the background color to newDocBackgroundColor
	set docRef to make new document with properties {initial fill:use background color, width:newDocWidth, height:newDocHeight}
	set background color to initalBackgroundColor
end tell

both of the posted scripts complain on my end, about not being able to convert the width / height to pixels?

Is it an issue with photoshop CC 2018?

FOUND IT.
This little snippet fixed it :slight_smile:

set ruler units of settings to pixel units

after some consideration, copying from keynote to photoshop applies anti-aliasing to the content, which is not what i want to happen, so the option now is to export from keynote as PDF (got that part working) and then import the PDF to photoshop and set the canvas size to the actual size from keynote (could be 1920x1080 or whatever i have it defined as in the script)

i thought this SHOULD work, but i can’t get it to work.(i got Photoshop 2019 now, but it didn’t fix the issue.

tell application "Adobe Photoshop CC 2019"
	activate
	set ruler units of settings to pixel units
	set myFilePath to alias ":Users:markusrasmussen:Desktop:Testfile.pdf"
	open myFilePath as PDF with options {class:PDF open options, bits per channel:eight, crop page:crop box, height:1080, width:1920, mode:RGB, resolution:72, use antialias:false, page:1, constrain proportions:true, suppress warnings:true}
	
	
	
end tell

EDIT:

a lot of trial and error, but adding myFilePath as string fixed it :slight_smile:


tell application "Adobe Photoshop CC 2019"
	activate
	set ruler units of settings to pixel units
	set myFilePath to alias ":Users:markusrasmussen:Desktop:Testfile.pdf"
	open alias (myFilePath as string) as PDF with options {class:PDF open options, bits per channel:eight, crop page:crop box, height:1080, width:1920, mode:RGB, resolution:72, use antialias:false, page:1, constrain proportions:true, suppress warnings:true}
	
	
	
end tell

You’re getting some odd behavior there.

My script runs fine here in CS6 and CC2018. This is generally the best way to handle units in scripts because it’s faster than changing unit settings, you don’t have to keep track of what your unit settings are, and leaving unit settings changed can upset users. When you don’t keep track of your units as raw numbers, but as Photoshop unit types, then Photoshop converts on the fly to keep things correct. So with the way I did it, setting the numbers to pixels, my Photoshop unit settings can be set to inches, centimeters, pixels, points, or whatever, and the script always creates the correct size document without having to change unit settings.

I’ve had scripts doing it that way running on about 30 macs on about every version of Photoshop since CS6, so it’s not just my machine, this usually works. Don’t why it’s not working for you.

On the file opening, you’ve found some pretty odd behavior on Photoshop’s part.
This works:

set myFilePath to "Hackintosh HD:Users:tspoon:Downloads:Test.psd"

tell application "Adobe Photoshop CC 2018"
	open alias myFilePath
end tell

As you’ve discovered, this doesn’t work:

set myFilePath to alias "Hackintosh HD:Users:tspoon:Downloads:Test.psd"

tell application "Adobe Photoshop CC 2018"
	open myFilePath
end tell

Which seems weird to me. But at least you discovered a work-around. I checked, and that odd behavior goes back to CS6, too. I wouldn’t expect that usage to be required based on Photoshop’s Dictionary. I’ll try to report it to Adobe as a bug.

If you are going to change the pixel units, it’s generally best practice to first set a variable to the existing pixel units, then change them, then when you’re done with the code that’s effected by them, change them back to what they were. Like I did with the background color in my script, or we all commonly do with Applescript’s Text Item Delimiters.

New question.

Is it possible by script, to make a selection with the Marquee tool at a fixed size, lets say 500 x 900 px, with the selection uppermost left corner starting at X,Y coordinate? all within applescript?

I found that i can do a lot of the stuff with actions i call upon from the script, but for this i need variables from the script to size the selection, which i haven’t found a way to do with actions… yet.

You can modify variables in actions by using Scripting Listener to record the Javascript AM code (Action Manager code) and then play it back in your script. Even when you don’t need to affect the variables, this is generally a preferable way to handle actions as opposed to actually telling Applescript to run an action, because the code is portable - it doesn’t rely on specific actions being on the machine.

However, you don’t need actions to make the selection. I’m surprised you’d need actions for any of what you said you need to do, it should all be accessible directly by Applescript.

set selOrigin to {0, 0}
set selHeight to 500
set selWidth to 900
tell application "Adobe Photoshop CC 2018"
	set currentDoc to the current document
	tell currentDoc
		set OX to item 1 of selOrigin
		set OY to item 2 of selOrigin
		select region {{OX, OY}, {OX, OY + selHeight}, {OX + selWidth, OY + selHeight}, {OX + selWidth, OY}}
	end tell
end tell