newmarcel / KeepingYouAwake

Prevents your Mac from going to sleep.
https://keepingyouawake.app
MIT License
5.62k stars 223 forks source link

Toggle on and off via AppleScript #43

Open drewbarontini opened 8 years ago

drewbarontini commented 8 years ago

Hello!

Previously, with Caffeine, I was able to use the following AppleScript to toggle it on and off via the keyboard:

tell application "System Events"
    if exists (process "Caffeine") then
        tell application "Caffeine"
            if active is false then
                turn on
            else
                turn off
            end if
        end tell
    else
        tell application "Caffeine" to activate
    end if
end tell

When trying that with this application (subbing "Caffeine" with "KeepingYouAwake"), it doesn't work. Using the Script Editor application, it seems that the active variable is not present. As far as I can tell, there aren't any available actions when using the Script Editor Dictionary, but I thought I'd confirm here.

newmarcel commented 8 years ago

You are right, I should probably integrate some scripting definitions to be a good Cocoa and AppleScript citizen.

I'll do some research if it is easily possible to ship basic AppleScript support in the next release. I'll report any findings here.

Thanks for reminding me :grinning:

drewbarontini commented 8 years ago

Awesome! Thank you, sir :+1:

newmarcel commented 8 years ago

I'm sorry for the delay, but I became absolutely discouraged from developing this feature when I saw how these AppleScript APIs and scripting definitions should be implemented 😭.

I'll have a look at this year's AppleScript-related WWDC sessions to see if there is anything new in there to make my life easier implementing scripting support.

I'll update this issue when I've gathered new insights into AppleScript. Thanks for your patience!

marcphk commented 6 years ago

Just to add - I would very much like this too if it's something that's still on the cards.

tsankuanglee commented 5 years ago

Or is there a way to do it in bash (some command line tool)? Having an API-like service can open up possibilities such as user-defined scripts (therefore a lot of custom features can be offload to user scripts), control from the touch bar, etc.

AppleScript can then call the bash script (or some command line tool) to achieve the same thing.

babul commented 2 years ago

My use case was I wanted to reset the countdown to 5 hours via a bash script, which is how I got here.

To workaround not being able to AppleScript it, I just set the default activation period to 5 hours, then close and reopen the app from Bash. Easy peasy!

AlecRust commented 1 year ago

Is there still not a simple way to toggle KeepingYouAwake from the command line?

I've been using Lungo only because it has a CLI for this.

newmarcel commented 1 year ago

@AlecRust Since version 1.3.0 you can use URL schemes to control KYA from the command line, e.g.

open "keepingyouawake:///activate"
open "keepingyouawake:///deactivate"
open "keepingyouawake:///activate?minutes=5"

(please note the quotes when using zsh)

Is that insufficient for your use case? If that is the case I would even recommend using Apple's caffeinate command line tool directly.

AlecRust commented 12 months ago

Oh that's great, thanks!

alexraileanu commented 11 months ago

it wasn't entirely obvious to me but looking thru the source code, i found out that there is also

open "keepingyouawake:///toggle"

to, you guessed it, toggle activation state.

RX4NT9UP commented 10 months ago

FYI on 1.6.5 at least to make this work consistently I had to do open “keepingyouawake:///deactivate” && open “keepingyouawake:///activate”