setting and then comparing dates

Boy do I need help. I’m trying to figure out how to set and then compare dates. My code goes like this:

– first I set two date variables
set catalogModDate to (current date) as date
set fileModDate to (current date) as date

– then I set string variables by pulling the strings from the window of an application (iView MediaPro), and from an array of strings
set catalogModDatetxt to modification date of object i of window catalog_name
set fileModDate to item 3 of item j of fileArray

– then I try to re-set the date variables
set fileModDate to fileModDatetxt
set catalogModDate to catalogModDatetxt

– lastly I try to compare the variables
if catalogModDate comes after fileModDate then

when executing the script, I get the error:
“Can’t make “Monday, June 16, 2003 9:51:25 AM” into a date”

What am I doing wrong? If I set fileModDate and catalogModDate like this
set fileModDate to “Monday, June 16, 2003 9:51:25 AM”
and then compare teh dates, it works. Why can’t I set the date variable to the contents of a string variable?

Please help - I’m pulling my hair out over this one.

Jason

You’re apparently trying to compare a date with a string. I’d guess your variable ‘catalogModDatetxt’ is being set to a date rather than a string, in which case ‘fileModDate’ gets set to a date too. (If this is true, you should change ‘catalogModDatetxt’ to a more suitable name.) ‘fileModDatetxt’, on the other hand, is probably a string from your array, so you need to set ‘fileModDate’ to the date version of that:

set catalogModDatetxt to modification date of object i of window catalog_name
set fileModDatetxt to item 3 of item j of fileArray

set fileModDate to date fileModDatetxt -- NB the keyword 'date'
set catalogModDate to catalogModDatetxt -- but not here if 'catalogModDatetxt' is really a date after all

if catalogModDate comes after fileModDate then

When you say it works if you set both ‘fileModDate’ and ‘catalogModDate’ to strings, what’s actually happening is that the two strings are being compared alphabetically, not calendrically. The fact that this is possible means you don’t get an error message. But the two variables both need to be dates to get the result you want.

By the way, the ‘current date’ command returns a date, so you don’t need to coerce the result to date as well. (I don’t think the ‘as date’ coercion exists outside of third-party OSAXen anyway.)

set catalogModDate to (current date)
set fileModDate to (current date)

Nigel,

Thanks so much for your help. Your explanation makes perfect sense. I have only one problem though - your suggestion didn’t work. That is, this code:

set fileModDate to date fileModDatetxt – NB the keyword ‘date’

gives the error:

iView MediaPro got an error: Can’t get date “Saturday, May 17, 2003 1:18:13 PM”. Access not allowed.

So it’s as if the coercion doesn’t work. Again, all I want to to is create a date variable out of the contents of an existing string variable.

Any other ideas?

That’s not a trivial side-issue. :wink:

My own problem is that I can’t tell from here what fileModDatetxt is actually being set to. You say it’s from an array of strings, so I’ll assume for the moment that it is a string.

Since it’s iView MediaPro that’s reporting the error, it may be that the instruction’s in a ‘tell application “iView MediaPro”’ block and the application doesn’t know how to set an AppleScript date in this way. If this is the case, here are three possible cures:

  1. End the ‘tell’ block before this line. Generally, you should only ‘tell’ applications to do things from their own dictionaries.

  2. Put another ‘tell’ inside the “iView MediaPro” one, addressed to the script - or more properly to the application running it:

tell application "iView MediaPro"

  -- Blah blah blah
  
  tell me to set fileModDate to date fileModDatetxt
  
  -- More blah
  
end tell
  1. Change the line to:
set fileModDate to my date fileModDatetxt

All three suggestions do basically the same thing - ie. take the task away from your application. If none of them work, let me know. :slight_smile:

Nigel,

Thanks again. I have to tell you that I’ve already tried to move / end / change the tell block with no difference. I’ve tried executing those lines inside a finder block, and outside any block, with no difference - I get a very similar error. And I was just about to say that your third suggestion didn’t work either… but it did! I think. I’ve actually been doing some research on this and everwhere I look I find references to the fact that Applescript can’t coerce a string to a date, including Apple’s own documentation:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/index.html

So I thought I was out of luck. But your suggestion seems to work, somehow. For example:

set aDate to “01/15/2003”
set anotherDate to date aDate

…does not work. I get the error I originally reported. However,

set aDate to “01/15/2003”
set anotherDate to my date aDate

DOES work, and date comparison are posible, and anotherDate appers to behave like a date.

I’m thrilled. You fixed it, I think.
:wink:

Hi, Jason.

I’m glad at least one of the suggestions appears to work, though I can’t understand why the first two don’t work if the third does.

I’ve been experimenting with the ‘date’ + variable construct in both 10.2.6 and 8.6 this morning. In no case did it work inside a ‘tell’ block directed at an application. (The Finder’s an application, which is why it doesn’t work there.) The creation of a date object from a string is a clever process that the AppleScript language knows how to do, but I think applications themselves don’t. Many can handle a date object when it’s passed to them, but can’t actually create one this way. All three of my suggestions were aimed at getting the script itself (‘me’) to do the conversion rather than telling an application to do it.