Setting Canvas Size via AppleScript

Having tech problems? Post them here and do not worry, there is always a solution for you.
Post Reply
frankjance
Posts: 4
Joined: Mon Dec 31, 2018 6:54 pm
Country: Canada

Setting Canvas Size via AppleScript

Post by frankjance » Mon Dec 31, 2018 7:01 pm

Is there a way to set the Canvas Size using AppleScript?

I have written a script that adds a watermark image to a photo. The watermark is 237px by 280px. If the photo is shorter or narrower than the watermark image, I'd like to change the canvas size of the photo - adding white space to the background - before the watermark is inserted. How can this be done in AppleScript?

I don't want to scale the image. I only want to make the photo's canvas size larger.

Thanks,
Frank

GC 10.1.0
OSX 10.11.6

User avatar
forum_adm
Site Admin
Posts: 585
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: Setting Canvas Size via AppleScript

Post by forum_adm » Tue Jan 01, 2019 9:37 am

Hi Frank,

you can use:
change margins v : changes the margins of the object
change margins window
with list of integer : delta values (left, top, right, bottom), positive values add a margin, negative values remove the margins

I suggest to create a batch and call this via the command line. That is much faster.

Thorsten

frankjance
Posts: 4
Joined: Mon Dec 31, 2018 6:54 pm
Country: Canada

Re: Setting Canvas Size via AppleScript

Post by frankjance » Wed Jan 02, 2019 5:33 am

Hi Thorsten,

Thanks for the reply. I didn't get an email saying you had replied, so I ended up using Image Events. However, I'd prefer to do everything in GC.

After some trial and error, I got change margins to work. Here's the code I came up with:

Code: Select all

set the_image to ":Applications:MAMP:htdocs:original_images:xxxx4.jpg"

tell application "GraphicConverter 10"
	activate
	open the_image
	tell front window
		copy image dimension to {orig_pic_width, orig_pic_height}
		set {left_margin, right_margin, top_margin, bottom_margin} to {0, 0, 0, 0}
		if orig_pic_width < 300 then
			set the_difference to 300 - orig_pic_width
			set {left_margin, right_margin} to {the_difference / 2, the_difference / 2}
		end if
		if orig_pic_height < 300 then
			set the_difference to 300 - orig_pic_height
			set {top_margin, bottom_margin} to {the_difference / 2, the_difference / 2}
		end if
		change margins with {left_margin, top_margin, right_margin, bottom_margin}
	end tell -- front window
end tell
This does resize the canvas, but I wasn't able to get "background color" to work. What's the proper syntax for that?

Frank

User avatar
forum_adm
Site Admin
Posts: 585
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: Setting Canvas Size via AppleScript

Post by forum_adm » Wed Jan 02, 2019 9:51 am

That is on old RGB format: set to {0xFFFF,0xFFFF,0xFFFF} for white.
That is on old RGB format: set to {0,0,0} for black.

frankjance
Posts: 4
Joined: Mon Dec 31, 2018 6:54 pm
Country: Canada

Re: Setting Canvas Size via AppleScript

Post by frankjance » Wed Jan 02, 2019 2:17 pm

I'm getting compile errors in Script Debugger. When trying things like:

Code: Select all

change margins with {left_margin, top_margin, right_margin, bottom_margin} set background color to {0,0,0}

Code: Select all

change margins with {left_margin, top_margin, right_margin, bottom_margin} set to {0,0,0}
I get:
Expected end of line, etc. but found "set".

User avatar
forum_adm
Site Admin
Posts: 585
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: Setting Canvas Size via AppleScript

Post by forum_adm » Wed Jan 02, 2019 4:27 pm

The background color is a window property. Just set it before the change margin comand:
Screen Shot 2019-01-02 at 16.26.29.jpg
Screen Shot 2019-01-02 at 16.26.29.jpg (45.39 KiB) Viewed 482 times

frankjance
Posts: 4
Joined: Mon Dec 31, 2018 6:54 pm
Country: Canada

Re: Setting Canvas Size via AppleScript

Post by frankjance » Wed Jan 02, 2019 7:07 pm

The background color is a window property. Just set it before the change margin comand:
OK, that works. Thanks. Unless I'm reading it incorrectly, the dictionary makes it look like it's part of the Change Margins syntax.

Now I just have to figure out the color numbering scheme. I thought RGB used values from 0 to 255, however these go up to 65535.

BTW, the hex colors "{0xFFFF,0xFFFF,0xFFFF}" that were mentioned before don't work for me. Script Debugger won't compile the script.

Thanks again for your help,
Frank

User avatar
forum_adm
Site Admin
Posts: 585
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: Setting Canvas Size via AppleScript

Post by forum_adm » Thu Jan 03, 2019 7:41 am

The AppleScript syntax is from MacOS 9.
So, the RGB color has 16 bit component.
So, the range is from 0 to 65535.

Thorsten

Post Reply