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?
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.
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?
…
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.
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.
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.