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