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

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.

Thanks DB123.

I have found Amphetamine this morning and loaded that. It is testing presently. Do you know about it? it seems to have loads of functions.

Or is caffeinate a shell command or the likes?

Do you know how both compare?

Not sure what you mean here. included it in the AppleScript to lounge it at the start of the script?

Close what window?

Yes, but for me an app for this function was too much, since it is already part of the system.

Yes, “caffeinate” is a shell command. Just run it in the terminal.

The terminal window.

Run this in the terminal:

and you get: “usage: caffeinate [-disu] [-t timeout] [-w Process ID] [command arguments…]”. Without parameter it runs endlessly.

You can also start it with AppleScript:


	do shell script "caffeinate </dev/null &>/dev/null &"

but then you can only stop it via the Activity Monitor. But maybe that would be a way to start it at system startup.

— Edit —
Of course, it also works with a Stay Open applet. Whether it makes sense is another question.


use scripting additions
property pid : missing value

on run
	set my pid to do shell script "caffeinate </dev/null &>/dev/null & echo $!"
end run

on quit
	do shell script "kill " & my pid
	continue quit
end quit

The interesting thing about the caffeinate command is that you can use it to prevent the mac asleep while another command or script is running.

PS: The “</dev/null &>/dev/null &” part prevents the script from blocking at this point.

OK here i am again.

Powernap is disabled. Makes no difference.

I will test today and see if i see anything in the log.

I am now also talking to Apple. Was relutant to do this as it is always a matter of having hours of phone time. Today I needed to be connected 4 times to different people but got one who seemed knowledgeable to than lose the connection. but he emailed a possible solution we are going to try. Will keep you informed.

Apple said they send mail with suggestions, nothing came, need to phone again.

I did a timeout as suggested and it makes no difference. I am getting a bit frustrated that I can not get this to work. Scripts run fine when manually activated but not when trigged. Is this a OS11 bug? I do not know.

I also tested it with a application called Scheduler (https://www.macscheduler.net – a versatile app by the way and free, how the developer does it I can not say, but lovely).

Has anybody any idea as to how to proceed? I hope so.

Thanks for this db123. I am not a AS champion but think I can follow the thinking here. I did not know that you could do this and how to do it.

I am back at the machine and will start to experiment today. Hopefully I see something in the logs that helps.