Apple Script Square canvas size Photoshop CS2

I am trying to develop a script to detect larger either the hight or width of the canvas size. Then replace either the h or w with the larger of the two to make the image square. Am I on the right track? Any help would be great.


tell application "Adobe Photoshop CS2"
	activate
	set w to width of current document
	set h to height of current document
	if h = w then
	end if
	if w < h then
		resize canvas current document width (h) anchor position middle center
	end if
	
	if h < w then
		resize canvas current document height (w) anchor position middle center
	end if
end tell

Thanks

Try this. I am not able to test it right now because my Photoshop acting up.


tell application "Adobe Photoshop CS2"
	activate
	open newPath
	set docheight to height of document 1
	set docWidth to width of document 1
	set display dialogs to never
	set thisdoc to current document
	tell thisdoc
		if docheight > docWidth then
			resize image width pixels docheight resample method bicubic
		else
			resize image height pixels docWidth resample method bicubic
		end if
	end tell
end tell

Ok, got Photoshop going again.

See if this works for you.


tell application "Adobe Photoshop CS3"
	activate
	set docHeight to height of document 1
	set docWidth to width of document 1
	set display dialogs to never
	set thisdoc to current document
	tell thisdoc
		if docHeight > docWidth then
			resize canvas width docHeight anchor position middle center
		else
			resize canvas height docWidth anchor position middle center
		end if
	end tell
end tell

thanks craig! Fast response! I will try this as soon as I get to work tomorrow and let you know how it works out.

perfect!!!:slight_smile: thanks alot. This site is awesome!

Your welcome! and Yes it is!

MacScripter has the most helpful, friendly group of people I’ve found.

Hi Craig, I was wondering if you could help me.

I got your code seen here and I put it on to Automator under “Run Applescript”. I’m getting an error saying “can’t get document 1”. What am I doing wrong?? Here is the link with the error I’m getting http://www.chris-aranha.co.uk/question-about-automator

I need the same thing as robotflojo stated above. However I need to automate this to over 2000 images.

Any help would be greatly appreciated.

Chris

What exactly do you need to accomplish?

Hi Craig, I have a bunch of pictures of different sizes and I need them all to be of square aspect ratio without distorting them. For example, I may have a picture that has the following dimensions (200px by 320px), I would like the picture to be a square, therefore I would need to increase the canvas size so that the picture now is (320px by 320px). All my pictures are png files with transparent background. I’m assuming that any new canvas created by Photoshop CS3 would be transparent as well.

Here is a link with a graphical representation of what I mean http://www.chris-aranha.co.uk/square-images

robotflojo wrote the following a while ago which I believe would solve my problem, the problem is that I’m new to automator and applescript:

"I am trying to develop a script to detect larger either the hight or width of the canvas size. Then replace either the h or w with the larger of the two to make the image square. Am I on the right track? Any help would be great.

tell application “Adobe Photoshop CS2”
activate
set w to width of current document
set h to height of current document
if h = w then
end if
if w < h then
resize canvas current document width (h) anchor position middle center
end if

if h < w then
resize canvas current document height (w) anchor position middle center
end if
end tell "

The important thing is the aspect ratio (square) and not necessarily the pixel size of the image.
What do you think? Is this possible?
By the way, thanks for the fast response.

Regards
Chris

I believe this will do what you are looking for. Run a test on some sample images before turning it loose on the actual images. You will need to create a second folder to contain the processed images. The originals will not be overwritten.

Example Usage from AppleScript:
NOTE: Change paths to correct POSIX paths for your situation


    set imageFolder to "/Users/user_name/Desktop/PNG/"
    set outFolder to "/Users/user_name/Desktop/PNG/_resized/"

    do shell script "/usr/bin/ruby ~/desktop/square_png.rb" & space & imageFolder & space & outFolder