Output changes when script saved as application? (Constants)

I am a beginning scripter. As an exercise I created this script (below) using pieces provided by Apple, which uses Image Events to return various properties of an image file and display it in a dialog box. It works perfectly in Script Editor. When I save it as an application and run it, it works… but the information displayed for Flie Type (previously displayed as RGB for instance) turns into «constant ****RGB». The same kind of transformation happens for the color space and bit depth information. My assumption is that Image Events has these words or phrases tagged as constants. I have tried setting - instead of copying - the information as text, but it makes no difference. Would somebody please take the time to explain to me 1.) Why the output is different when it is run as an app instead of as a script out of Script Editor, 2.) What do I need to do to get it to work as an app, and 3.) If necessary, how to tell the information being assigned to each of these variables that I only want a certain part of it to appear on the dialog. For instance, how can I tell it to delete the “«constant ****” and “»” portions, but display the RGB portion? You can see my futile attempt at this…


on number_to_text(this_number)
set this_number to this_number as text
if this_number contains “E+” then
set x to the offset of “.” in this_number
set y to the offset of “+” in this_number
set z to the offset of “E” in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to “”
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & “0”
end try
end repeat
return the converted_number

else
	return this_number
end if

end number_to_text


on round_truncate(this_number, decimal_places)
if decimal_places is 0 then
set this_number to this_number + 0.5
return number_to_text(xsize div 1)
end if

set the rounding_value to "5"
repeat decimal_places times
	set the rounding_value to "0" & the rounding_value
end repeat
set the rounding_value to ("." & the rounding_value) as number

set this_number to this_number + rounding_value

set the mod_value to "1"
repeat decimal_places - 1 times
	set the mod_value to "0" & the mod_value
end repeat
set the mod_value to ("." & the mod_value) as number

set second_part to (this_number mod 1) div the mod_value
if the length of (the second_part as text) is less than the ¬
	decimal_places then
	repeat decimal_places - ¬
		(the length of (the second_part as text)) times
		set second_part to ("0" & second_part) as string
	end repeat
end if

set first_part to this_number div 1
set first_part to number_to_text(first_part)
set this_number to (first_part & "." & second_part)

return this_number

end round_truncate


set this_file to choose file
try
tell application “Image Events”
launch
– open the image file
set this_image to open this_file

	-- extract the property value
	set xloc to the path of this_file as text
	set xsize to the size of this_file
	set xdate to the modification date of this_file as text
	set xname to the name of this_image as text
	copy the resolution of this_image to {xres, yres}
	copy the dimensions of this_image to {xdim, ydim}
	copy the file type of this_image to xtype
	
	--set xtype to the file type of this_image as text
	
	--if xtype contains "mill" then
	--set xtype to "millions of colors" as text
	--end if
	
	copy the color space of this_image to xmode
	copy the bit depth of this_image to xdepth
	
	-- purge the open image data
	close this_image
end tell

--set xsize to file size of this_image

display dialog ("Image name: " & xname & return) & return & ("Location path: " & xloc & return) & return & ("Modification date:  " & xdate & return) & return & "File size: " & round_truncate(number_to_text(xsize / 1024), 3) & " KB" & return & return & ("Resolution:  " & xres & " x " & yres & " dpi" & return) & return & ("Dimensions:  " & xdim & " x " & ydim & " pixels" & return) & return & ("File type: " & xtype & return) & return & ("Color mode: " & xmode & return) & return & ("Bit depth: " & xdepth) buttons {"Thanks!"} default button 1

on error error_message
display dialog “Doh!” & return & error_message with icon stop
end try

You are going to be a very good scripter if stuff like this doesn’t make you run away. This is what we call a “bug.” The coercion of the file type constant to a string returns one thing if run in script editor and another in an application.

So… we learn to work around these things and wait for the next version of our favorite apps. Sometimes you can use another app to do what you want, or get the information from somewhere else, or do it another way. Sometimes finding workarounds is fun and sometimes it drives you nuts. But it’s never boring…

Someone else might know a way to coerce this constant to a string so it comes back with something consistant. I don’t know how, though, so I cheat…



set this_file to choose file
try
	tell application "Image Events"
		launch
		set this_image to open this_file -- open the image file
		
		set xloc to the path of this_file as text -- extract the property value
		set xsize to the size of this_file
		set xdate to the modification date of this_file as text
		set xname to the name of this_image as text
		set {xres, yres} to the resolution of this_image
		set {xdim, ydim} to the dimensions of this_image
		set xtype to file type of this_image
		
		if (xtype is PDF) then
			set xtype to "It's a PDF"
			-- you could add other else if comparisons here
		end if
		
		set xmode to the color space of this_image
		set xdepth to the bit depth of this_image
		
		close this_image -- purge the open image data
	end tell
	
	
	display dialog ("Image name: " & xname & return) & return & ("Location path: " & xloc & return) & return & ("Modification date: " & xdate & return) & return & "File size: " & round_truncate(number_to_text(xsize / 1024), 3) & " KB" & return & return & ("Resolution: " & xres & " x " & yres & " dpi" & return) & return & ("Dimensions: " & xdim & " x " & ydim & " pixels" & return) & return & ("File type: " & xtype & return) & return & ("Color mode: " & xmode & return) & return & ("Bit depth: " & xdepth) buttons {"Thanks!"} default button 1
	
on error error_message
	display dialog "Doh!" & return & error_message with icon stop
end try

Thanks “digest4d” for the info. I was originally hoping for a more elegant (read: easier) resolution to the problem, but I guess we have to do what we have to do…

I have done what you suggested, adding an if else statement for each of the file types, color spaces and bit depths found in the Image Events library. This works fine - sometimes. Your basic RGB, CMYK, TIIF, JPEG info reports normally (yeah!) but now it is saying that xdepth is not defined - but only when checking a bitmap Tiff. It also says “can’t get item 1 of {}” when checking an image with 5 color channels, a grayscale eps or a Lab color JPEG. What additional info about how Image Events is reading and/or reporting this information am I missing here?
One other thing. After each of my series of if else statements, I close with an

else
set xtype to “Unrecognized file type”
end if

However, if it gets to this point, it says that xtype is undefined rather than assigning “Unrecognized file type” to it. Ideas?

My next step will be to create an interface for this app that will allow a drag and drop of a single image or multiple images to return this info. If there is anyting I need to know about preparing this script for that phase I would also appreciate any help offered…