add date to images

So I figured that since you guys helped me I’ll post the useful stuff I made with that help. This little script uses graphicconverter to add the date to the bottom right of the image. Its not the cleanest code around, and it isn’t as user friendly as it could be (i.e., some of the settings about the text size and placement could be more transparent), but it works. Any and all suggestions, criticism, and possible improvements are highly welcome. Here goes:

-- get the folder, make list of files inside, count them
set welcomeText to "This script will take a folder full of pictures and add the date the picture was taken (based on the creation date) to the bottom right-hand corner of all the pictures using GraphicConverter. If you do not own GraphicConverter, this script will not function."
display dialog welcomeText

set pictureFolder to choose folder

tell application "Finder"
	set listOfFiles to every file of pictureFolder
	set numberOfFiles to count of listOfFiles
end tell

tell application "GraphicConverter" to get registered
delay 2 -- have it launch now

-- for each file...
repeat with thisFile in listOfFiles
	-- set the name; it'll be useful later in the fullFilePath thing
	set fileName to name of thisFile
	-- this is the date we want to put on the pictures
	set creationDate to creation date of thisFile
	
	set fullFilePath to ((POSIX path of pictureFolder as string) & fileName) as string
	
	-- close open windows so that the frontmost one is always our file
	
	set width to last word of (do shell script "sips -g pixelWidth " & fullFilePath) as integer
	set height to last word of (do shell script "sips -g pixelHeight " & fullFilePath) as integer
	-- get file imageinfo thisFile as alias -- this would have worked also, after the fact
	
	-- we always want the date to be in the same spot no matter the size of the image. Here we calculate, based on a user-defined right and bottom buffer and a user-defined size of the textbox, (1) how large the textbox in which the date resides should be, and (2) the size of the text to best fit the image. Note that while I tried to put in some measures to account for vertical pictures, it isn't battle tested and likely won't work very well.
	
	set dateHeight to 0.1 -- how tall the date textbox should be, as a percent of the total image height
	set dateWidth to 0.4 -- same as above for width
	set bottomBuffer to height / 40 as integer -- same as above for size of bottom buffer... the space between the date and the bottom of the image
	set rightBuffer to width / 100 * 4 as integer -- same as above for right buffer
	
	set boxHeight_Top to height - (height * dateHeight) - bottomBuffer as integer
	set boxHeight_Bottom to height - bottomBuffer as integer
	set boxWidth_left to width - (width * dateWidth) - rightBuffer as integer
	set boxWidth_Right to width - rightBuffer as integer
	
	if height < width then
		set textSize to ((boxHeight_Bottom - boxHeight_Top) / 2) - 3 as integer
	else
		set textSize to ((boxWidth_Right - boxWidth_left) / 5) - 3 as integer
	end if
	set textToAdd to date_format(creationDate as string)
	
	-- add the text to the window, use the calculations above to figure out where it should be and how large
	tell application "GraphicConverter"
		open thisFile
		delay 1
		-- see the GraphicConverter dictionary for other possible options
		draw text into front window text textToAdd as string justification 0 color {64000, 50000, 31000} box {boxWidth_left, boxHeight_Top, boxWidth_Right, boxHeight_Bottom} font "Arial" size textSize textface 1 with antialias
		save front window
		close front window
	end tell
end repeat

-- this is used to make the date shorter (xx/xx/xx instead of "Saturday, January...")
on date_format(old_date)
	set {year:y, month:m, day:d} to date old_date
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8 & " " & items -11 thru -1 of old_date
end date_format

Hi, eykanal.

Just reading through your script, it looks very good. Just one thing I noticed that might affect users other than yourself:

A date coerced to string is in the format set up for dates and times in the user’s own “International” preferences. You’ve assumed above that the user will prefer the same eleven-character time format as your own. (Presumably a 12-hour time, followed by either " AM" or " PM".) If they don’t, the time part of the string put out by your handler could contain too many or too few characters.

It would be better to have the handler format the time itself from the time of the original date object. A 24-hour format would be best:

set creationDate to (current date)
set textToAdd to date_format(creationDate)

on date_format(old_date)
	set {year:y, month:m, day:d, time:t} to old_date
	tell (y * 10000 + m * 100 + d) as string to set date_part to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
	tell (1000000 + t div hours * 10000 + t mod hours div minutes * 100 + t mod minutes mod 100) as string to ¬
		set time_part to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
	
	return date_part & " " & time_part
end date_format

(* Tiger only:
on date_format(old_date)
	set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to old_date
	tell (y * 10000 + m * 100 + d) as string to set date_part to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
	tell (1000000 + h * 10000 + min * 100 + s) as string to ¬
		set time_part to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
	
	return date_part & " " & time_part
end date_format
*)

If you want to impose a 12-hour format with a Latin-initial suffix:

set creationDate to (current date)
set textToAdd to date_format(creationDate)

on date_format(old_date)
	set {year:y, month:m, day:d, time:t} to old_date
	tell (y * 10000 + m * 100 + d) as string to set date_part to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
	if (t < 12 * hours) then
		set suffix to " AM"
	else
		set suffix to " PM"
	end if
	tell (1000000 + ((t div hours + 11) mod 12 + 1) * 10000 + t mod hours div minutes * 100 + t mod minutes mod 100) as string to ¬
		set time_part to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7 & suffix
	
	return date_part & " " & time_part
end date_format