[Solved] AppleScript to add watermark

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

[Solved] AppleScript to add watermark

Post by frankjance »

I have searched high and low for this without success. I wrote one years ago for use with iMagine Photo (see below) but that app no longer works on Catalina, so I'd like to convert it for use with GC10. This is for watermarking a single photo - I don't want to do batch processing. If anyone has any ideas, I'd really appreciate it. Thanks!

Here's the AppleScript I wrote:

Code: Select all

on watermark(water_mark_file, orig_image, wm_corner)
	set copyrightText to "© 2015"
	set artistText to "Me, myself, and I"
	set new_filename to item 2 of parse_file(orig_image) & "." & item 3 of parse_file(orig_image)
	--display dialog new_filename
	set filepath to item 1 of parse_file(orig_image)
	
	set amount_to_blend to 15 as integer -- (% transparency)
	
	tell application "iMagine Photo.app"
		-- Import image and watermark and create a window to draw them to.
		set waterMarkImporter to «event ASDsimGr» water_mark_file
		if the «class imEr» of waterMarkImporter is not equal to 0 then
			close waterMarkImporter
			return
		end if
		set thisImporter to «event ASDsimGr» orig_image
		
		(* Set copyright info *)
		set theProps to the «class iPdt» of thisImporter
		copy the «class GrTy» of theProps to graphicType
		
		tell thisImporter to «event ASDsmaEx» given «class prdt»:{«class exFT»:"JPEG"}
		set the «class eFlc» of thisImporter to orig_image
		set the «class eExD» of thisImporter to {{«class udTy»:«constant udTy©cpy», «class exUn»:copyrightText}, {«class udTy»:«constant udTy©ART», «class exUn»:artistText}}
		
		if the «class imEr» of thisImporter is not equal to 0 then
			close waterMarkImporter
			return
		end if
		
		set {x, y, xDim, yDim} to the «class NBRe» of thisImporter
		set thisDocument to make new «class WiDo» with properties {«class gWSz»:{xDim, yDim}}
		set the «class grDc» of thisImporter to thisDocument
		set the «class grDc» of waterMarkImporter to thisDocument
		
		«event ASDsdraw» thisImporter
		(* 
		  Scaling and positioning code for the watermark.  
		  The height of the watermark will be no taller than 1/3 the height of the image.
		*)
		set {x, y, xWDim, yWDim} to the «class NBRe» of waterMarkImporter
		if xDim > yDim then
			set one_third to xDim / 3
			if xWDim ≤ one_third then
				set scaleX to 1
			else
				set scaleX to one_third / xWDim
			end if
			set scaleY to scaleX
		else
			set one_third to yDim / 3
			if yWDim ≤ one_third then
				set scaleY to 1
			else
				set scaleY to one_third / yWDim
			end if
			set scaleX to scaleY
		end if
		
		(* Since the watermark is now smaller, use the new size in setting the position. *)
		set new_xWDim to xWDim * scaleX
		set new_yWDim to yWDim * scaleY
		
		if wm_corner is equal to "Top Left" then
			set theTop to 10
			set theLeft to 10
		else if wm_corner is equal to "Center Left" then
			set theTop to ((yDim - new_yWDim) / 2) as integer
			set theLeft to 10
		else if wm_corner is equal to "Bottom Left" then
			set theTop to yDim - new_yWDim - 10
			set theLeft to 10
		else if wm_corner is equal to "Top Middle" then
			set theTop to 10
			set theLeft to ((xDim - new_xWDim) / 2) as integer
		else if wm_corner is equal to "Center Middle" then
			set theTop to ((yDim - new_yWDim) / 2) as integer
			set theLeft to ((xDim - new_xWDim) / 2) as integer
		else if wm_corner is equal to "Bottom Middle" then
			set theTop to yDim - new_yWDim - 10
			set theLeft to ((xDim - new_xWDim) / 2) as integer
		else if wm_corner is equal to "Top Right" then
			set theTop to 10
			set theLeft to xDim - new_xWDim - 10
		else if wm_corner is equal to "Center Right" then
			set theTop to ((yDim - new_yWDim) / 2) as integer
			set theLeft to xDim - new_xWDim - 10
		else if wm_corner is equal to "Bottom Right" then
			set theTop to yDim - new_yWDim - 10
			set theLeft to xDim - new_xWDim - 10
		end if
		
		set the «class Scal» of waterMarkImporter to {scaleX, scaleY}
		set the «class TLPt» of waterMarkImporter to {theLeft, theTop}
		set amount_to_blend to (amount_to_blend / 100) * 65535 as integer
		set blendOpColor to {amount_to_blend, amount_to_blend, amount_to_blend}
		set the «class drMd» of waterMarkImporter to {«class grMd»:«constant grMd », «class opCo»:blendOpColor}
		«event ASDsdraw» waterMarkImporter
		set «class eFlc» of thisDocument to filepath & ":" & new_filename
		set wm_image to filepath & ":" & new_filename
		«event ASDsxprt» thisDocument
		close thisDocument
		close thisImporter
		close waterMarkImporter
		quit
	end tell
	return wm_image
end watermark
User avatar
forum_adm
Site Admin
Posts: 1814
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: AppleScript to add watermark

Post by forum_adm »

Hi,

just call GraphicConverter via the command line.

Thorsten

Screen Shot 2021-06-18 at 07.27.09.jpg
Screen Shot 2021-06-18 at 07.27.09.jpg (306.2 KiB) Viewed 7425 times
frankjance
Posts: 9
Joined: Mon Dec 31, 2018 6:54 pm
Country: Canada

Re: AppleScript to add watermark

Post by frankjance »

forum_adm wrote: Fri Jun 18, 2021 7:27 am just call GraphicConverter via the command line.
Thanks, but I can't get it to work. Even using sudo I get this error:

Code: Select all

zsh: permission denied: /Applications/GraphicConverter 10.app
Any idea what's blocking it?

I checked the Secirity & Privacy control panel on Catalina and all I found was two checkboxes under Files and Folders, but ticking those didn't help.
User avatar
forum_adm
Site Admin
Posts: 1814
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: [Solved] AppleScript to add watermark

Post by forum_adm »

Hi,

you have to call the app itself.

So, for the current GraphicConverter 11 inside Applications folder:
"/Applications/GraphicConverter 11.app/Contents/MacOS/GraphicConverter 11"

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

Re: [Solved] AppleScript to add watermark

Post by frankjance »

Yep. I glossed right over that. :oops:

Would you supply an example of the format for "corner,x,y,opacity"? I'm not seeing anything in the manual. Using:

Code: Select all

-convert "/original.jpg" -watermark "top left,0,0,15" "/wm.jpg" -to "/new_img.jpg"
the new image is created, but it's not watermarked.

Thanks.
Last edited by frankjance on Fri Jun 18, 2021 6:39 pm, edited 8 times in total.
User avatar
forum_adm
Site Admin
Posts: 1814
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: [Solved] AppleScript to add watermark

Post by forum_adm »

Please use:

-convert "/original.jpg" -watermark "0,0,0,15" "/wm.jpg" -to "/new_img.jpg"

0 is left top
frankjance
Posts: 9
Joined: Mon Dec 31, 2018 6:54 pm
Country: Canada

Re: [Solved] AppleScript to add watermark

Post by frankjance »

forum_adm wrote: Fri Jun 18, 2021 7:34 pm Please use:

-convert "/original.jpg" -watermark "0,0,0,15" "/wm.jpg" -to "/new_img.jpg"
Thanks, but the image is still not watermarked. The original image is returned. I must be missing something. :?
forum_adm wrote: Fri Jun 18, 2021 7:34 pm 0 is left top
What are the values for the other corners, including middle left, middle right, center, etc.?
Last edited by frankjance on Fri Jun 18, 2021 8:49 pm, edited 1 time in total.
User avatar
forum_adm
Site Admin
Posts: 1814
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: [Solved] AppleScript to add watermark

Post by forum_adm »

I found the bug in my docs.

Please exchange the position and the path:
-watermark "/wm.jpg" "0,0,0,15"

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

Re: [Solved] AppleScript to add watermark

Post by frankjance »

That did it. Thanks!

I experimented and found the other corners:

Code: Select all

0 = top left
1 = bottom left
2 = top right
3 = bottom right
4 = center
However, I wasn't able to find any options for left center, right center, top middle, and bottom middle. Do those not exist? I hope they do, as I could sure use them.

Compared to what it's replacing, it's great to find something so powerful and concise as this. Is the cli interface documented somewhere? If so, I'm not finding it.
Last edited by frankjance on Sun Jun 20, 2021 6:16 am, edited 2 times in total.
User avatar
forum_adm
Site Admin
Posts: 1814
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: [Solved] AppleScript to add watermark

Post by forum_adm »

Please download the latest BETA:
http://www.lemkesoft.org/beta.html

I added top-center and bottom-center. Also, you find now the numbers in the help menu.

Thorsten
Post Reply