Scripts saved as applications do not run if triggered by Calendar App.

Scripts saved as applications do not run if triggered by Calendar Application that is part of OS11

I have several scripts that send emails at different points of the day. They get triggered by alarms in the Mac OS 11.6 Calendar application (its Version is 11.0 (2811.5.1)).

The problem is that I get errors that the script times out (-1712). (https://postimg.cc/sQNv0g2S) Now if I run it directly by clicking the application it runs fine, all the time. Every time I get the error, I run it manually (double click application) to check and it works.

And next time it should be triggered by Calendar it times out again. Now I have this problem with several scripts that should run.

One thought I had was that it maybe that the computer was asleep (it is a Mac Mini that is a blind server) and did not get the network connection up in time for sending the mail. That is why I put the network check in front of it. The address it pings is the mail server of my ISP. Just to make sure this on is not down either.

Any idea as to how to solve this?

Thanks.

-- check if network is up

repeat
	try
		set pingRequest to do shell script "/sbin/ping -c6 sslout.df.eu"
		if pingRequest contains "0% packet loss" then exit repeat -- This will run the ping until there's no packet loss. 
	on error
		beep
		delay 10
	end try
end repeat
display dialog "Finished checking - Network is up and running" buttons {"OK"} default button "OK" giving up after 6
delay 3

say "OK"

-- end


set theBody to "Text"
set theSubject to "Text"
set theTarget to "<name@yahoo.com>"
tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:theTarget}
		set sender to "<sender@fyahoo.com>"
	end tell
	send newMessage
end tell

delay 2

tell application "System Events"
	set visible of (every process whose visible is true) to false
	set visible of process "Finder" to true
end tell

Model: Mac Mini
Browser: Safari 605.1.15
Operating System: Other

On one hand I’m not qualified to answer the question, but a search suggested something like using a timeout:
eg:

tell application “xxx”
    activate
    with timeout of 3600 seconds
        — do something
    end timeout
end tell

This will override Applescripts default timeout of 2 mins, giving it longer to finish executing that command. This is a plagiarised answer. :smiley:

MM, could work, worth a try, will give it a go with a test today.

PS like your signature.

Are you sure you add open file alarm successfully? I can’t do it either manually.

I can set open file alarm only manually with choosing application. But after few seconds Calendar.app removes this file from event. I checked with event Info

well I don’t see it being removed as it still shows in the Calendar GUI window. or how do you check?

Close event’s Info window and open it again. When I do that, the Info window removes added open file alarm. That means: Calendar.app doesn’t save it (removes it). As I know, the dictionary of Calendar.app states: “Starting with OS X 10.14, it is not possible to create new open file alarms or view URLs for existing open file alarms”.

So, to have open file alarm on new OSX workaround is following: make your calendar with events & open file alarms on some old OSX, copy this calendar to new OSX. Because created on the old OSX open file alarms works on new OSX. You can’t just modify them.

It is still or again possible to create an event with “Open file…” under Big Sur (just tested successfully). Also, the entry in the calendar is preserved again when changes are made. This was different in the meantime, but works again.

Edit: It also works under Catalina (10.15.7). You must always use: alarm → Custom → Open file and then change “Calendar” to “Other…” and select the file.

Yes, I am on Catalina, I can add file choosing “Other…”, but the problem is Calendar.app doesn’t retain this adding. If you understand what I talk about. I guess, the same is with Big Sur

I just tested it again several times. Created a calendar entry that opens an applet that displays a notification. I changed the appointment several times and always the notification was triggered. Also, the file name remains (when I go through “Custom…”) or I can select a new file.

PS: The file was always in my user folder

I remember that it was different (as you described), but it works for me now under macOS 10.15.7 and 11.6 (German).

How you trigger the open file alarm? You have opened Event Info window or it is triggered with closed Info window on the time of event?

Here is the video: http://bunar.de/temp/Catalina-Event-FileOpen.mov
(You have to wait a few seconds until the system time is 19:17)

It looks like a nightmare. I am currently on Catalina 10.15.7. I confirm that the application file is successfully added in exactly the way shown in the video. But after a few seconds it is removed by the Calendars application from the event…

Maybe it’s an iCloud or otherwise synced calendar?
I have only used the Automator calendar for such events. It is now also in iCloud, but it works.
Try using a local calendar.

It looks like it could be that some times when it does not work the machine is asleep or in some sort of rest. not sure if that is it, but my experimenting seems (emphases on seems) to point in that direction.

Could that be? I would find it strange.

The problem is I don’t understand what do you mean with “Automator calendar”. I tested with local calendar and your method doesn’t work on my Catalina 10.15.7. Calendar.app removes added open file alarm.

I admit that it works on Big Sur, I cannot verify. I wonder if there is in the Calendars’s dictionary of Big Sure note like the one I mentioned above regarding open file alarm.

Thanks.

“Automator calendar” means in Automator you can make an alarm that schedules in Calendar. That does not solve the problem either, gives the same faulty result.

@KniazidisR:

With “Automator Calendar” I mean the one that is used or created when creating an Automator Calendar Alarm workflow. In this case, the “Open file feature” is also used.

The video was created with Catalina 10.15.7.

@SW108:

Since you get an error message from the applet, I don’t assume that it has anything to do with KniazidisR’s problem (disappearing settings in the calendar). The script is started, it just fails.

I have tried this once with your script under Big Sur (11.6). Here are my results:

It is not necessary that the calendar app is started. It is also not started when the event is triggered.

The event is not fired when the Mac asleep. Instead, it is triggered when the Mac wakes up again. Since the applet is not started in this case, there can be no error message and therefore I do not assume that this has anything to do with it.
Possibly this behaves differently when Power Nap is activated and then only parts wakes up (e.g. the TaskScheduler for the event - but not other resources), but I could not test that in the time. This would be pretty weird.

Otherwise, it has run without any problems for me so far.

I would recommend you to disable Power Nap once to be on the safe side and include log entries in the script to find the error:


use framework "Foundation"
use scripting additions

set logFileName to "MyScripts.log" -- change if you like

my writeToLog("-------------------")
my writeToLog("Mail-Script started")

repeat
	try
		my writeToLog("Mail-Script network check started")
		set pingRequest to do shell script "/sbin/ping -c6 sslout.df.eu"
		if pingRequest contains "0% packet loss" then
			my writeToLog("Mail-Script network check finished")
			exit repeat
		else
			error "Packets lost"
		end if
	on error errMsg number errNum
		my writeToLog("Mail-Script network check error: " & errMsg)
		delay 10
	end try
end repeat

set theBody to "Text"
set theSubject to "Text"
set theTarget to "<name@yahoo.com>"
tell application "Mail"
	my writeToLog("Mail-Script create new message")
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody}
	tell newMessage
		my writeToLog("Mail-Script add message recipient")
		make new to recipient at end of to recipients with properties {address:theTarget}
		set sender to "<sender@fyahoo.com>"
	end tell
	my writeToLog("Mail-Script send message")
	send newMessage
end tell

my writeToLog("Mail-Script ended")

on writeToLog(newEntry)
	set logFile to (POSIX path of (home directory of (system info))) & "/Library/Logs/" & my logFileName
	set now to current application's class "NSDate"'s |date|()
	set df to current application's NSDateFormatter's new()
	df's setDateFormat:"YYYY-MM-dd HH:mm:ss.SSS"
	set txt to ((df's stringFromDate:(now)) as text) & " " & newEntry & return & linefeed
	set oFullPath to (current application's NSString's stringWithString:logFile)'s stringByStandardizingPath
	set {blnExists, intFolder} to (current application's NSFileManager's defaultManager()'s fileExistsAtPath:oFullPath isDirectory:(reference))
	if blnExists then
		set oData to (current application's NSString's stringWithString:txt)'s dataUsingEncoding:(current application's NSUTF8StringEncoding)
		set h to current application's NSFileHandle's fileHandleForWritingAtPath:oFullPath
		h's seekToEndOfFile
		h's writeData:oData
		h's closeFile()
	else
		(current application's NSString's stringWithString:txt)'s writeToFile:(stringByStandardizingPath of oFullPath) atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
	end if
end writeToLog

You can find the log in the Console app under “Log Reports”. For example, it looks like this:

2021-10-20 18:45:28.682 -------------------
2021-10-20 18:45:28.834 Mail-Script started
2021-10-20 18:45:28.967 Mail-Script network check started
2021-10-20 18:45:34.253 Mail-Script network check finished
2021-10-20 18:45:34.468 Mail-Script create new message
2021-10-20 18:45:35.731 Mail-Script add message recipient
2021-10-20 18:45:36.085 Mail-Script send message
2021-10-20 18:45:36.388 Mail-Script ended

You can add more log entries and maybe find out where the timeout occurs.

Good luck!

Thanks, DB123

I am going to give it a go. Presently I am on the road for a few days so will have time on Saturday PM to engage. The MM is at home, and I am not at the moment.

I did some other testing. I ran an application that is called Jigler. This is keeping the Mac active as it ‘moves’ the mouse every x minutes. This makes no difference to the problem using Calendar to fire the scripts. I get the same errors.

Since at an earlier point I figured the network part of the hardware does not become active (similar along the lines of your thoughts) I added the ping bit. This did not help either.

Next, I installed an application called ‘Schedular’. I did an overnight test with this email and sending it every 5 minutes. If I have Jigler running it sends a mail every 5 minutes, if not it does not send it. So now suspicion is, it is not Calendar or the script, but that indeed some part of the Mac does not wake up.

Now my scripting skill is pretty limited, but is there a way to script the Mac to be fully fit and awake on all levels? I know you can script ‘sleep’. Can you wake it with a script?

As to Power Nap, it is off. And Jigler would also stop it from kicking in. Here is a pic of my ES Panel. https://postimg.cc/njbk2fmJ

Hope somebody out there has the solution, and thanks again everybody for engaging.

Look for “caffeinate”. It’s on board. I use the command e.g. when I copy many and large files via script. If you execute it without parameters in the terminal, it prevents the sleep until you terminate the command with Ctrl+C or close the window.
Not nice, but at least you can test if it’s because of that.