Convert RGB or CMYK PSD, TIFF etc.. to RGB JPEG.

I have been trying to piece together a script (I’m a bit of a newbie) that will convert a copy of user selected image of unknown format & color space to an RGB JPEG file at a certain size into a specific folder. I need this to work without any 3rd party software.

Here’s what I have:


property saveTypes : {"BMP", "JPEG", "JPEG2", "PICT", "PNG", "PSD", "TIFF"}
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", "PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}

--Get the artwork file
set sourceFile to choose file with prompt "Choose art file(s)" of type openTypes without multiple selections allowed and invisibles

tell application "Finder"
	set theFile to duplicate file sourceFile to (path to documents folder as text)
	set thePicture to quoted form of POSIX path of theFile
	do shell script "/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' " & thePicture
	set ext to name extension of theFile
	set ext_length to count (get name extension of theFile)
	set newFile to ((text 1 thru -(ext_length + 1) of (get name of theFile)) & "JPG")
	try
		set name of theFile to newFile
	on error e
		display dialog e
	end try
end tell



tell application "Image Events"
	launch
	set newHeight to 1024
	set newWidth to 0
	set imageFile to (open newFile)
	set theSize to dimensions of imageFile
	set width to item 1 of theSize
	set height to item 2 of theSize
	set ratio to (width / height)
	set newWidth to (ratio * newHeight) as integer
	if newHeight > newWidth then
		scale imageFile to size newHeight
	else
		scale imageFile to size newWidth
	end if
	save imageFile as JPEG in newFile
end tell
display dialog "Wrote " & newFile giving up after 1

I am getting an error just before the shell script.

Hi,

the result of Finder’s duplicate command is a Finder file specifier which has no POSIX path property.
Coerce the file specifier to text or alias


set sourceFile to choose file with prompt "Choose art file(s)" of type openTypes

tell application "Finder"
	set theFile to duplicate sourceFile to (path to documents folder)
	set thePicture to quoted form of POSIX path of (theFile as text)
.

Notes:
¢ without multiple selections allowed and invisibles is the default behavior of choose file
¢ the result of choose file is an alias specifier which should be used directly (without keyword file)
¢ the result of path to . is an alias specifier, the as text coercion is not needed and not recommended

I’ve taken the liberty of editing your script, hope you don’t mind. :slight_smile:

There were many little things. First one was like Stefan said. Then I changed the try…error block to be a bit more extensive and display the error number too.

Then there was the problem of the name of the file. You tried to force the duplicate’s name to change to a new name with a JPG extension although the file is most probably not in that format. You should do this when you save the file at the end, when you are certain that the file is actually a JPEG. And also you tried to save the file to a path that only defined it’s name, no path to this file.

There was also a few other enhancements to simplify the readability of the script and some changes to help Image Events receive things as it wants them. Let us know if this helps.

There is as few lines that are commented out, they were for debugging purposes, i.e., they’re useless. :slight_smile:

property saveTypes : {"BMP", "JPEG", "JPEG2", "PICT", "PNG", "PSD", "TIFF"}
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", "PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}

--Get the artwork file
set sourceFile to choose file with prompt "Choose art file(s)" of type openTypes without multiple selections allowed and invisibles

tell application "Finder"
	try
		set errorOccured to false
		set pathToAppSupportInUserDomain to (path to application support as string)
		set pathToSaveNewFiles to (path to desktop folder as string)
		set nameOfTempFolder to "FILEPROCESSING"
		set nameOfProcessedFilesFolder to "FILES_PROCESSED"
		try
			tell application "Finder" to make new folder in container (path to application support) with properties {name:nameOfTempFolder}
		end try
		try
			tell application "Finder" to make new folder in container (path to desktop folder) with properties {name:nameOfProcessedFilesFolder}
		end try
		set tempDestinationFolder to (pathToAppSupportInUserDomain & nameOfTempFolder & ":") as string
		set processedFilesDestinationFolder to (pathToSaveNewFiles & nameOfProcessedFilesFolder & ":") as string
		
		--tell application "Finder" to open tempDestinationFolder
		--display dialog tempDestinationFolder as string
		set theDuplicateFile to duplicate file sourceFile to tempDestinationFolder as alias
		set theDuplicateFileAsPosix to quoted form of (POSIX path of (theDuplicateFile as string))
		do shell script ("/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' " & theDuplicateFileAsPosix)
		set theDuplicateFileExtension to name extension of theDuplicateFile
		set newFileName to ((text 1 thru -((count of characters in theDuplicateFileExtension) + 1) of (get name of theDuplicateFile)) & "JPG")
		--display dialog newFileName
	on error theError number theErrorNumber
		set errorOccured to true
		try
			tell application "Finder" to delete file (theDuplicateFile as alias)
		end try
		tell application "Finder" to display alert "Error!" message ((theErrorNumber & " : " & theError) as string) buttons {"OK"} default button "OK"
	end try
end tell


if errorOccured is false then
	tell application "Image Events"
		try
			launch
			set imageFile to open (theDuplicateFile as string)
			set theSize to dimensions of imageFile
			set {imageWidth, imageHeight} to {item 1 of theSize, item 2 of theSize}
			set imageRatio to (imageWidth / imageHeight)
			set newHeight to 1024
			set newWidth to (imageRatio * newHeight) as integer
			if newHeight > newWidth then
				tell imageFile to scale to size newHeight
			else
				tell imageFile to scale to size newWidth
			end if
			tell imageFile to save as JPEG in (processedFilesDestinationFolder & newFileName)
		on error theError number theErrorNumber
			set errorOccured to true
			try
				close imageFile
				tell application "Finder" to delete file (theDuplicateFile as alias)
			end try
			tell application "Finder" to display alert "Error!" message ((theErrorNumber & " : " & theError) as string) buttons {"OK"} default button "OK"
		end try
	end tell
	if errorOccured is false then display dialog "Wrote " & newFileName giving up after 1
	try
		close imageFile
		tell application "Finder" to delete file (theDuplicateFile as alias)
	end try
end if

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

WOW!

Thank you leonsimard & stefank!

I knew I had problems with the script, other than what I had first mentioned, but I needed to get past that part first. I am going to have to dig through your revised script & figure out what you’ve got going on there!
Like I said, I am sort of new at this & am mostly learning by picking through other people’s scripts to figure out what they are doing & modifying them to suit what I need.

This has been so very helpful!
Thanks again.

You’re welcome. Actually, for me, trying to understand other people’s script is how I got better at it. This is why I’ve introduced a few things for you to explore and (hopefully!) have fun with.

Good luck!

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)