Dates don't work in OS X?

I’m trying to do some date maipulation on images in iPhoto, but I’m running into a basic syntax problem: just about anything that refers to dates doesn’t work, giving me syntax errors every time. For example:

set new_date to date "1/15/2003"
(or set new_date to date "15/1/2003" as I am in UK)

gives an ‘expected end of line but found "’ error on the first quote (I have tried different quote types too). Not one of the scripts I have found that use dates seem to work, including many that apparently work for others. The only thing I can do is read the current date, and even then, date arithmetic doesn’t work, e.g.

set new_date to current date + 1 week

gives a similar ‘expected end of line but found identifier’ on the “week” text.

Curiously, this compiles:

set new_date to date

but (not surprisinlgy) gives a runtime error.

What am I doing wrong? Isn’t the date type supported as standard as the AppleScript docs seem to suggest?

I have no trouble getting photo dates out of iPhoto, but they’re returned as unicode text rather than dates (why???). I’m successfully coercing those to basic strings, but I still can’t get them to convert to date variables because of the above problems, which seem unrelated to iPhoto.

I’m running a stock 10.2.6 with no additions.

I can’t see what’s wrong with your first examples. They should work as long as you use the version appropriate to the short-date order settings on your computer - presumably the UK style.

The proper syntax for adding a week to the current date is:

set new_date to (current date) + 1 * weeks

…or simply:

set new_date to (current date) + 1 * weeks

‘weeks’ is a constant containing the number of secons in a week.

It’s too hot and humid here today. :-

The simpler code I referred to above should of course be:

set new_date to (current date) + weeks

Thanks for the tips. I tracked down the cause of the problem.
iPhoto disables/overrides the system date class. e.g. this will compile:

set new_date to date "Wednesday, January 15, 2003 12:00:00 am"

but this:

tell application "iPhoto"
set new_date to date "Wednesday, January 15, 2003 12:00:00 am"
end tell

will not. The iPhoto “photo” class has a date property, it’s not in scope, so it should not be causing this clash. This seems like a bug. Is something else doing it? There don’t seem to be any other date properties/methods in iPhoto. How can I specify that I want to access the system date class and not the iPhoto one?
This is inside an iPhoto “tell” statement; can I temporarily “escape” from tell mode? Or perhaps tell some different thing (e.g. “system” or something) within the iPhoto tell?

The function I’m trying to achieve is to change dates on a bunch of iPhoto pictures by a fixed amount (my Camera was set to the wrong time zone).

The problem is probably that iPhoto is using the word date, which is sloppy on it’s programmers part. You’ll need to do any date-manipulation outside of the iPhoto tell block.

On a similar note, I REALLY wish the date was NOT read-only. What a pain!

If you are trying to change the creation and modification dates of the files, this may help…

set theDate to (do shell script "date '+%m/%d/%y/%H:%M:%S'") -- sets & formats the date to the current date
set x to quoted form of POSIX path of (choose folder)
do shell script "cd " & x & ";/Developer/Tools/SetFile -d " & theDate & " *" -- -d = creation date
do shell script "cd " & x & ";/Developer/Tools/SetFile -m " & theDate & " *" -- -m = mod date
do shell script "cd " & x & ";touch myTempFile;rm -f myTempFile" -- refresh window by adding file, removes it also

It will change the modification and creation dates of all the files in the selected folder (not recursively) to the current date minus one hour, I’m not sure why it goes back one hour though – Any Help on that would be nice --. You need to have the Apple Developer Tools installed for the script to work.

Greg, the issue here is the photo’s date-taken in iPhoto, not the image file’s creation date. Unfortunately, they are not the same, and changing one does not affect the other. You cannot change the photo-taken date using AppleScript, it is read-only.

I don’t need to change file dates, just the dates stored inside iPhoto, but a good tip anyway.

I can’t see any reason for iPhoto to be overriding date at this level, so I’ll report it as a bug to Apple.

Go ahead - hopefully they’ll listen. Of course, changing it now will cause a lot of problems for people who have already written scripts. IT should have used the term ‘date taken’ or ‘date shot’. Oh well. Ask them to make it NOT read-only, while you’re at it.

Actually, changing the name of the token shouldn’t affect existing, compiled scripts. They’d still work and the new name would appear in the script editor window when an old script was opened in the presence of a later version of the app. It’s been done before, for instance, in the case of the Standard Additions’ ‘new file’ command, which is now called ‘choose file name’.

This should not be a factor at all. the important bit in what I said was “at this level”. The bug is that although there is no documented date property (there is no date property or class defined in the iPhoto dictionary, other than as a property of the photo class, which is not in scope here), it is clearly affecting this context, which it should not be doing. So, there is no clash with the documented behaviour in changing this, and it will not affect existing scripts. Existing scripts that currently work around this bug will no longer have to work around it, so it won’t break anything if they fix it, even in uncompiled scripts…

Unfortunately this bug will, I suspect, make my script much slower as it will have to have 5-6 tell blocks instead of just one. The annoying thing about this bug is it means you have to break context every time you want to do a date calculation, which is just plain dumb.

Not sure what you mean by making it not read only – the dictionary marks the date property of the photo class as “i/o”, which would indicate to me that it is writable.

The date property of the photo is part of the EXIF info. Most apps allow you to read this but not modify it (e.g., iPhoto & GraphicConverter). QuickTime Player allows you to view and even modify this information in the “Creation Date” annotation field (even with AppleScript), but then, curiously, it doesn’t allow you to save the file as a JPEG. There is a freeware app PhotoInfo that allows you to change the EXIF info record and even has a batch feature for changing the dates on a folder full of images but, alas, it isn’t scriptable on its own. You could cobble something together using GUI scripting, though. I’m sure there’s also a way to read the EXIF info directly in AppleScript but I don’t know enough about EXIF to be helpful with that.

HTH,
Jon

Actually, PhotoInfo is apparently scriptable accoding to their site - in fact they go so far as to say that it’s designed for scripting rather than interactive use. I hesitated in trying to use that as any photos I dropped onto it came up with no exif info at all, including the date, when I know that the pictures have exif info. It’s own date setting is absolute, not relative, so though I could set all dates to be some particular time, I can’t see an easy way of getting it to read the date out of a picture, do some date arithmetic, then put it back.
It may be that the date field in iPhoto does not have anythign to do with exif, and is merely stored by iPhoto in its own DB. As far as I can tell, the scriptable date functions in iPhoto are exposed in the GUI in the info panel on the left that allows you to edit the date, which would agree with the dictionary’s assertiion that the date field is writable.
Oh well, I shall persevere…

Um, in AppleScript dictionaries there is no such thing as “[i/o]” - it’s just that the font is kinda small. Copy and paste that into TextEdit and make the font bigger - you’ll see clearly that it in fact is “[r/o]”, which is the standard AppleScript way of marking a property read-only. Read/write properties are just left unmarked. Read more about attempts to modify dates at http://iphoto.tidbits.com/iPhoto/3.

The reviews of PhotoInfo at VersionTracker don’t indicate that it actually works. There are two reviews that don’t give any details, and another that states the same problem CoolBru mentioned: no exif info shows at all. Doesn’t sound like something I’d like to try out.

The TidBiTs FAQ (at the url above) poitns out that probably the only way to script date changes right now is to use QuicKeys or YoupiKey (aka iKey), which I’ve started work on. The TidBiTs FAQ suggests doing it in Edit mode when scripting the GUI, since in Organize mode, it seems to take a direct click to make the picture’s own info show, instead of the album’s, even when a single photo is selected by script.

Ahem, sorry about that - the formatting in the dictionary viewer is hard to read, and it does indeed say r/o, not i/o. After some little tests, i can confirm that it definitely is read only!

I also got mixed up between image handling apps – the one I was talking about was Image Info, not Photo Info. You can find it here: www.kanzu.com

From their blurb:

Now that I know that I can’t do this with iPhoto alone, I might give this a go.

ImageInfo seems like it allows reading lots of info, but not setting it - iPhoto can return much of the same data. However, the PhotoInfo app mentioned earlier does do what is needed - alter dates in the files by a set increment for a single file or a whole folder. Unfortunately, since its not integrated into iPhoto, that means dragging the files from iPhoto to a folder, using PhotoInfo to update, then re-importing and resetting up all the ratings/comments/names from before.

Finally possible, but not an elegant solution by any means. Has anyone come up with a better way to do this directly from iPhoto?

Ironically, I bet that the “batch change” function in iPhoto was intended to do this, and the developer misunderstood. Technically, it does allow you to take a group of photos, and change their dates so they’re all incremented by an arbitrary time… but it shouldn’t have been incremented against the previous photo! I can’t think of a reason to batch change using the method in iPhoto…

Hi,

Here’s a workaround.duplicate, export, or whatever, the photo. Next change the creation date of the duplicate. Now delete the photo from iPhoto library. Finally, add the duplicate photo back. The date should be taken from the creation date.

That’s what happened to me.

gl,

Hi,

I just tried it again. Changed the modification date of a jpeg, deleted the original from the iPhoto library, then imported the duplicate. iPhoto takes the modification date fo the imported photo. You can change the mod date with the Finder but I think the mod date can’t be earlier than the creation date.

EDIT: Tried changing the mod date to before the creation and it worked. IPhoto used the mod date which came before the creation date.

gl,

Hi,

Here’s how far I got but I need to eat:

tell application “iPhoto”
activate
set the_album to first album whose name is “Test Album”
set the_photo to first photo of the_album
set image_path to (image path of the_photo)
end tell
set as_ref to (image_path as POSIX file) as alias
tell application “Finder”
set new_ref to (duplicate as_ref to desktop) as alias
set cre_date to (creation date of new_ref)
set new_date to (cre_date - 366 * days) – leap year I think
set modification date of new_ref to new_date
end tell
tell application “iPhoto”
activate
remove the_photo from photo library album
– how to add the file back?
end tell

Use a test album and place a test photo in there. Now how to add the file from the Finder to iPhoto?

gl,