Hendrik Luehrsen
Hendrik Luehrsen

Reputation: 105

AppleScript: Error -1728 while creating date object

I am trying to get the image date from a batch of images and paste that image date to another batch of images with the same file name (but different file type)

I found an AppleScript that helps me here: http://brettgrossphotography.com/2008/12/10/aperture-applescript-export-restore-metadata

However, the date part is tricky.

In the Exporting part I parse out the date normally, but the importing part doesn't work.

        if (curTagShort is "ImageDate") then
            log "-- try to set the date --"
            -- set curDate to (curVal as date)
             set curDate to rich text of (item ctr of theList)
             log (curVal as string)
             adjust image date (date curVal as string) of images {curImg}
        else

The above code produces the following log:

(*-- try to set the date --*)
(*Freitag, 26. September 2014 20:21:21*)
get date "Freitag, 26. September 2014 20:21:21" of image version id "CeT8CoCwSYuhejftq9kmag"
    --> error number -1728 from date "Freitag, 26. September 2014 20:21:21" of image version id "CeT8CoCwSYuhejftq9kmag"
(*date "Freitag, 26. September 2014 20:21:21" of item 1 of {«class rkdp» id "CeT8CoCwSYuhejftq9kmag" of application "Aperture"} kann nicht in Typ string umgewandelt werden.*)

The variable curVal is

set curVal to item ctr of theList

ctr is the current index, theList is a list... obviously.

So far for the non working part, if I replace >>curVal as string<< with the actual string "Freitag, 26. September 2014 20:21:21" it works.

With my little knowledge of applescript I think the problem is that "curVal" is part of a list, hence the

"Freitag, 26. September 2014 20:21:21" of item 1 of {«class rkdp» id "CeT8CoCwSYuhejftq9kmag" of application "Aperture"}

So is there a trick that I'm missing?

Thank you!

Upvotes: 0

Views: 577

Answers (1)

Simon White
Simon White

Reputation: 736

Don’t coerce the date object into a string. Date objects have a property called “date string” which you can use.

Instead of doing this:

date curVal as string

… do this:

date string of date curVal

Try this example script by itself in your AppleScript Editor to make it clear:

set theDate to the current date
set theDateString to the date string of theDate
set theTimeString to the time string of theDate
set theDateTimeString to theDateString & space & theTimeString
return theDateTimeString

In the Result pane at the bottom of the window in AppleScript Editor, you will see this script return the current date and time as a string, like so:

"Sunday, September 28, 2014 09:11:36"

ctr is the current index, theList is a list... obviously.

If you have to explain your variable names, then they are named badly. I recommend you rename “ctr” as “theCurrentIndex” and whatever “theList” is listing, specify that. For example: “thePhotoList.” Or you can use a plural name for your lists, for example “thePhotos” because then you can do things like say “repeat with thePhoto in thePhotos.”

Upvotes: 0

Related Questions