PDF dimensions and Image Events

Usually this script works, and its great when it does work. Sometimes it doesn’t work, and the following message pops up:
“The variable MyImage is not defined”

  1. Why does this happen sporadically?

  2. I’m trying to get a creation date out of Image Events, but that isn’t working either. Could anyone help?

–Herbert Ripka
Greendale, WI

The following was (99%) written by Jerome on Nov. 1, 2005
http://bbs.applescript.net/viewtopic.php?id=14783

set MyImage to ""

to MakeList(ProcessFolder)
	tell application "Finder"
		set filelist to (files of entire contents of alias ProcessFolder whose file type is "PDF" or name extension is "pdf" as list)
		return filelist
	end tell
end MakeList

to GetDimensions(TheFile)
	tell application "Image Events"
		set MyImage to open alias TheFile
		set MyResolution to resolution of MyImage
		set MyDimension to dimensions of MyImage
		set MyColorspace to color space of MyImage
		set InfoList to name of MyImage & "    " & MyResolution & "    " & MyColorspace & "    " & (item 1 of MyDimension) / (item 1 of MyResolution) & "    " & (item 2 of MyDimension) / (item 2 of MyResolution) & return
		close MyImage
	end tell
	return InfoList
end GetDimensions
set ProcessFolder to (choose folder) as string
set filelist to MakeList(ProcessFolder)
set MyPath to ((path to desktop folder) as string)
set logfile to MyPath & "Image_Log.txt"
open for access file logfile
close access file logfile
set MyList to ""
repeat with AnItem in filelist as list
	set MyList to MyList & (GetDimensions(AnItem as string))
end repeat
open for access file logfile with write permission
write MyList to file logfile
close access file logfile

Herbert:

I have had similar issues with Image Events, so I now insert the line close images every time it is called. It seems more consistent now, but I cannot explain why. Here is a script I use to get creation date, as well as other metadata tag info via Image Events:

set a to choose file

tell application "Image Events"
	close images
	set b to open (a as alias)
	set c to properties of b's metadata tag
	close b
	repeat with d in c
		if d's name is "creation" then
			set e to d's value
		end if
	end repeat
	
end tell
e --> "2005:10:23 19:03:07"
c --> (all metadata tag records) {{name:"formatOptions", description:missing value, class:metadata tag, value:"default"}, {name:"hasAlpha", description:missing value, class:metadata tag, value:false}, {name:"space", description:missing value, class:metadata tag, value:"RGB"}, {name:"pixelHeight", description:missing value, class:metadata tag, value:766.0}, {name:"typeIdentifier", description:missing value, class:metadata tag, value:"public.tiff"}, {name:"bitsPerSample", description:missing value, class:metadata tag, value:8}, {name:"creation", description:missing value, class:metadata tag, value:"2005:10:23 19:03:07"}, {name:"path", description:missing value, class:metadata tag, value:"/Users/casdvm/Pictures/Display Images/DispImage9.tiff"}, {name:"profile", description:missing value, class:metadata tag, value:profile "Generic RGB Profile" of image "DispImage9.tiff" of application "Image Events"}, {name:"dpiWidth", description:missing value, class:metadata tag, value:72.0}, {name:"pixelWidth", description:missing value, class:metadata tag, value:1024.0}, {name:"samplesPerPixel", description:missing value, class:metadata tag, value:3}, {name:"dpiHeight", description:missing value, class:metadata tag, value:72.0}, {name:"format", description:missing value, class:metadata tag, value:"tiff"}, {name:"software", description:missing value, class:metadata tag, value:"QuickTime 7.0"}}

Since the metadata tag property returns a list of records, looping through them seems to be the only way to get one piece of information out of it. You can see just how much stuff is in there in the final comment.

Good luck,

Thank you Craig for the insight!

  1. How do you get it to format “2005:10:23 19:03:07” ?

  2. The following is the metadata that I get: (Why No date??)
    {{name:“formatOptions”, description:missing value, class:metadata tag, value:“default”}, {name:“space”, description:missing value, class:metadata tag, value:“CMYK”}, {name:“pixelHeight”, description:missing value, class:metadata tag, value:3228.0}, {name:“bitsPerSample”, description:missing value, class:metadata tag, value:8.0}, {name:“dpiWidth”, description:missing value, class:metadata tag, value:300.0}, {name:“pixelWidth”, description:missing value, class:metadata tag, value:2410.0}, {name:“samplesPerPixel”, description:missing value, class:metadata tag, value:4.0}, {name:“dpiHeight”, description:missing value, class:metadata tag, value:300.0}, {name:“format”, description:missing value, class:metadata tag, value:“tiff”}}

  3. This is a different tack at getting a list of file names, and their creation dates, using Finder instead of Image Events:

set f to (choose folder)
tell application "Finder" to set n to name of items of f
tell application "Finder" to set e to creation date of items of f

if e = {} then
	beep
	display dialog "No items found in the folder "" & f & ""." buttons {"Cancel"} default button 1 with icon 0 giving up after 10
else
	set filelist to {"Contents of " & f & return & return}
	repeat with i from 1 to (count e)
		set end of filelist to ("" & n's item i & "     " & e's item i & return)
	end repeat
	set the clipboard to "" & filelist
	beep 2
end if

tell application "TextEdit"
	make new document at beginning with properties {text:filelist as Unicode text}
	activate
end tell

The result is:
060.pdf Monday, October 9, 2006 7:15:57 AM
003.pdf Monday, October 9, 2006 7:14:13 AM

Question: How do I get it into the nice format of “2005:10:23 19:03:07”, which is more easily sortable?

Hello

You may do the trick with these two handlers

tell application "Finder"
	set cd to creation date of file "Macintosh HD:Users:yvankoenig:Desktop:remarques sur Pages.pages:"
	set cd to my normalize(cd)
end tell

on normalize(d)
	return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & my pad(hours of d) & ":" & my pad(minutes of d) & ":" & my pad(seconds of d)
end normalize

on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

Due to the use of the coercion of month as number it works only with 10.4 and higher.

Yvan KOENIG (from FRANCE mercredi 18 octobre 2006 19:20:10)

I have OS 10.3.7, and the year, month and day works fine, but it stumbles on the hour.

???

Honestly, I have no idea. I am working today on my G4 iBook, which is about 18 months old. I have done all sorts of things to this machine during that time, running a lot of software tests (my own and downloaded stuff), but I can’t remember what I did or even if I did anything to create that format.

When I run Yvan’s script without the handler, I get the typical:

date “Sunday, January 29, 2006 9:22:20 AM”

I looked into my System Preferences under both International and Date/Time, but found nothing there to help. Sorry, I will check my other machines tomorrow when I am back in my officd and see how they return the metadata.

Additionally, I can only get the creation date via Image Events on a few PDF files myself. It works fine on all images except the thumbnails generated by iPhoto.

Oops

It seems that my memory fails.

Here is an old one I used with Mac OS 9.

I am sure that you will be able to make some kind of synthesis of the two.

tell application "Finder"
	set cd to creation date of file "Macintosh HD:Users:yvankoenig:Desktop:remarques sur Pages.pages:"
	set cd to my writeDateHeure(cd)
end tell

on writeDateHeure(d)
	try
		set dd to d as text -- génère une erreur si la date est incorrecte
		set j_j to (text -2 thru -1 of ("0" & (day of d)))
		set m_m to (text -2 thru -1 of ("0" & (my monthNum(d))))
		set yy to year of d --(text -2 thru -1 of ("" & (year of d)))
		if 1 = 2 then
			set tt to (time of d)
			set hh to round (tt / 3600) rounding down
			set mi to (round (tt / 60) rounding down) - (hh * 60)
			set ss to (tt mod 60)
			set h_h to (text -2 thru -1 of ("0" & (hh)))
			set m_i to (text -2 thru -1 of ("0" & (mi)))
			set s_s to (text -2 thru -1 of ("0" & (ss)))
			return j_j & "/" & m_m & "/" & yy & space & h_h & ":" & m_i & ":" & s_s
		else
			return j_j & "/" & m_m & "/" & yy & space & text -8 thru -1 of dd
		end if
	on error
		return "01/01/1904" & space & "00:00:00"
	end try
end writeDateHeure

on monthNum(dat)
	return 1 + (offset of (characters 1 thru 3 of (month of dat as string) as string) in ¬
		"JanFebMarAprMayJunJulAugSepOctNovDec") div 3
end monthNum


Unable to resist, I built a synthesis:

tell application "Finder"
	set cd to creation date of file "Macintosh HD:Users:yvankoenig:Desktop:remarques sur Pages.pages:"
	
	set cd to my normalize(cd)
end tell

on normalize(d)
	return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & my pad((time of d) div 3600) & ":" & my pad(minutes of d) & ":" & my pad(seconds of d)
end normalize

on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

Yvan KOENIG (from FRANCE mercredi 18 octobre 2006 21:35:19)

Yvan–

I am still trying to figure out what you are doing (this is way above my understanding of AppleScript).

Instead of

    return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & my pad((time of d) div 3600) & ":" & my pad(minutes of d) & ":" & my pad(seconds of d)

Do you mean:

	return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & my pad((time of d) div 3600) & ":" & my pad((time of d) div 60) & ":" & my pad((time of d) mod 60)

Now I have to put this snippet together, along with code that gets the name, and output a list.

Thank you very much for you and Craig’s efforts.

–Herbert Ripka
Greendale, WI

Hello

I was sleeping (in France) when you sent your late message.

I meant exactly what I posted:

tell application "Finder"
	set cd to creation date of file "Macintosh HD:Users:yvankoenig:Desktop:remarques sur Pages.pages:"
	
	set cd to my normalize(cd)
end tell

on normalize(d)
	return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & my pad((time of d) div 3600) & ":" & my pad(minutes of d) & ":" & my pad(seconds of d)
end normalize

on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

It’s in fact my initial script with just a change to take care of the fact that you wrote that the hours tool was missing in your system.
Reading your post, I assumed that minutes and seconds where available.
I was unable to check that because 10.3.x is no longer available on my machine.

If seconds and minutes are also missing you may use one of the two variants given here:

tell application "Finder"
	--set cd to creation date of file "Macintosh HD:Users:yvankoenig:Desktop:remarques sur Pages.pages:"
	set cd to current date
	set cd1 to my normalize(cd)
	log cd1
	set cd2 to my normalize2(cd)
	log cd2
end tell

on normalize(d)
	set tt to (time of d)
	set hh to (tt div 3600)
	set mi to (tt div 60) - (hh * 60)
	set ss to (tt mod 60)
	return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & my pad(hh) & ":" & my pad(mi) & ":" & my pad(ss)
end normalize

on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

on normalize2(d)
	return "" & year of d & ":" & my pad((month of d) as number) & ":" & my pad(day of d) & " " & text -8 thru -1 of (d as text)
end normalize2

CAUTION, normalize2 assumes that the date is given in 24hours format (not AM/PM)

Yvan KOENIG (from FRANCE jeudi 19 octobre 2006 08:33:16)