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

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.

Thank you I did not know how to get it to write to the log. My log looks similar. And you posting it helps me to see I did it right. Thanks again.

it all looks a bit complex to me and I do understand some of it, the block at the end is in places a bit of a mystery to me, but I get the gist I think. I am running a test now as I am back at the machine (same room). It is running on an hourly bases and I hope it catches the problem.

DB123.

you wirte at the start of your script

‘’

use framework "Foundation"
use scripting additions

is this only for the logging or is that useful to do anyway.

I ask as have seen this in other scripts on the WWW. before, like this one:

use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit"
use scripting additions

Could this be part of my problem? And if so what version of AppleScript should be used in OS11? I see that Script Editor is now in version 11. But can not find a AppleScript application like in the olden days.

Yes, the Foundation framework is required for logging (AppleScriptObjC). It is not relevant to your code.

No. I don’t think so. The specification is for a minimum version:


use AppleScript version "2.5"
get version of AppleScript
--> 2.7

2.7 is the AppleScript version of Big Sur.

Thanks.

FMI, how do you find out? Where does it hide?

Skripteditor → About Skripteditor

aha so simple

Thanks for this db123 and other answers.

The test finally did not work with the test script you suggested and so I have an error report. And it does not help me. All I see is that it stopped after checking the network and before or while Mail was firing up.

Are there further test steps one could add to see more?

Here it is:

2021-10-31 20:48:59.060 -------------------
2021-10-31 20:48:59.083 Mail-Script started
2021-10-31 20:48:59.100 Mail-Script network check started
2021-10-31 20:49:04.186 Mail-Script network check finished
2021-10-31 20:49:14.266 Mail-Script create new message
2021-10-31 20:49:14.583 Mail-Script add message recipient
2021-10-31 20:49:14.644 Mail-Script send message
2021-10-31 20:49:17.004 Mail-Script System Events
2021-10-31 20:49:17.016 Mail-Script ended

2021-11-01 13:30:00.801 -------------------
2021-11-01 13:30:00.944 Mail-Script started
2021-11-01 13:30:00.960 Mail-Script network check started
2021-11-01 13:30:06.039 Mail-Script network check finished

the first one is a working one the next one is a hung one with the box popping up “AppleEvent timed out”.

Any ideas as too how to proceed?

Could it be a security setting somewhere?

The error seems to occur when accessing the mail applicatiion.

Change the script in the following section and create a log again:


…
set theBody to "Text"
set theSubject to "Text"
set theTarget to "<name@yahoo.com>"

my writeToLog("Mail-Script call test application")
tell application "TextEdit"
	get version
end tell

my writeToLog("Mail-Script call mail application")
tell application "Mail"
	my writeToLog("Mail-Script create new message")
…

This can be used to check if the problem is specific to the mail application, or if all access to other applications is a problem.

Great. Will give it a go tomorrow when back at the machine, and thanks!

Hi db123.

I adjusted the script as you suggested and it now flips out on starting TextEdit.

here is the log.

2021-11-02 14:51:01.074 -------------------
2021-11-02 14:51:01.094 Mail-Script started
2021-11-02 14:51:01.111 Mail-Script network check started
2021-11-02 14:51:06.171 Mail-Script network check finished
2021-11-02 14:51:16.203 Mail-Script call test application TextEdit start
2021-11-02 14:51:16.223 Mail-Script call test application TextEdit end
2021-11-02 14:51:18.241 Mail-Script call test application Console start
2021-11-02 14:51:18.249 Mail-Script call test application Console end
2021-11-02 14:51:20.518 Mail-Script System Events
2021-11-02 14:51:20.531 Mail-Script ended
2021-11-02 15:51:01.372 -------------------
2021-11-02 15:51:01.392 Mail-Script started
2021-11-02 15:51:01.409 Mail-Script network check started
2021-11-02 15:51:06.485 Mail-Script network check finished
2021-11-02 15:51:16.512 Mail-Script call test application TextEdit start
2021-11-02 15:51:16.532 Mail-Script call test application TextEdit end
2021-11-02 15:51:18.548 Mail-Script call test application Console start
2021-11-02 15:51:18.558 Mail-Script call test application Console end
2021-11-02 15:51:20.884 Mail-Script System Events
2021-11-02 15:51:20.901 Mail-Script ended
2021-11-02 16:51:00.602 -------------------
2021-11-02 16:51:00.621 Mail-Script started
2021-11-02 16:51:00.637 Mail-Script network check started
2021-11-02 16:51:05.762 Mail-Script network check finished

as you see it ran twice and then failed in the same spot.

Here is the script I used.


-- start logging

use framework "Foundation"
use scripting additions
--start log stuff
set logFileName to "MyScripts.log" -- log stuff change if you like

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



-- check if network is up

repeat
	try
		my writeToLog("Mail-Script network check started") --log stuff
		set pingRequest to do shell script "/sbin/ping -c6 sslout.df.eu"
		if pingRequest contains "0% packet loss" then -- This will run the ping until there's no packet loss. 
			my writeToLog("Mail-Script network check finished") --log stuff
			exit repeat
			
			beep
		else
			error "Packets lost"
			delay 10
			error "Packets lost"
		end if
	on error errMsg number errNum -- log stuff
		my writeToLog("Mail-Script network check error: " & errMsg) -- log stuff
		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 ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
set theSubject to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
set theTarget to "<name@gmx.net>"

my writeToLog("Mail-Script call test application TextEdit start")
tell application "TextEdit"
	get version
	my writeToLog("Mail-Script call test application TextEdit end")
end tell

delay 2

my writeToLog("Mail-Script call test application Console start")
tell application "Console"
	get version
	my writeToLog("Mail-Script call test application Console end")
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
	my writeToLog("Mail-Script System Events") -- log stuff
end tell

-- more log stuff

my writeToLog("Mail-Script ended")



on writeToLog(newEntry) -- log stuff
	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

-- end log stuff


So strange it runs twice and then flips out. All the other scripts do this, they run several times and next the fail. Any idea?

What is this???


…
display dialog "Finished checking - Network is up and running" buttons {"OK"} default button "OK" giving up after 6
delay 3

say "OK"
…

I have assumed so far that you have used the script posted by me. In this I had removed display dialog and say “OK” etc. because something like that (UI) has no place in an automatically executed script! That’s what the log is for!

And the do shell script commands are also new (for me).


set theBody to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
set theSubject to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")

If the log is to make sense and you want to know up to which step the script runs, then you must also log every important step:


my writeToLog("Mail-Script do shell script theBody")
set theBody to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
my writeToLog("Mail-Script do shell script theSubject")
set theSubject to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
my writeToLog("Mail-Script do shell script end")

PS: Check if you have signed the applet for run locally. The selection is available when you export the script as applet.

first off, how do you do this? I see no option to do so when I save the script as an applet.

I will tomorrow incorporate your suggestion. Now away from the machine.

I had written:

I know so I have the script, do a save-as, select Application, and next? Where is this option? How to activate?

here a pic of the window: https://ibb.co/GvHSKYv

Use “Export” instead of “Save as”.

I did not know that. Thanks.

I have implemented it and am running the following test script.


use framework "Foundation"
use scripting additions

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

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

my writeToLog("Mail-Script call test email info")
set theBody to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
set theSubject to ("MyScripts.log - ") & (do shell script "date '+%d-%m-%Y - time %H-%M-%S'")
set theTarget to "<me@email.com>"
my writeToLog("Mail-Script call test application email info end")

my writeToLog("Mail-Script call test application TextEdit start")
tell application "TextEdit"
	get version
	my writeToLog("Mail-Script call test application TextEdit end")
end tell

my writeToLog("Mail-Script delay start")
delay 2

my writeToLog("Mail-Script delay end")

my writeToLog("System Event start")
tell application "System Events"
	set visible of (every process whose visible is true) to false
	set visible of process "Finder" to true
	my writeToLog("System Events end") -- log stuff
end tell



-- more log stuff


on writeToLog(newEntry) -- log stuff
	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

-- end log stuff


For my information, is this last bit of code needed? And if so what does it do?

on writeToLog(newEntry) -- log stuff
	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

More info. I ran the above script automatically every hour for 12 hours. no errors. So I think I can assume it is working since I did get errors before after 2 or 3 runs.

What would you suggest is the next step?