"use scripting additions" hangs script app

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property holidays : {date "Monday, May 29, 2017 at 12:00:00 AM", date "Tuesday, July 4, 2017 at 12:00:00 AM", date "Monday, September 4, 2017 at 12:00:00 AM", date "Thursday, November 23, 2017 at 12:00:00 AM", date "Monday, December 25, 2017 at 12:00:00 AM", date "Monday, January 1, 2018 at 12:00:00 AM"}

logToConsole given theString:"Update TIAA Executed."

if weekday of (current date) is in {Saturday, Sunday} then
	logToConsole given theString:"Weekend Day Detected and TIAA script not run."
	return
end if

if (current date) is in holidays then
	logToConsole given theString:"Holiday Detected and TIAA script not run."
	return
end if

logToConsole given theString:"TIAA Total Begun."
tell application "Numbers"
	activate
	tell table 1 of sheet 1 of document "Finances TIAA and BofA"
		set theCell to first cell of column "R" whose value is missing value
		set value of theCell to value of cell "E5"
		set format of theCell to format of cell "E5"
		logToConsole of me given theString:"TIAA Total Completed."
		set value of cell "Symbol Date and time of last script run" to (current date)
		set value of cell "Close Date and time of last script run" to (current date)
		logToConsole of me given theString:"TIAA Date Stamp Updated."
	end tell
end tell

to logToConsole given theString:aMessage
	set theString to "logger -t TIAADaily" & space & quoted form of aMessage
	do shell script theString
	return
end logToConsole

Hello,
The above script works fine if run from Script Debugger, but when double-clicked in the Finder (in the User Library /Scripts/Applications/Numbers), or run with a launchd plist, it errors out and the message is :

Can’t get every scripting addition of file “…Library:Scripts:Applications:Numbers:Update TIAA 7 PM.app:”.

Also, on some attempts, the error is:
current date
privilege violation occurred: errAEPrivilegeError -10004

Never have seen this before. Anyone can help?

Hi.

I don’t know what’s causing the problem, but you don’t need the ‘use’ commands at all here. There’s nothing in the AppleScript code which won’t work on systems dating from long before ‘use’ was invented and if you don’t use the ‘use AppleScript version xxx’, you won’t need ‘use scripting additions’ either.

I can tell you that (current date) will never be in holidays unless the script’s run at the exact time of one of those dates in the year specified.

Gulp. Thanks for the information Nigel - I’ll try running it without the “use” lines on Tuesday. (Script Debugger helpfully puts them in).

I guess this gets back to the old “How to remove the time from a date object”, eh? It’s been a while.

So any tip on the best way to extract a date string in the form dd/mm/yyyy (or yy) from a date object? I will change the holidays myself each year (I can’t find anything scriptable in the Calendar app that identifies a holiday, although the app itself cheerfully displays all holidays).

If there is no way to do it, I will just have to see if the daily total is the same as the day before and assume we are on a holiday.

If your local setting is dd/mm/yyyy (as it is here in France) you may use :

set shortDate to short date string of (current date)

If you want that your script may work with other settings, you may use :

tell (current date) to set shortDate to text -2 thru -1 of ((100 + (its day)) as text) & "/" & text -2 thru -1 of ((100 + (its month)) as text) & "/" & text -4 thru -1 of ((10000 + (its year)) as text)

I know that we may get the same result thru do shell script but the vanilla code posted above is faster.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 27 mai 2017 20:56:58

Thanks Yvan. Really helpful. And works to get a comparable date.

It seems that a property can’t be an expression, though - so to make the list of holidays, would I have to loop through the property list of dates and make another list of strings by applying short date string?

I don’t know how you get the original date values.

Maybe you may define directly

property holidays : {"29/05/2017", "04/07/2017", "04/09/2017", "23/11/2017", "25/12/2017", "01/01/2018"}

If you really need to get dates, you may use :

property holidays : {}

set holidaysDates to {date "lundi 29 mai 2017 à 12:00:00", date "mardi 4 juillet 2017 à 12:00:00", date "lundi 4 septembre 2017 à 12:00:00", date "jeudi 23 novembre 2017 à 12:00:00", date "lundi 25 décembre 2017 à 12:00:00", date "lundi 1 janvier 2018 à 12:00:00"}
repeat with aDate in holidaysDates
	tell aDate to set end of my holidays to text -2 thru -1 of ((100 + (its day)) as text) & "/" & text -2 thru -1 of ((100 + (its month)) as text) & "/" & text -4 thru -1 of ((10000 + (its year)) as text)
end repeat
my holidays

or

property holidays : {}

property holidaysDates : {date "lundi 29 mai 2017 à 12:00:00", date "mardi 4 juillet 2017 à 12:00:00", date "lundi 4 septembre 2017 à 12:00:00", date "jeudi 23 novembre 2017 à 12:00:00", date "lundi 25 décembre 2017 à 12:00:00", date "lundi 1 janvier 2018 à 12:00:00"}

repeat with aDate in my holidaysDates
	tell aDate to set end of my holidays to text -2 thru -1 of ((100 + (its day)) as text) & "/" & text -2 thru -1 of ((100 + (its month)) as text) & "/" & text -4 thru -1 of ((10000 + (its year)) as text)
end repeat
my holidays

I wish to add that I dislike to store variables values in a property because it will be saved in the script on disk.

So for my use, I would put the property storing the strings in a script object.

property holidaysDates : {date "lundi 29 mai 2017 à 12:00:00", date "mardi 4 juillet 2017 à 12:00:00", date "lundi 4 septembre 2017 à 12:00:00", date "jeudi 23 novembre 2017 à 12:00:00", date "lundi 25 décembre 2017 à 12:00:00", date "lundi 1 janvier 2018 à 12:00:00"}

script o
	property holidays : {}
end script

repeat with aDate in my holidaysDates
	tell aDate to set end of o's holidays to text -2 thru -1 of ((100 + (its day)) as text) & "/" & text -2 thru -1 of ((100 + (its month)) as text) & "/" & text -4 thru -1 of ((10000 + (its year)) as text)
end repeat
o's holidays

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 28 mai 2017 10:59:56

Hi Yvan.

The way you’ve specified the script object in the run handler, it gets compiled into the script, rather like a property in its own right. So it and its own properties will be saved anyway.

It needs to be instantiated at run time and assigned to a local variable. This is one way to do it:

property holidaysDates : {date "lundi 29 mai 2017 à 12:00:00", date "mardi 4 juillet 2017 à 12:00:00", date "lundi 4 septembre 2017 à 12:00:00", date "jeudi 23 novembre 2017 à 12:00:00", date "lundi 25 décembre 2017 à 12:00:00", date "lundi 1 janvier 2018 à 12:00:00"}

local o
script
	property holidays : {}
end script
set o to result

repeat with aDate in my holidaysDates
	tell aDate to set end of o's holidays to text -2 thru -1 of ((100 + (its day)) as text) & "/" & text -2 thru -1 of ((100 + (its month)) as text) & "/" & text -4 thru -1 of ((10000 + (its year)) as text)
end repeat
o's holidays

Thank you Yvan. I decided to just use text versions of the holidays, and then convert (current date) to text as you indicate, for comparison. As you stated, it requires that the text list be exact, leading zeros etc. Seems to be a longstanding shortcoming of AppleScript, not to be able to get “date only” dates (without times).

Best wishes, thanks again,
Johnny

Oh, and the hangs were the result of a problem in Script Debugger - namely saving a script with debugging enabled.

Thank you also Nigel.

You may use an alternate scheme which doesn’t use dates as strings.

# CAUTION, you must edit the dates to fit your local settings.
property holidaysDates : {date "lundi 29 mai 2017 à 12:00:00", date "mardi 4 juillet 2017 à 12:00:00", date "lundi 4 septembre 2017 à 12:00:00", date "jeudi 23 novembre 2017 à 12:00:00", date "lundi 25 décembre 2017 à 12:00:00", date "lundi 1 janvier 2018 à 12:00:00"}

set aDate to current date
log aDate (*date dimanche 28 mai 2017 Ã  19:12:51*)
aDate is in holidaysDates
log result (*false*)

# build a date
set aDate to "12/12/12"
set aDate to date aDate
set month of aDate to 7
set day of aDate to 8
set year of aDate to 2017
set time of aDate to 12345
log aDate (*date samedi 8 juillet 2017 Ã  03:25:45*)
aDate is in holidaysDates
log result (*false*)

set day of aDate to 4
log aDate (*date mardi 4 juillet 2017 Ã  03:25:45*)
aDate is in holidaysDates
log result (*false*)

set time of aDate to (12 * 60 * 60) # 12:00:00
log aDate (*date mardi 4 juillet 2017 Ã  12:00:00*)
aDate is in holidaysDates
log result (*true*)

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 28 mai 2017 19:13:59

I found that I had to set the time to zero to get 12:00:00 AM. (12 * 60 * 60) sets it to 12 PM (noon).

But now that I found a simple way to stop worrying about the time (i.e., by setting it to zero), I will see if I can simplify this thing.

Thanks

Oops
I forget - quite always - that 12:00:00 AM is mignight not noon.
Alas, I’m not the only one.
When I run :

"lundi 29 mai 2017 Ã  12:00:00 AM"
date result

I get :
date “lundi 29 mai 2017 Ã 12:00:00”
exactly what I get if I run :

"lundi 29 mai 2017 Ã  12:00:00 PM"
date result

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 28 mai 2017 21:14:19