Thursday, November 23, 2017

#26 2011-06-10 07:18:38 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4660

Re: Dates & Times in AppleScripts

use seconds without units:

Applescript:

set TS to time string of ((current date) + (25 * 60))

or the word minutes:

Applescript:

set TS to time string of ((current date) + (25 * minutes))

then:

Applescript:

tell application "Quicksilver" to show large type TS

and by the way, when you call a handler from your script from a tell block you must include the word "my" in front of the handler call -- Quicksilver doesn't know about the handler.

That change makes your script work (without the last "end" you have in it). You do don't use an "end" statement following a one line tell. You can see from above that there's a much simpler way than you used.

Applescript:

--http://www.devdaily.com/apple/applescript-current-time-example-script-format

say "It will be " & getTimeInHoursAndMinutes() & " in 25 minutes" using "Alex"

on getTimeInHoursAndMinutes()
   -- Get the "hour"
   set timeStr to time string of (current date)
   set Pos to offset of ":" in timeStr
   set theHour to characters 1 thru (Pos - 1) of timeStr as string
   set timeStr to characters (Pos + 1) through end of timeStr as string
   
   -- Get the "minute"
   set Pos to offset of ":" in timeStr
   set theMin to characters 1 thru (Pos - 1) of timeStr as string
   set timeStr to characters (Pos + 1) through end of timeStr as string
   
   --Get "AM or PM"
   set Pos to offset of " " in timeStr
   set theSfx to characters (Pos + 1) through end of timeStr as string
   
   return (theHour & ":" & theMin & " " & theSfx) as string
   
end getTimeInHoursAndMinutes


set today to date string of (current date)
get today

tell application "Quicksilver" to show large type (my getTimeInHoursAndMinutes())


iMac running OS X 10.13.1

Offline

 

#27 2011-06-11 09:27:15 am

Skillet
Member
Registered: 2010-01-11
Posts: 127

Re: Dates & Times in AppleScripts

Wonderful thank you Adam that was very helpful.   This seems like a complicated way to get the seconds to not be included and show AM and PM but I am glad that I have something that works.  If anyone knows of a simpler way I'd love to hear about it. 

I am also guessing as Stephan mentioned it probably wouldn't work on everyones system and would depend on their time settings.


Applescript:

--http://www.devdaily.com/apple/applescript-current-time-example-script-format

say "It will be " & getTimeInHoursAndMinutes() & " in 25 minutes" using "Alex"

on getTimeInHoursAndMinutes()
   -- Get the "hour"
   set timeStr to time string of (current date)
   set Pos to offset of ":" in timeStr
   set theHour to characters 1 thru (Pos - 1) of timeStr as string
   set timeStr to characters (Pos + 1) through end of timeStr as string
   
   -- Get the "minute"
   set Pos to offset of ":" in timeStr
   set theMin to characters 1 thru (Pos - 1) of timeStr as string
   set timeStr to characters (Pos + 1) through end of timeStr as string
   
   --Get "AM or PM"
   set Pos to offset of " " in timeStr
   set theSfx to characters (Pos + 1) through end of timeStr as string
   
   return (theHour & ":" & (theMin + 25) & " " & theSfx) as string
   
end getTimeInHoursAndMinutes


set today to date string of (current date)
get today

tell application "Quicksilver" to show large type (my getTimeInHoursAndMinutes())

Oh darn I just ran the script again and realized it now says 10:83AM.  That worked fine when the time was between 00-30.

Thanks again Adam!

Last edited by Skillet (2011-06-11 10:00:18 am)

Offline

 

#28 2011-06-11 12:46:37 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4424

Re: Dates & Times in AppleScripts

Skillet wrote:

Oh darn I just ran the script again and realized it now says 10:83AM.  That worked fine when the time was between 00-30.


Yeah. You need to add the twenty-five minutes before extracting the time string.  smile

By the way, lines like:

Applescript:

set theHour to characters 1 thru (Pos - 1) of timeStr as string

… would be better rendered as:

Applescript:

set theHour to text 1 thru (Pos - 1) of timeStr

It's a more efficient, one-step process and doesn't depend on the current state of AppleScript's text item delimiters.

I think you could get away with using the 'words' of the time string, which would give you a shorter handler:

Applescript:

on getTimeInHoursAndMinutes(aDate)
   set ts to time string of aDate
   
   set hm to text 1 thru word 2 of ts
   if ((count ts's words) is 4) then set hm to hm & space & word 4 of ts
   
   return hm
end getTimeInHoursAndMinutes

say "It will be " & getTimeInHoursAndMinutes((current date) + 25 * minutes) & " in 25 minutes" using "Alex"

--tell application "Quicksilver" to show large type (my getTimeInHoursAndMinutes())


NG

Offline

 

#29 2011-07-22 11:33:12 am

mariojune
Member
Registered: 2011-07-22
Posts: 1
Website

Re: Dates & Times in AppleScripts

There are two other units that come in handy when performing business or scheduling calculations. Weeks and Months. Sometimes it's useful to determine how many weeks or months have elapsed between two particular dates. I've also used this in document backup scripts for determining when to trigger weekly & monthly backups.

Offline

 

#30 2011-07-26 09:10:26 pm

Skillet
Member
Registered: 2010-01-11
Posts: 127

Re: Dates & Times in AppleScripts

Nigel Garvey wrote:

It's a more efficient, one-step process and doesn't depend on the current state of AppleScript's text item delimiters.


Nigel I just wanted to say thanks again for your help on this, I use what you helped me with a couple dozen times daily and find it very useful!

Offline

 

#31 2013-05-14 06:55:58 am

hamw
Member
Registered: 2010-11-07
Posts: 8

Re: Dates & Times in AppleScripts

I recently installed a pulse water meter. With each 0.1 gallons use a pulse is generated. The pulses are recorded in a variable in Indigo, the home automation system.

I'd like to be able to log the data as current day, prior day, current week, prior week, current month and the preceeding 12 months, possibly going back to a couple of years. Obviously I need to create the relevant variables to store the data, but I'm not sure where to begin to get the data properly sorted. Any advice? I looked thru the thread and did not see a direct example.

Thanks!!!

Offline

 

#32 2013-05-14 06:18:07 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4660

Re: Dates & Times in AppleScripts

I think this does what you have in mind:

Applescript:


set timeStr to time string of ((current date) + 25 * 60) -- you add seconds
tell application "Quicksilver" to show large type (get timeStr)

or in shorter form:

Applescript:


tell application "Quicksilver" to show large type (time string of ((current date) + 25 * 60))


iMac running OS X 10.13.1

Offline

 

#33 2013-05-14 09:02:52 pm

hamw
Member
Registered: 2010-11-07
Posts: 8

Re: Dates & Times in AppleScripts

thanks for the quick reply. i downloaded Quicksilver and ran the script in the editor. This is what it produced:

tell application "Quicksilver"
    current date
        --> error number -10004
end tell
tell current application
    current date
        --> date "Tuesday, May 14, 2013 9:51"
Result:
error "Can’t make \"9:51\" into type number." number -1700 from "9:51" to number


If I can ask, why do I want to do this in Quicksilver when i am trying to just partition a data stream from Indigo, that will stay in Indigo variables? I was thinking I would just accumulate the pulses and then segregate them into bins on a schedule.

what about something like this?

set todaysTotal to value of variable "WaterMeterPulse_1_Today"

set yesterdaysTotal to todaysTotal

set thisweeksTotal to value of variable "WaterMeterPulse_3_ThisWeek"


set value of variable "WaterMeterPulse_2_Yesterday" to yesterdaysTotal

set {year:y, month:m, day:d, weekday:w} to (current date)
log w  --produced tue
log y  --produced 2013


--this kind of works. I would run the script every night at 11:59. Idea is that if it is not sunday at 11:59, script will add today's total to this week's total and set today's total to "". If it is sun, it will take this week's total and todays total, sub that into last week, and then set today's total and this week's total to ""


if w is not "sun" then
    set value of variable "WaterMeterPulse_3_ThisWeek" to thisweeksTotal + todaysTotal as number
        set value of variable "WaterMeterPulse_1_Today" to ""
end if
log value of variable "WaterMeterPulse_3_ThisWeek" as string

end if

--getting lost after this point.... I tried using "if d is tue" to get a script to execute tonight, but it wouldn't execute.  Does this approach basically look right to you?
(*
if d is Sun
    set value of variable "WaterMeterPulse_3_ThisWeek" to thisweeksTotal + todaysTotal
delay 1
    set value of variable "WaterMeterPulse_4_LastWeek" to value of "WaterMeterPulse_3_ThisWeek" to thisweeksTotal + todaysTotal

    set value of variable "WaterMeterPulse_3_ThisWeek" to ""
end if
*)

Last edited by hamw (2013-05-14 11:11:58 pm)

Offline

 

#34 2013-05-15 09:21:04 pm

hamw
Member
Registered: 2010-11-07
Posts: 8

Re: Dates & Times in AppleScripts

I think I figured it out. Each "current variable", for instance Today, This Week and This Month, are incremented each time the water meter clicks via a single trigger. Every day at 12:01 AM a schedule runs the following script:

Applescript:


set {year:y, month:m, day:d, weekday:w} to (current date)


--Today and Yesterday. Second line in each sequence resets the current variable to ""
set value of variable "WaterUse_2_Yesterday" to value of variable "WaterUse_1_Today" as string

set value of variable "WaterUse_1_Today" to ""


--This week and last week. Only runs on Monday morning.
if w is Monday then
   set value of variable "WaterUse_4_LastWeek" to value of variable "WaterUse_3_ThisWeek"
   set value of variable "WaterUse_2_Yesterday" to ""
end if


--This month and last month. Only runs on first of month.
if d is 1 then
   set value of variable "WaterUse_6_LastMonth" to value of variable "WaterUse_5_ThisMonth"
   set waterUseThisMonth to value of variable "WaterUse_5_ThisMonth"
   set value of variable "WaterUse_5_ThisMonth" to ""
end if


--Prior Month historical data
if d is 1 then
   if m is January then
       set value of variable "WaterUse_18_Dec" to waterUseThisMonth
   end if
   
   if m is February then
       set value of variable "WaterUse_7_Jan" to waterUseThisMonth
   end if
   
   if m is March then
       set value of variable "WaterUse_8_Feb" to waterUseThisMonth
   end if
   
   if m is April then
       set value of variable "WaterUse_9_Mar" to waterUseThisMonth
   end if
   
   if m is May then
       set value of variable "WaterUse_10_Apr" to waterUseThisMonth
   end if
   
   if m is June then
       set value of variable "WaterUse_11_May" to waterUseThisMonth
   end if
   
   if m is July then
       set value of variable "WaterUse_12_Jun" to waterUseThisMonth
   end if
   
   if m is August then
       set value of variable "WaterUse_13_Jul" to waterUseThisMonth
   end if
   
   if m is September then
       set value of variable "WaterUse_14_Aug" to waterUseThisMonth
   end if
   
   if m is October then
       set value of variable "WaterUse_15_Sep" to waterUseThisMonth
   end if
   
   if m is November then
       set value of variable "WaterUse_16_Oct" to waterUseThisMonth
   end if
   
   if m is December then
       set value of variable "WaterUse_17_Nov" to waterUseThisMonth
   end if
   
end if
           

Honestly, this is probably not very elegant. If anyone can show me how to write this in a less "brute force" way, I'd appreciate seeing how it's done.

Last edited by hamw (2013-05-15 10:10:43 pm)

Offline

 

#35 2013-05-16 06:35:55 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4424

Re: Dates & Times in AppleScripts

Hi hamw.

Is your query about manipulating AppleScript dates or about scripting Indigo? If the latter, it should be posted in the "AppleScript | Mac OS X" forum rather than appended to this article about dates. And since it's possible that not many people will have experience of Indigo, you might include some information to lessen any confusion your scripts could cause. For instance, does Indigo have 'variable' elements with 'value' and 'name' properties?

Assuming your script in the previous post works, and that 'variables' are indeed elements in Indigo's scripting dictionary, the following should also work. It makes use of an aspect of AppleScript dates.  wink

Applescript:


set {year:y, month:m, day:d, weekday:w} to (current date)


--Today and Yesterday. Second line in each sequence resets the current variable to ""
set value of variable "WaterUse_2_Yesterday" to value of variable "WaterUse_1_Today"
set value of variable "WaterUse_1_Today" to ""


--This week and last week. Only runs on Monday morning.
if w is Monday then
   set value of variable "WaterUse_4_LastWeek" to value of variable "WaterUse_3_ThisWeek"
   set value of variable "WaterUse_2_Yesterday" to ""
end if


--This month and last month. Only runs on first of month.
if d is 1 then
   set m to m as integer -- Coerce the AppleScript month to the equivalent month number.
   set namedLastMonthVariableName to item m of {"WaterUse_18_Dec", "WaterUse_7_Jan", "WaterUse_8_Feb", "WaterUse_9_Mar", "WaterUse_10_Apr", "WaterUse_11_May", "WaterUse_12_Jun", "WaterUse_13_Jul", "WaterUse_14_Aug", "WaterUse_15_Sep", "WaterUse_16_Oct", "WaterUse_17_Nov"}

   set waterUseThisMonth to value of variable "WaterUse_5_ThisMonth"
   set value of variable "WaterUse_6_LastMonth" to waterUseThisMonth
   set value of variable namedLastMonthVariableName to waterUseThisMonth
   set value of variable "WaterUse_5_ThisMonth" to ""
end if


NG

Offline

 

#36 2013-05-16 03:29:59 pm

hamw
Member
Registered: 2010-11-07
Posts: 8

Re: Dates & Times in AppleScripts

Nigel, of course please move the posts to the proper area. That would be great as I might have some other questions related to this project later.

Appreciate the hints re the script; I am interested to see how your modificatiosn work . Also, yes, Indigo uses variables and values, which make it quite useful for doing stuff. It's a great program if you have a chance to play with it.

Thanks again!

Offline

 

#37 2014-12-01 07:26:14 pm

Calion
Member
Registered: 2010-03-24
Posts: 5

Re: Dates & Times in AppleScripts

So Apple documents an issue with the date function, such that you can't do

Applescript:

date of (current date)

and get December 1, 2014.

Is there any workaround for this? That's exactly what I need: Some way to return today's date without the weekday. Just the month and day would be sufficient, or even something like 12/1/2014 or 2014-12-1.

Last edited by Calion (2014-12-01 07:29:10 pm)

Offline

 

#38 2014-12-01 07:55:04 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Dates & Times in AppleScripts

You'll never use date of (current date) because date is already an date object. The documentation is wrong, we'll know but similar commands with other classes won't work either like string of "Hello World!". To get the date in another notation you could use something like:

Applescript:

tell (current date) to return "" & it's year & "-" & (it's month as integer) & "-" & it's day

Last edited by DJ Bazzie Wazzie (2014-12-02 07:16:45 am)

Offline

 

#39 2014-12-01 10:52:14 pm

Calion
Member
Registered: 2010-03-24
Posts: 5

Re: Dates & Times in AppleScripts

Thanks! You'd think there was an easier way to do something so simple, but this should work.

Offline

 

#40 2014-12-02 02:57:49 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Dates & Times in AppleScripts

date formatting is never simple, because there are many formatting conventions for all the countries around the world, even you mentioned two quite different string patterns


regards

Stefan

Offline

 

#41 2014-12-02 06:53:31 am

ccstone
Member
Registered: 2009-02-07
Posts: 409

Re: Dates & Times in AppleScripts

Calion wrote:

You'd think there was an easier way to do something so simple, but this should work.


Hey Calion,

Dealing with dates is pretty simple; it's just a bit verbose.

Applescript:

set _date to current date
set dateString1 to date string of _date # Date-String depends upon the system date setting.
set dateString2 to YYYYMMDD(_date, "/")
set dateString3 to YYYYMMDD(_date, "-")

set _date to date "Thursday, May 04, 1961 at 11:30:00 GMT-5"
set dateString4 to YYYYMMDD(_date, ".")

on YYYYMMDD(_date, _sep)
   set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, _sep}
   tell _date
       set dateString to (get {its year, text -2 thru -1 of ("0" & (its month as number)), text -2 thru -1 of ("0" & its day)}) as text
   end tell
   set AppleScript's text item delimiters to oldTIDS
   return dateString
end YYYYMMDD

Applescript:

set {year:yyyy, month:m, day:d, weekday:w} to (current date) # Per Nigel on this page.
yyyy
m as number
d
w as text

If you want something more straightforward then use the Satimage.osax or the shell.

# Satimage.osax
set dateStringSIO to strftime (current date) into "%Y/%m/%d"

# Using the shell
set dateStringShell to do shell script "date '+%Y/%m/%d'"

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)