DrDavid
DrDavid

Reputation: 964

Applescript: Convert date into something useful, then perform calculation

I'm trying to create an applescript script that will take a date in the form:

02/20/99

Then, subtract 5 days from the date, and return a date like this:

02/15/99

Here's the basic applescript I have, but, obviously, I'm missing the logic around converting the date into something that I can manipulate (and returning it to something readable by humans).

set itemPath to quoted form of POSIX path of theFile

set dateString to do shell script "awk 'NR == 10 {print $3}' " & itemPath & " -"
set myDueDate to dateString - (5 * days)

set theTask to "Pay AMEX bill"

tell application "OmniFocus"
    tell front document
        set theContext to first flattened context where its name = "OFFICE"
        set theProject to first flattened context where its name = "Bookkeeping - PMS"
        tell theProject to make new task with properties {name:theTask, context:theContext, due date:myDueDate}
    end tell
end tell

Thanks in advance!

Upvotes: 1

Views: 4400

Answers (2)

stevieegee
stevieegee

Reputation: 1

If you get error "FileMaker Pro got an error: Object not found." number -1728, add my before date. e.g,

set thisDate to my date dateString.

When working inside a tell, I think it needs to access methods outside the tell's scope.

Upvotes: 0

Digital Trauma
Digital Trauma

Reputation: 16016

I think these lines should be sufficient for what you need:

set dateString to "02/20/99"

set thisDate to date dateString
set fiveDaysEarlier to thisDate - 5 * days
log date string of fiveDaysEarlier
log short date string of fiveDaysEarlier

This page has some more examples of the coercions supported when converting strings to objects of date class: AppleScript Class Reference: date

Also, it probably goes without saying, but the string "02/20/99" is not Y2k compliant, so guesses will have to be made somewhere as to which century this date lies in.

Upvotes: 1

Related Questions