katernet / darkmode

Set macOS dark mode and Alfred dark theme at sunset
GNU General Public License v3.0
150 stars 10 forks source link

Compatible with macOS Big Sur? #26

Closed trankien386 closed 1 year ago

trankien386 commented 2 years ago

I used your script on macOS Catalina to have dark mode automatically changing based on my preferred time instead of Night Shift's time. After installing macOS Big Sur, I noticed that the script doesn't work sometimes. Like when i open my macbook at today morning, it doesn't automatically change to light mode.

I looked at the ~/Library/Logs/io.github.katernet.darkmode.log and i see these line:

12/17/21 09:32:52 darkmode: 105:109: execution error: Application isn’t running. (-600)

What do you think?

trankien386 commented 2 years ago

I have set up the time to turn on dark mode automatically at 19:00. I rarely work at 19:00 but today i do and i notice dark mode doesn't automatically turn on. Here is the log again:

12/16/21 13:40:25 darkmode: Error: no such table: solar
12/18/21 05:45:04 darkmode: Error: no such table: solar
12/16/21 13:46:13 darkmode: Error: no such table: solar
12/17/21 09:32:52 darkmode: 105:109: execution error: Application isn’t running. (-600)
12/17/21 09:32:53 darkmode: Error: no such table: solar
12/17/21 10:29:18 darkmode: Error: no such table: solar
12/17/21 10:29:29 darkmode: Error: no such table: solar
12/17/21 10:31:39 darkmode: Error: no such table: solar
12/17/21 10:35:13 darkmode: Error: no such table: solar
12/17/21 10:35:13 darkmode: Error: no such table: solar
12/18/21 07:30:04 darkmode: Error: no such table: solar
12/18/21 22:14:57 darkmode: 105:110: execution error: Application isn’t running. (-600)
12/19/21 07:30:04 darkmode: Error: no such table: solar
12/21/21 07:30:11 darkmode: Error: no such table: solar
12/22/21 07:15:06 darkmode: Error: no such table: solar
12/23/21 16:08:34 darkmode: Error: no such table: solar
12/23/21 16:08:34 darkmode: Error: no such table: solar
12/24/21 16:09:56 darkmode: Error: no such table: solar
12/24/21 16:09:56 darkmode: Error: no such table: solar
12/24/21 18:13:16 darkmode: Error: no such table: solar
12/24/21 18:13:16 darkmode: Error: no such table: solar
12/25/21 06:45:10 darkmode: Error: no such table: solar
12/25/21 19:00:01 darkmode: 105:110: execution error: Application isn’t running. (-600)

I'm still in macOS Big Sur 11.6.1.

katernet commented 2 years ago

I am very sorry for such a belated reply. I haven't been monitoring this project for quite some time; the latest issue was posted in Mar 2020 and I conceded that Apple added Auto dark mode in Catalina.

execution error: Application isn’t running. (-600)

This is the Apple Script failing to invoke System Events, which does exit after inactivity and in theory should re-activate when invoked with Apple Script. It appears to be a macOS bug when it fails to activate, increasingly so in recent macOS updates. I am guessing it could be due to power efficiency updates or sandboxing and application entitlements, especially in Big Sur.

I have a MacBook Air still running High Sierra (yes I am lazy to update) and the dark mode script is still chugging along and working fine, but it actually also has a few -600 errors sporadically throughout the log. This is my secondary machine, maybe it occurs after long periods of sleep?

Other people with this issue https://stackoverflow.com/questions/66251431/why-applescript-not-working-since-updating-mac https://stackoverflow.com/questions/19957268/applescript-fails-with-error-600-when-launched-over-ssh-on-mavericks https://stackoverflow.com/questions/19957268/applescript-fails-with-error-600-when-launched-over-ssh-on-mavericks/24111931#24111931

Some reading if you are interested https://developer.apple.com/forums/thread/669829 https://www.reddit.com/r/applescript/comments/ll0zti/applescript_problem_since_updating_mac/gnue0xy/ https://macscripter.net/viewtopic.php?id=42895

I you have not yet abandoned this script due to my inactivity, you could try the following and let me know if it is any better?

A reboot - yes I know this sounds silly, but it could sort out System Events not activating properly, especially if you just sleep and have a long up time. Please monitor how it goes and if the errors re-appear in the log after a while, then continue with below.

Next you test adding a few lines to the AppleScript to try and fire up System Events before we use it.

They would go between these two lines of code (between osascript and tell application) at line 21 and line 64 in the script.

osascript -e '
tell application id "com.apple.systemevents"

The Apple Script will go in order of severity. First we try simply to activate System Events:

tell application "System Events" to activate

Next to try is launching System Events if not already launched, with a small delay (sleep) to allow time for it to launch:

if application "System Events" is not running then
    launch application "System Events"
    delay 0.5
end if

Next to try is the most severe, to force quit and relaunch System Events, in case it is misbehaving or has become 'stale' or unresponsive:

set app_name to "System Events"
set the_pid to (do shell script "pgrep " & (quoted form of app_name))
if the_pid is not "" then do shell script ("kill -9 " & the_pid)

delay 0.5

tell application "System Events"
    activate
end tell

delay 0.5

Try in order of severity and monitor if the error returns, or just try one of them and see how it goes.