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

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?

Add your email code again like this:


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

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

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 "<target@yahoo.com>"
my writeToLog("Mail-Script call test application email info end")

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@yahoo.com>"
	end tell
	my writeToLog("Mail-Script send message")
	send newMessage
end tell

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

my writeToLog("Mail-Script ended")

-- 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

Don’t forget to sign the applet for local run and if everything works, then finally remove the log entries.

DB123
did you see my personal message?

Thanks for fixing.

I am now testing emails incorporating your suggestions. so far so good, but only tested 2.

it all seems to work since one day. Thanks again.

I have some Automator workflows, can one sign those too? On my machine that option is greyed out.

Automator workflows should work without signature or are signed automatically.

I did not know that they sign automatically. Thanks.