chronological by exif...

Well I put the path to the files but upon running the script, Script Editor told me that It cant get every file from the folder. This must have something to do with the fact that it is a mix of jpgs, raw and tiff files. The raw files even have seperate .THM files which I think are the thumnails. I don’t think the finder can read the raw files therefore It cant edit the comments. I wonder if iMagine Photo could do this?
~Joe

Do you get the same error when you use:

tell application "Finder" to set theseFiles to files of (choose folder)

instead of:

tell application "Finder" to set theseFiles to files of folder "Path:To:Folder:"

I’m a little confused here. Isn’t the “creation” date that is gotten from image events the same as sorting files in the finder by “creation date”? I think it’s the same date, and if this is the case then using image events won’t help Joe (aka nottoohairy) because he said he had problems sorting by the creation date in the Finder.

The date in the exif data is different from the “creation” date in the finder. There’s actually 3 different dates in the exif data. In my pictures all 3 of the exif dates are the same though. But these exif dates are different than the creation date gotten from the “Finder” or “Image Events”. As proof here’s what I got using the “creation date” from “Image Events” versus an exif date from “Graphic Converter”

set macFolder to choose folder
tell application "Finder" to set theFiles to files of folder macFolder

set exifData to {}
repeat with i from 1 to count of theFiles
	set thisFile to item i of theFiles as string
	try
		tell application "Image Events"
			launch
			open thisFile
			tell image 1 to set this_exifDate to (value of 1st metadata tag whose name is "creation")
			try
				close image 1
			end try
		end tell
	end try
	set end of exifData to {filePath:thisFile, creationDate:this_exifDate}
end repeat
quit application "Image Events"
exifData

results → {{filePath:“TimeMachine:testpics:IMGP3925.JPG”, creationDate:“2007:05:06 04:26:23”}, {filePath:“TimeMachine:testpics:IMGP3926.JPG”, creationDate:“2007:05:06 04:24:11”}, {filePath:“TimeMachine:testpics:IMGP3927.JPG”, creationDate:“2007:05:06 03:57:51”}}

set macFolder to choose folder
tell application "Finder" to set theFiles to files of folder macFolder

set exifData to {}
repeat with i from 1 to (count of theFiles)
	set thisFile to item i of theFiles as string
	tell application "GraphicConverter" to set thisExif to get file exif of thisFile
	repeat with i from 1 to (count of thisExif)
		if item i of thisExif contains "Date and time of original data generation" then
			set thisDNT to item i of thisExif
			exit repeat
		end if
	end repeat
	set TIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	set dateList to text items of thisDNT
	set AppleScript's text item delimiters to TIDs
	set this_exifDate to item -1 of dateList
	set end of exifData to {filePath:thisFile, exifDate:this_exifDate}
end repeat
exifData

results → {{filePath:“TimeMachine:testpics:IMGP3925.JPG”, exifDate:“2007:04:29 16:46:34”}, {filePath:“TimeMachine:testpics:IMGP3926.JPG”, exifDate:“2007:04:29 18:00:37”}, {filePath:“TimeMachine:testpics:IMGP3927.JPG”, exifDate:“2007:04:29 18:01:05”}}

So you can clearly see the dates are different. It seems like the creation date is the date the picture was added to the computer whereas the exif date is the date the picture was actually taken with your camera. With that said, I think Joe needs to be using something that will give him exif dates. There’s several things that will give him those dates. iMagine Photo is one of them but there are others. Graphic Converter is another and there are unix executables that will do the job also. You can even see all of the exif data for a picture using “Preview”. Open an image with Preview and hit command-i (to get info) and go to the details tab to see the exif data.

OK since my above comment I tried to find a “free” application that could get the exif data from a file. I found a unix executable called ExifTool to do this. So go here and download/install ExifTool. ExifTool looks useful for other things as well… like writing exif data to many different types of files (not just images).

 http://www.sno.phy.queensu.ca/~phil/exiftool/

You can use this script to write the exif date to the comments field of your files. This script first looks for the original date of the picture in the exif data. If it can’t find that date then it uses the creation date of the file.

tell application "Finder" to set theFiles to files of (choose folder)

-- get the exif data for the files using exiftool
set exifData to {}
repeat with i from 1 to (count of theFiles)
	set thisFile to item i of theFiles as string
	set posixPath to quoted form of POSIX path of thisFile
	
	-- check for the exif original date and use that if it exists
	set the_results to do shell script "exiftool -datetimeoriginal " & posixPath
	-- if a file doesn't have an "exif original date" then use the "File Modification Date/Time" (all files should at least have this date)
	if the_results is "" then set the_results to do shell script "exiftool -filemodifydate " & posixPath
	
	set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ": "}
	set theData to text items of the_results
	set AppleScript's text item delimiters to TIDs
	set end of exifData to {filePath:thisFile, exifDate:(item 2 of theData)}
end repeat

-- write the exif date to the comments field of the files
repeat with i from 1 to (count of exifData)
	set this_exifData to item i of exifData
	set the_file to filePath of this_exifData
	set the_date to exifDate of this_exifData
	tell application "Finder"
		set current_comment to comment of file the_file
		if current_comment is "" then
			set comment of file the_file to the_date
		else
			set comment of file the_file to the_date & return & current_comment
		end if
	end tell
end repeat

Unless you took the picture with your mac, the dates aren’t the same. The finders creation date reflects the moment when the image was copied to the hard drive. Whereas the EXIF date(s) reflect the actualI time your image was taken. This time’s accuracy depends on the date setting in your camera.

Using command line helpers is certainly the fastest way to go. I cringe at the idea of opening and closing thousands of images in an app like Graphic Converter just to extract some EXIF data.

You’re right and you’re wrong here SwissalpS. I looked into it more and you’re right that the finder creation date and the creation date from image events aren’t the same. But if you look at the results from my example above you’ll see that the creation date derived from image events is not the same date as the “Date and time of original data generation” derived from the exif data. Therefore the image events date is not the “date setting on your camera”. It’s some other date found in the exif data. My biggest point was that if you use the date from image events then you won’t be getting the true date a picture was taken, so if you want the true date derived from the “date setting on your camera” then you can’t use image events to get it. As such I proposed a couple different options.

In fact using graphic converter is lightning fast. Much faster than any other technique I tried including command line helpers. If you look at my script code “tell application “GraphicConverter” to set thisExif to get file exif of thisFile” you’ll see that graphic converter can read the exif data without actually opening the file.

Nice, I Missed that. Thanks.

Image Events has tried to frustrate me on many occasion :frowning:

I settled with the “command line PHP” way of doing EXIF work. To my knowlege it’s accurate and any OS X 10.4 Mac can use it by default.
As PHP is portable, the core-code can be used on many platforms provided the exif-module has been configured.

BTW, imgren also handles the not so exif-compatible RAW - format used by Minolta’s DiMage A2 and Uses the .THN files (which are essentially jpeg thumbnails) to extract the dates for movies.

I just tried your Xcode project SwissalpS, and unfortunately it didn’t get the dates right either. You’re getting the same date as image events gets… not the “original date and time” exif date. I tried to look through your php code to find the error, but I don’t know php so I couldn’t find it.

One other problem. I had my origin folder named testpics and my destination folder named testpics2. Your app gave me an error saying that my destination folder couldn’t be located inside of my origin folder… it wasn’t. They were in the same directory but not inside each other. When I renamed testpics2 to destpics2 your app worked.

FYI, as I was looking through the ExifTools instructions I found that it has the same feature as your php script… i.e. it can rename files based on exif data like the date. You can see it here. Just make sure to use the “datetimeoriginal” tag for the renaming to get the right date.
http://www.sno.phy.queensu.ca/~phil/exiftool/filename.html

line 143: $a_xif = @exif_read_data($pf, 0, TRUE);
line 146: $a_data[$i][“xidt”] = date(“Ymd_His”, $a_xif[“FILE”][“FileDateTime”]);
The sniplet below demonstrates how to get all the available exif-tags:

-- prepare the script / choose file
set theScript to "$pf = \"" & POSIX path of (choose file) & "\";
$a_xif = @exif_read_data($pf, 0, TRUE);
var_dump ($a_xif);"

-- run the script and save output to file
do shell script "php -r " & quoted form of theScript & " > " & POSIX path of ((path to desktop as Unicode text) & "exifinfo.txt")
-- run the script and return output
do shell script "php -r " & quoted form of theScript

to get specific tag:

set tagHeader to "EXIF"
set tagName to "DateTimeOriginal"

-- prepare the script / choose file
set theScript to "$pf = '" & POSIX path of (choose file) & "';
$a_xif = @exif_read_data($pf, 0, TRUE);
echo '" & tagHeader & " - " & tagName & " : ';
echo ($a_xif ['" & tagHeader & "']['" & tagName & "']);
echo '
FILE - FileDateTime : ' . $a_xif['FILE']['FileDateTime'] . ' -> ' . date('Y:m:d H:i:s', $a_xif['FILE']['FileDateTime']);"

-- run the script and return output
set theResult to do shell script "php -r " & quoted form of theScript

Thanks for the useful feedback regulus6633.

Our edits crossed… yes the choice of tag makes a difference.

That’s really cool. I like that SwissalpS. I don’t understand all of the code but I was able to make minor changes to your code to get just the information I wanted. Then I see how you used the date() function to make a date out of the FileDateTime value. Very interesting. Unfortunately I could never come up with that code myself unless I studied php.

On caveat, I noticed that not all image files contain the datetimeoriginal tag so you’d probably need an if statement to tell your script that if the datetimeoriginal value is empty then to use the filedatetime value (I’m assuming that its value is always present).

One last thing…
is there an exif_write_data() function in php? I did a quick google search for it and couldn’t find it. That’s what I need though. Sometimes I have my camera set to the wrong date and thus I need a way to change the datetimeoriginal tag. I figured out how to write that value with the ExifTool that I mentioned earlier but if php had that function I’d use that instead since php it’s installed on all Macs by default.

That’s exactly why I used the FILE section over EXIF (little time saver to get the app working, can always be refined later)

No. You have to write the tags yourself or use a tool (Open Source so you can tie it in to your script)
google “php exif write” to find some library like PEL

Writing is more complicated, sorry I’m not great at detecting and switching byte orders…

here some links that help get started in PHP:
PHP function index for OS X for quick overview
php.net for more details on PHP.

Hi
I know this an applescript forum and one should be concerned with scripting problems but…
I am a photographer and if you want to do this sort of stuff then can I recommend an app?
Photo mechanic will sort images (lightning fast) in capture order, filename, modification date and a whole host of other options including camera settings. You can rename the files using any of the sort data. It will read ALL your image formats including grouping lo res jpg and hi res TIFF options on some cameras. Its the quickest browser I know and the best $150 I have spent.
If what you are trying to achieve is not intergrated into a much larger workflow and you need to do this on a regular basis its worth the investment.
http://www.camerabits.com

Thanks everyone. I wish I knew more about scripting so that I could give more to this effort instead of just recieve. I am still in the process of working through Burt Altenburg’s Applescript for Absolute Starters.
Anyway I Used regulus6633’s way and got the ExifTool and ran the script he posted but got an error. It gives me an applescript error that says “can’t get item 2 of {”“}” and the scripteditor window comes back with this line highlighted:

set end of exifData to {filePath:thisFile, exifDate:(item 2 of theData)}

???,
Joe

above

set end of exifData to {filePath:thisFile, exifDate:(item 2 of theData)}

add this line:

log theData

in the window “Event Log History” you may see the output

My Guess: the_results is “” or does not contain a ": " causing theData to be too “short”
You might find some unexpected value in thisFile and posixPath

Sorry I haven’t responded sooner nottoohairy. It shouldn’t be hard to find the problem.
Do you see these lines in my script code?

You can see I’m first searching for the date called “datetimeoriginal”. I know that sometimes this date isn’t in the exif data of a file so you can see I told the script that if that date doesn’t exist then to look for the date “filemodifydate”. I think your error is because neither of those dates exists in the file you are looking at. I thought that every file had the “filemodifydate”, but maybe I’m wrong.

Anyway there’s an easy way to find out what dates your file has. Do you know how to use the Terminal? Just open the Terminal and type "exiftool -s " and then drag the problem file onto the Terminal window so that the path to the file is inserted after the command. Hit your “return” key and you will see the exif data. Look at it and see if either of those dates are in the exif data… if not then you found your problem. Find a date that is in your file’s exif data and put that value in the script and you should be good to go.

Tell me your results too, then we can figure out how to make the script work for everyone.

regulus6633,
Using exiftool from the command line I looked at three different file types in my folder and sure enough all of them had the original capture date in the filemodifydate field so I don’t know why I am getting the error?
~Joe

it would be usefull to know the values of the variables just before the error occurs:

log variableName
  -- or
display dialog variableName

not only the variables of the error provoking line are interesting, but also the values from which they have been derived from.

Just to let you know SwissalpS, and anyone else looking through this thread. Yesterday I contacted nottoohairy off-line about this. He sent me 4 of his pictures that he was having troubles with. On my computer there wasn’t any problems with the script. It worked just like it was supposed to with no errors… so I’m not sure why he’s having problems. Maybe he’ll figure something out and post it here.

Dear regulus6633, swissalpS and others,
I kept getting the same error with the script that regulus6633 posted, so I sorted my folder of images by kind, put all the raw files in one folder, all the tiffs in another and left all the jpgs togeather, then ran the script on each folder, which worked on the raw folder and the tiffs but still got the same error message on the jpgs…
The folder of jpgs is huge; more than 8gigs, and it is a collection of lots of pictures from different cameras etc. I am thinking that there are images in there that didn’t come from a camera and thus dont have the filemodifydate. I am going to search through and try to organize them better and run the script again.
By the way now that I have the creation date in the comments field, how do I organize by that?
Thanks,
~Joe

Go to [b]list view/b and click on the Comments title.
Apple+J for column options.

Good luck sorting all those images.