Working with dates in applescript

Posted on
Tue Mar 24, 2015 1:23 pm
Sleepy81 offline
Posts: 47
Joined: Sep 25, 2014

Working with dates in applescript

I am trying to write an applescript, but failing miserably.
What I want to do is the following:
I want to be able to go to a screen on my iPad and set a date for when I will be back from vacation. 2 days prior that date I want my "HouseMode" to swap from "Vacation" to "Away", thereby starting to heat up the house again.

What I am struggling with is calculating dates using Applescript, and getting the results to change a scheduled time/date trigger.

I have made the following variables:
VacationDay = the day of the month I will be back from vacation, example: 01
VacationMonth = the month (as an integer) I will be back from vacation, example: 04
VacationYear = the year I will be back from vacation, example: 2015
VacationRetur = a combination of the three variables above, giving example: 01042015
VacationDuration= Number of days from todays date until VacationRetur - 2 days. Example: 6

What I want to do Is make a script comparing todays date to VacationReturn, then populate VacationReturn.
VacationReturn would then be used to set the date of a trigger that changes my HouseMode from "Vacation" to "Away".

So for the example of me being on vacation until 1. April 2015, the script should set the trigger date on the 30. March 2015.

I have tried to use several guides, but I am struggling to get the date formats into a format that will not produce an error, and will calculate.

Anybody that can help me get started on this one? I think if I was shown how to properly calculate the difference between VacationReturn and todays date, I could manage the rest myself.

Posted on
Tue Mar 24, 2015 9:26 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Working with dates in applescript

This is the perfect example of why I don't use AppleScript unless I have to. Python has great date/time manipulation classes and lots of examples out on the net.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Mar 25, 2015 6:17 am
pvrfan offline
Posts: 53
Joined: Apr 19, 2011
Location: Near Toronto, Canada

Re: Working with dates in applescript

There is a good introduction to date handling in:

http://macscripter.net/viewtopic.php?id=24737

Apple's formal documentation is in the Applescript Language Guide (s/b a link under Help in Applescript Editor). Go to the Class Reference, see Date. Here's one of their examples of a date calculation:

date "Friday, November 9, 2007" + 4 * days + 3 * hours + 2 * minutes
--result: date "Tuesday, November 13, 2007 3:02:00 AM"


The 'trick' is that a Date object represents a specific date and time. The difference between two dates is expressed in seconds. Above, "2 * minutes" equates to 120 seconds; "hours" and "days" are also just constants to simplify representing the number of seconds in a time period.

Craig

Posted on
Fri Apr 10, 2015 1:32 pm
Sleepy81 offline
Posts: 47
Joined: Sep 25, 2014

Re: Working with dates in applescript

I solved it using Python instead. Took me a while to learn, but eventually worked out quite nice. Thanks for the advise and help!


Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests