[AS] Image Events/Copy Dimensions

I’ve been looking for a way to count pages and compare page dimensions in a pdf without opening Acrobat. So far I’ve found a shell script that can count pages using Finder. To find page dimensions I have the following script using Image Events:

set thisFile to (choose file) as item
set AppleScript's text item delimiters to {","}
tell application "Image Events"
	launch
	set theImage to open thisFile
	copy the dimensions of theImage to {xres, yres}
	close theImage
end tell
display dialog "Dimension: " & {xres, yres as string}
end

It worked perfectly for about 4 runs. But now sends an error “Can’t find item 1 of {}” (refering to {xres,yres}). No clue why. I understand how it’s looking for a variable to be defined, but the script won’t let me change it to {x-axis.} or any other more defining property without changing “copy” to «event misccopy».

Thoughts?? I’m also open to other ideas on getting page dimensions (without Acrobat).

Hi,

not every PDF file has automatically metadata like page dimensions.
The contents of the property dimensions of System Events is a list of two values.
With the copy line you assign it to two variables, which are also a list:

copy the dimensions of theImage to {xres, yres}

is the same as:

copy item 1 of dimensions of theImage to xres
copy item 2 of dimensions of theImage to yres

If there are no data, the list is empty (no item 1) and you get an error.

2 Notes: choose file as item is nonsense, the result is an alias anyway, so omit “as item”
Changing the text item delimiters is okay, but you should reset it at the end of the script.
A better way to compose the string is:

display dialog "Dimension: " & (xres as string) & ", " & (yres as string)

I saw the same error myself yesterday working with single images…

I’m using this at the moment, and it seems to be working reliably:


set this_item to (choose file without invisibles)

tell application "Image Events"
	launch
	
	set target_image to open this_item as alias
	
	set image_dimens to dimensions of target_image
	set image_width to item 1 of image_dimens
	set image_height to item 2 of image_dimens
	set displayed_width to item 1 of image_dimens as text
	set displayed_height to item 2 of image_dimens as text
	
	set _Priors_ to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "." -- if dimens are fractional...
	set displayed_width to text item 1 of displayed_width
	set displayed_height to text item 1 of displayed_height
	set AppleScript's text item delimiters to _Priors_
	
	set displayed_dimens to displayed_width & " x " & displayed_height as string
	
end tell

tell me to activate
display dialog displayed_dimens

Anyone have a good routine for grabbing and displaying color space (including “Gray”) reliably?

Peter B.


Thank you for the reply and advice. It looks like the problem is the PDF’s metadata. No clue why but the PDFs I ran the script on yesterday suddenly do not have an embedded profile my script can retrieve. Tests on jpegs or other PDFs work fine. Is depending on a PDF’s embedded profile not a reliable source to tap for file info?

Peter,

Deja vu, I kept running into the color space question when I was looking for a dimension property answer. Here’s a link to other’s looking for the same (in case you haven’t found it yet that is). It’s in the apple forums.

http://lists.apple.com/archives/applescript-users/2007/Apr/msg00147.html

Hi,

my theory, that some PDF files doesn’t have metadata, isn’t true.
I just found out (thanks Peter Bunn for mentioning “fractional”), that Image Events doesn’t provide page dimensions,
if the value is fractional.

Here is a different approach using Spotlight metadata:

set inputFile to (choose file)
tell (info for inputFile) to set {fName, fKind} to {name, kind}
if fKind contains "pdf" then
	set {xres, yres} to paragraphs of (do shell script "mdls " & quoted form of POSIX path of inputFile & "| grep 'kMDItemPageHeight\\|kMDItemPageWidth' | awk '/ = / {print $3}'")
	display dialog "Dimensions of " & fName & return & "h: " & (xres as string) & ", w: " & (yres as string)
else
	display dialog fName & " isn't a PDF file"
end if

Hi Peter,

like in the post before, a way to grab the color space with Spotlight metadata:

set inputFile to quoted form of POSIX path of (choose file)
set ColorSpace to (do shell script "mdls " & inputFile & "| grep 'kMDItemColorSpace' | awk '/ = / {print $3}'")

Im not sure what you are refering to by fractional but this has been working fine for me.

set theFile to choose file with prompt "Where is the image"
--
tell application "Finder"
	set theFile to theFile as alias
end tell
--
try
	tell application "Image Events"
		set ThisImage to open theFile
		delay 1
		set theSize to dimensions of ThisImage
		set theWidth to my RoundDecimal(((item 1 of theSize) / 2.834645), 3, to nearest)
		set theHeight to my RoundDecimal(((item 2 of theSize) / 2.834645), 3, to nearest)
		close ThisImage saving no
	end tell
	display dialog (theWidth & "mm " & theHeight & "mm") as string
on error error_message
	display dialog error_message
end try
--
on RoundDecimal(NumberToRound, DecimalPlace, RoundType)
	set RoundFactor to 10 ^ DecimalPlace
	NumberToRound * RoundFactor
	round result rounding RoundType
	result / RoundFactor
end RoundDecimal

I don’t understand why you would not want to use Acrobat (with invisible) for this.

I’m working on re-writing a drop-script that can exports pages from InDesign file to PDF or take a PDF file, convert each PDF page to PNG via Photoshop, and insert each PNG as an image in a slide in Powerpoint.

Currently the script uses Acrobat to obtain page count/dimensions if a PDF is dropped on it. I’d like to take Acrobat out of the equation so one less application has to be used. It would be less if/then’s to keep track of and make for a smaller file.

On top of that I’m quite the newbie (can you tell) so simplifying whenever possible is very helpful.

My concern was that an Adobe PDF has a lot of information contained in it that would be much simpler to extract with Acrobat. If you know the nature of these PDF files then you may not need to utilize this info.
Im not sure if image events or shell will cope with all the varying page sizes, box types & rotation. Where as Acrobat can get you all this then pass it on to Photoshop.