image events not converting files

Hey guys, Ive got the following subroutine designed to convert any files fed to it into PNG.
the problem is, the files get saved in the correct folder (/private/tmp/) with the correct file name, but they are not of the type I specified (PNG)

Also, I was trying to get the properties of the file so I could determine the file type to see -if- it even needed conversion, but I just could not get that to work, as getting the properties always returned an error (hence why they are commented out). The application I feed the files to can only accept files of JPEG or PNG type, so I want to convert any non-supported files to png.

on convertImage(tempImage)
	
	tell application "Image Events"
		
		--open the original image
		set theImage to open tempImage
		--copy the properties of theImage to imageProperties
		
		--if the image isnt jpg or png, convert it to png
		--if imageExtension is not in {"JPEG", "PNG"} then
		
		--change the extension to .png
		set previousDelimiter to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set fileSplits to text items of tempImage
		set last item of fileSplits to "png"
		set newTempImage to fileSplits as string
		set AppleScript's text item delimiters to previousDelimiter
		
		--get just the file name, and put it in the tmp folder
		set previousDelimiter to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "/"
		set fileSplits to text items of newTempImage
		set newFile to the last item of fileSplits
		set AppleScript's text item delimiters to previousDelimiter
		
		set newTempImage to "/private/tmp/" & newFile
		
		--save the image as a png file
		save theImage as PNG in file newTempImage
              --end if
		close theImage
	end tell
	

	return newTempImage

Hi,

this modified version works on my machine

convertImage((choose file) as Unicode text) -- only for testing

on convertImage(tempImage) -- assuming the parameter is a string path
	set {name:Nm, name extension:Ex} to info for tempImage as alias
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	if Ex is not in {"JPEG", "PNG"} then
		tell application "Image Events"
			--open the original image
			set theImage to open tempImage
			set newTempImage to "/private/tmp/" & Nm & ".png"
			--save the image as a png file
			save theImage as PNG in file newTempImage
			--end if
			close theImage
		end tell
		return newTempImage
	end if
	return false
end convertImage

Every time I try that code, I get a “file [path to file] wasn’t found” applescript error on this line:

set {name:Nm, name extension:Ex} to info for tempImage as alias

Edit: setting it to a POSIX file fixed that problem still doing some fiddling with the rest of it though

Hi,

posting only a subroutine makes it very hard to guess the kind of parameter, which will be passed thru the handler.
For this reason I wrote assuming the parameter is a string path
Which kind of parameter do you use?

It was a string, but as a POSIX path, which aparantly I had to specify.

Then you should somewhere coerce the POSIX path to a HFS path with

set HFSpath to POSIX file POSIXpath

You should normally work only with HFS paths in AppleScript unless you use shell scripts

I am dealing with a shell script :slight_smile:
Thanks again for your help.

can sips convert images of type “pictClipping” such as the image now provided from itunes when you drag the image out of a file to your desktop.

i really wish that itunes would provide a normal image file again. :stuck_out_tongue: