Turn a String into a value

Hello

How do I go about breaking a value as a string. E.g. Turning “PNG” into PNG

Thanks?

Derek :slight_smile:

Is “PNG” not a string?

Do you want this:

set x to "PNG"
set x to characters of x

Yes it is, but the PNG without quotes is a enumerated constant of Image Events. Unfortunately the OP forgot to post the context :wink:

@Derek: You need a list of the constants in a Image Events tell block. Then compare the string against (aConstant as text) in a repeat loop. It’s very important that any access of the constants take place within the Image Events tell block, because neither AppleScript nor any other application has an idea where these constants (they are actually integer values) come from

Thanks Guys.

Here is what I have at the minute. I am trying to create a function whereby I can pass in the required format.

on convertImage(targetFormat)
	set theImage to choose file
	set theOutputFolder to choose folder
	tell application "Image Events"
		launch
		set theImageReference to open theImage
		tell theImageReference
			save in (theOutputFolder as string) & ("Converted Image.jpg" as string) as targetFormat
			close
		end tell
	end tell
end convertImage

convertImage("JPEG")

@Stefan I understand in principle what you are saying but I am still relatively new to applescript so unsure as to how to actually put it in place. Any help is greatly appreciated.

Derek :slight_smile:

on convertImage(targetFormat)
	set theImage to choose file
	set theOutputFolder to choose folder
	tell application "Image Events"
		launch
		set theImageReference to open theImage
		tell theImageReference
			if targetFormat is "JPEG" then
				save in (theOutputFolder as string) & ("Converted Image.jpg" as string) as JPEG
			else if targetFormat is "PNG" then
				save in (theOutputFolder as string) & ("Converted Image.png" as string) as PNG
			end if
			close
		end tell
	end tell
end convertImage

convertImage("PNG")

something like this

on convertImage(targetFormat)
	set theImage to choose file
	set theOutputFolder to choose folder
	tell application "Image Events"
		launch
		set formatList to {JPEG, JPEG2, PICT, PNG, PSD, QuickTime Image, TIFF}
		repeat with oneFormat in formatList
			if (oneFormat as text) is targetFormat then
				set theImageReference to open theImage
				tell theImageReference
					save in ((theOutputFolder as text) & "Converted Image.jpg") as oneFormat
					close
				end tell
				exit repeat
			end if
		end repeat
	end tell
end convertImage

convertImage("JPEG")

Thanks Guys

Both are great ideas. I am going to go for @stefan’s suggestion as it is more flexible and robust, But thanks to you both.

Derek :slight_smile:

In Stefan’s example, you will need to adjust the extension to match the format type or they will all have an extension of “jpg.”

@craig Well Spotted. (Didn’t pick up on that) Thanks :slight_smile: