What is the correct application to call to be able to use date?
Solution 1:
That's interesting, I had never noticed this oddity of AppleScript before. Strangely, if you create the date
object using a string by value, neither System Events nor Finder has a problem:
tell application "System Events"
set testResult to date "Friday, 21 September 2018 at 6:54:29"
end tell
is absolute fine (the format of my date string is different to yours, as per my system settings).
It's only when referencing the string's value using a variable that the issue presents itself:
tell application "System Events"
set testDate to "Friday, 21 September, 2018 at 6:54:29 PM"
set testResult to date testDate
end tell
as you pointed out, is not allowed.
Solution:
It turns out the solution is to refer the job to the top-level AppleScript object using my
(or AppleScript's
):
tell application "System Events"
set testDate to "Friday, 21 September, 2018 at 6:54:29 PM"
set testResult to my (date testDate)
end tell
Note to other users: AppleScript date strings are formatted according to your system's date/time settings, therefore a direct copy-n-paste of any of the above snippets—either mine or the OP's—may still generate an error. You need to determine the appropriate format to use for the date string on your system, which is most easily done by examining the value returned by the AppleScript command current date
, and using that as a template.