Help with Image Events Script

Hey,

Newbie at this but I can see I’ll be spending a lot of time here. Just learning Applescript and was hoping
someone might be able to get me started with the basics for a script I’d like to use.

I found an app called SiteSucker that can download images from the web.
From there I need a script that will open that file, size it to 480 wide and save it to a specified
folder as a png with a new name. Seems simple enough but if you’re clueless like me at this point it’s very frustrating.

I’ve tried a bunch of variations of a base script like this one:

tell application “Image Events”
set theImage to open alias (POSIX file “/Users/USERNAME/Pictures/FILENAME.EXT”)
scale theImage to size 480
save theImage as PNG in target_path with icon
close theImage
end tell

Really appreciate any help with it!

Thanks,

Carl

Moved to correct forum.

I guess the file name stored in the variable target_path didn’t exist yet. So you have to make sure there is a file named such before sending the save command to Image Events. Else, Image Events will not store the image file.

tell application "Image Events"
	set theImage to alias (POSIX file (my stringByExpandingTildeInPath("~/Desktop/objC.pdf"))
	scale theImage to size 480
	
	
	set target_path to my stringByExpandingTildeInPath("~/Desktop/TestFile.png")
	my makeFile(target_path, 1)
	
	save theImage as PNG in target_path with icon
	close theImage
end tell

on makeFile(filename, sizeInBytes)
	do shell script "mkfile " & sizeInBytes & "b " & my stringForTerminal(filename)
end makeFile

on stringByExpandingTildeInPath(filename)
	-- get home directory
	set homeDir to POSIX path of (path to home folder)
	if homeDir does not end with "/" then set homeDir to (homeDir & "/") as string
	
	-- if tilde and ev slash
	if filename is "~" then return homeDir
	if filename is "~/" then return homeDir
	
	-- if filename does not contain tilde
	if filename does not start with "~/" then return filename
	
	-- normal filename
	set newName to (homeDir & (characters 3 thru -1 of filename)) as string
	
	return newName
end stringByExpandingTildeInPath

on stringForTerminal(filename)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set allItems to every text item of filename
	set AppleScript's text item delimiters to "\\ "
	set newPath to allItems as text
	
	set AppleScript's text item delimiters to tid
	return newPath
end stringForTerminal

Hope it helps,
ief2

or

set sourceFile to ((path to pictures folder as text) & "FILENAME.jpg")
set target_path to ((path to desktop as text) & "filename.png")

try
	tell application "Image Events"
		launch
		set theImage to open alias sourceFile
		scale theImage to size 480
		save theImage as PNG in target_path with icon
		close theImage
	end tell
on error e
	display dialog e
end try

Thanks a bunch Stefan but I can’t seem to get it to work using my paths:

/Users/TV/Downloads/www.luc.edu/webcam/wtc/wtcfull.jpg for the original

and

/Library/Application Support/Perceptive Automation/Indigo 4/IndigoWebServer/images/controls/variables/webcam+chicago.png

for the final png.

Thanks,

Carl

Also ief2, thanks for your script as well but it saves a corrupt png somehow.
Weird…I can’t open it in any of my imaging apps but the file is created and shows as webcam+chicago.png

Thanks,

Carl

OK…This script works except it doesn’t name the png file:

tell application “Image Events”
set theImage to open alias (POSIX file “/Users/TV/Downloads/www.luc.edu/webcam/wtc/wtcfull.jpg”)
scale theImage to size 480
save theImage as PNG in alias (POSIX file “/Users/TV/Downloads/www.luc.edu/webcam/wtc/”) with icon
close theImage
end tell

Can it be amended to name the saved file as webcam+chicago.png?

Many thanks,

Carl

I think you have to specify the full path to the file if you want to save the image.

tell application "Image Events"
    set theImage to open alias (POSIX file "/Users/TV/Downloads/www.luc.edu/webcam/wtc/wtcfull.jpg")
    scale theImage to size 480
    save theImage as PNG in alias (POSIX file "/Users/TV/Downloads/www.luc.edu/webcam/wtc/wtcfull.jpg") with icon
    close theImage
end tell

Hope it works,
ief2

I don’t like these POSIX path > POSIX file > alias coercions

the native file system path type of AppleScript is HFS path
and there are a lot of predefined folder references to avoid specifiying startup disks and user names.


set sourceFile to ((path to downloads folder as text) & "www.luc.edu:webcam:wtc:wtcfull.jpg")
set target_path to ((path to application support folder as text) & "Perceptive Automation:Indigo 4:IndigoWebServer:images:controls:variables:webcam+chicago.png")

try
	tell application "Image Events"
		launch
		set theImage to open alias sourceFile
		scale theImage to size 480
		save theImage as PNG in target_path with icon
		close theImage
	end tell
on error e
	display dialog e
end try

Truly awesome guys! Both work perfectly, can’t thank you enough.

Carl