I came here from this question.
I want to disable the sleep prevention of Sleep Control Center as soon as I send my Mac to sleep myself. To do so I have to open the context menu of the menu bar icon, preferably with a right click, as I have set the left click to quickly toggle the state instead of opening the menu. This would be done by an AppleScript which would trigger on a ControlPlane sleep event.
So far I got this code:
ignoring application responses
tell application "System Events" to tell process "Sleep Control Center"
click menu bar item 1 of menu bar 1
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Sleep Control Center"
tell menu bar item 1 of menu bar 1
tell menu 1 of menu bar item 1
click menu item "Deactivate" of menu 1 # THIS DOES NOT WORK
end tell
end tell
end tell
The first part works fine if I disable the quick state toggle (right click wold be preferred, but I read that AST no longer works since Mojave, otherwise this would be the solution). Also "AXShowMenu" does not work.
The second part fails at "click menu item ..." because the index of the above line can not be found. I got this index from UIBrowser.
So I have two Problems:
I came here from this question.
Ten years old, and not so applicable if you're running the latest macOS. OK, I just read you're running Mojave. But, mind you, that post wasn't even that applicable at the time it was written, either. But, massive kudos for doing your research and providing links to it—it may be out-of-date, but it gives an immediate picture of where you're coming from, which is really useful.
preferably with a right click, as I have set the left click to quickly toggle the state instead of opening the menu
Why not set the right click to do this instead of the left click ? 🧐
Also "AXShowMenu" does not work.
It would have been prudent to include your failed attempts at trying to make it work, because, on balance, it's much more likely that your code was at fault rather than the action "AXShowMenu"
. That said, it's certainly not impossible for stuff in AppleScript to not work for no good reason, so...
otherwise this would be the solution
It would be a solution, but it would never be the solution, for the very reason that eventually transpired: third-party tools are not dependable. If there's an in-house way to do something, that's potentially the solution.
The second part fails at "click menu item ..."
You mean here:
tell menu bar item 1 of menu bar 1 tell menu 1 of menu bar item 1 click menu item "Deactivate" of menu 1 # THIS DOES NOT WORK end tell end tell
That's because your references are all fucked up. In the first line, you target menu bar item 1
(which is a descendant of menu bar 1
, as you've correctly coded). In the second line, you're targeting menu 1
, which is a descendant of menu bar item 1
, which is then (apparently) a descendant of menu bar item 1
(again), which descends from menu bar 1
.
See what's happening...?
When you open a tell...
block to target stuff, anything that gets "told" automatically inherits the ascendants being targeted in the tell
clause. Therefore, the above nest of blocks should look like this:
tell menu bar item 1 of menu bar 1
tell menu 1
click menu item "Deactivate" --OR: menu item 1
end tell
end tell
So I have two Problems:
- How to open the context menu of the menu bar item without left clicking it?
- Why does it tell me that it cant find the "Deactivate" menu item?
You're my favourite question-asker in years. You've composed a well-written question, with all the salient details (I don't have to ask for anything extra, apart from the bit about "AXShowMenu"
, which thankfully isn't pertinent to moving forward); the way you explain each point of difficulty is great, with a summary at the end; and you did your research, showed us your research, showed us your attempt based on this research, and included a useful screenshot. I've already +1'd your question, which I would happily do 29-times over if it were allowed.
Why not 30 ? Well, there's one small piece of research you neglected to cover:
So, with that in mind, I grabbed a copy of the app from the App Store, and poked around in the scripting definition file that lives in the Resources folder of the application package contents. I haven't actually run the application and don't intend to for the time-being, as I will delete it immediately after posting this (FYI, I use a program called Amphetamine which is free without ads, is also AppleScriptable, and appears to have a whole host of trigger events that either rivals or surpasses SCC).
You stated that:
I want to disable the sleep prevention of Sleep Control Center as soon as I send my Mac to sleep myself
and that:
This would be done by an AppleScript which would trigger on a ControlPlane sleep event.
Your AppleScript's purpose was purely and simply to turn off sleep prevention. It turns out there's an AppleScript command in the scripting definition file called turn off
, whose description states:
"deactivate sleep prevention"
Here's the AppleScript code that should invoke this command for you:
tell application id "com.3bitlab.AntiSleep" to turn off
Now, I'm pretty sure that replaces your entire script, and achieves your objective by not solving the problems you thought you had. However, for completeness, there's also a turn on
:
tell application id "com.3bitlab.AntiSleep" to turn on
This command also comes with a series of options to set the sleep prevention mode. The four available options are (only one can be chosen in any single invocation of the command):
tell application id "com.3bitlab.AntiSleep" to turn on mode system/display/lid/disk
and here's a description of what each do:
mode |
Description |
---|---|
system |
prevent system sleep |
display |
prevent display and system sleep |
lid |
prevent system sleep when lid close |
disk |
prevent external disks sleep |