s22 Tech

Useful code.

AppleScript code for GraphicConverter

Creates 4 picture sizes from 1 master photo.

Trying to find AppleScript code for GraphicConverter can be very difficult. After searching the web for days, we were finally able to put together a script to automatically create 4 different size photos (thumbnail, small, medium, large) in PNG and JPEG from a master photo in any format so they can be uploaded to LiteCart. Here is the script we came up with. Hopefully this can help someone with their scripting needs.

Tested with GraphicConverter 7 to 10 on MacOS 10.6.2 to 10.15.7 but may work with other versions.

This script is unsupported.


## Change these properties to the sizes that you prefer
property largeWidthMax : 700 -- no wider than __pixels
property largeHeightMax : 950 -- no taller than __pixels
property mediumWidthMax : 300
property smallWidthMax : 200
property smallHeightMax : 250
property thumbWidthMax : 100

set newHeight to ""

set changeQty to 0 -- count the # of changes this script makes to the master photo

display dialog "Name this photo w/o an extension..." default answer ""
set photoName to text returned of result as string

set MFG to {"Mfg_1", "Mfg_2", "Mfg_3"}
set chosen_ to (choose from list MFG with prompt "Choose the mfg...." without multiple selections allowed) as text
if result is "false" then
return -- exit script
end if

set usedMFG to changecaseof(chosen_, "lower")

tell application "Finder" -- Change these paths to match your setup.
set largeFilePath to "Mac_HD:Applications:MAMP:htdocs:mfg:" & usedMFG & ":ssp_images:large:" as string
set mediumFilePath to "Mac_HD:Applications:MAMP:htdocs:mfg:" & usedMFG & ":ssp_images:medium:" as string
set smallFilePath to "Mac_HD:Applications:MAMP:htdocs:mfg:" & usedMFG & ":ssp_images:small:" as string
set thumbFilePath to "Mac_HD:Applications:MAMP:htdocs:mfg:" & usedMFG & ":ssp_images:thumb:" as string
end tell

tell application "GraphicConverter"
activate
tell front window
set picMode to mode
set origPicDimensions to image dimension
set origPicWidth to item 1 of origPicDimensions -- width
set origPicHeight to item 2 of origPicDimensions -- height

# LARGE Photo (used for largest picture format)
if origPicWidth > largeWidthMax then -- if the original width is greater than largeWidthMax
set newHeight to round ((largeWidthMax / origPicWidth) * origPicHeight) -- set new dimensions
set image dimension to {largeWidthMax, newHeight}
set changeQty to changeQty + 1 -- counts the number of changes made for the "Undo" loop
end if
if newHeight > largeHeightMax then -- if the new height is greater than largeHeightMax, refigure new dimensions
undo
set changeQty to 0
set newWidth to round ((largeHeightMax / origPicHeight) * origPicWidth) -- set new dimensions
set image dimension to {newWidth, largeHeightMax}
set changeQty to changeQty + 1
end if

delay 1
if picMode ≠ 3 then
set mode to 3 -- (3 is for RGB, which is smaller than CMYK - 4)
set changeQty to changeQty + 1
end if
set JPEG quality to 80
set JPEG progressive to true
save in largeFilePath & photoName & ".jpg" as JPEG with makeCopy and wwwready
## makeCopy saves a copy w/o permanently altering the open photo so you can 'Revert To Saved' and start again.
repeat changeQty times

undo -- instead of reverting - undo only the # of times this script changes the master photo so it won't undo any cropping done manually before this script is run.
end repeat
set changeQty to 0


# MEDIUM Photo .png (used for static web pages, if smaller than JPG)
set newHeight to round ((mediumWidthMax / origPicWidth) * origPicHeight)
set image dimension to {mediumWidthMax, newHeight}
set changeQty to changeQty + 1
if picMode ≠ 3 then
set mode to 3 -- (3 is for RGB)
set changeQty to changeQty + 1
end if
save in mediumFilePath & photoName & ".png" as PNG with makeCopy and wwwready
repeat changeQty times
undo
end repeat
set changeQty to 0


# MEDIUM Photo .jpg (used for static web pages, if smaller than PNG)
set newHeight to round ((mediumWidthMax / origPicWidth) * origPicHeight)
set image dimension to {mediumWidthMax, newHeight}
set changeQty to changeQty + 1
if picMode ≠ 3 then
set mode to 3 -- (3 is for RGB)
set changeQty to changeQty + 1
end if
set JPEG quality to 80
set JPEG progressive to true
save in mediumFilePath & photoName & ".jpg" as JPEG with makeCopy and wwwready
repeat changeQty times
undo
end repeat
set changeQty to 0

# SMALL Photo (for categories and SurfShopPRO detail pages)
set newHeight to round ((smallWidthMax / origPicWidth) * origPicHeight)
set image dimension to {smallWidthMax, newHeight}
set changeQty to changeQty + 1
if newHeight > smallHeightMax then -- if the new height is greater than smallHeightMax, refigure new dimensions
undo
set changeQty to 0
set newWidth to round ((smallHeightMax / origPicHeight) * origPicWidth) -- set new dimensions
set image dimension to {newWidth, smallHeightMax}
set changeQty to changeQty + 1
end if

set changeQty to changeQty + 1
if picMode ≠ 3 then
set mode to 3 -- (3 is for RGB)
set changeQty to changeQty + 1
end if
save in smallFilePath & photoName & ".png" as PNG with makeCopy and wwwready
repeat changeQty times
undo
end repeat
set changeQty to 0

# THUMBNAIL Photo (used for Hot Item photo in SurfShopPRO)
set newHeight to round ((thumbWidthMax / origPicWidth) * origPicHeight)
set image dimension to {thumbWidthMax, newHeight}
set changeQty to changeQty + 1
if picMode ≠ 3 then
set mode to 3 -- (3 is for RGB)
set changeQty to changeQty + 1
end if
save in thumbFilePath & photoName & ".png" as PNG with makeCopy and wwwready
repeat changeQty times
undo
end repeat
scale horizontal 1 vertical 1 --view original photo at 100%

beep

end tell -- front window
end tell -- GraphicConverter

## Tell Finder to go to the medium folder, compare the size of the .jpeg vs the .png file, and delete the larger of the two.
tell application "Finder"
set jpgFile to mediumFilePath & photoName & ".jpg"
set pngFile to mediumFilePath & photoName & ".png"

set jpgSize to size of (info for file jpgFile)
set pngSize to size of (info for file pngFile)

if jpgSize > pngSize then
set imageMedium to mediumFilePath & photoName & ".png"
delete jpgFile
else
set imageMedium to mediumFilePath & photoName & ".jpg"
delete pngFile
end if

end tell


set imageThumb to thumbFilePath & photoName & ".png"
set imageSmall to smallFilePath & photoName & ".png"
-- imageMedium is set above
set imageLarge to largeFilePath & photoName & ".jpg"


## If you want GraphicConverter to open the new photos...
tell application "GraphicConverter"
activate
open imageThumb
open imageSmall
open imageMedium
open imageLarge
end tell

## Open FileMaker Pro and run the "Update Image List" script. Not needed if you're not using FileMaker Pro.
if app_is_Running("FileMaker Pro") then
tell application "FileMaker Pro Advanced"
activate
go to database "Vendors" -- this brings the Vendors db to the front
do script FileMaker script "Update Image Lists"
delay 2
do menu menu item "Hide Window" of menu "Window" -- (this hides the Vendors db window)
end tell
else
display dialog "Open FileMaker and update the image lists."
end if


on changecaseof(thistext, thiscase)
if thiscase is "lower" then
set the comparisonstring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set the sourcestring to "abcdefghijklmnopqrstuvwxyz"
else
set the comparisonstring to "abcdefghijklmnopqrstuvwxyz"
set the sourcestring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the newtext to ""
repeat with thisChar in thistext
set x to the offset of thisChar in the comparisonstring
if x is not 0 then
set the newtext to (the newtext & character x of the sourcestring) as string
else
set the newtext to (the newtext & thisChar) as string
end if
end repeat
return the newtext
end changecaseof


on app_is_Running(appName)
tell application "System Events" to (name of processes) contains appName
end app_is_Running