Do Something If Timer Runs Out...?

I use Shuttie when I want to go to bed and leave my mac doing something for a while.

I figured this might be the kind of thing that one could build in KM. Anyone fancy figuring it out with me? I'm imagining:

Triggering the macro presents you with a prompt like this:

image

Once you hit enter, a window appears on screen with a timer counting down. It has a cancel button on it in case you change your mind, but if the timer runs down, your mac sleeps or shuts down.

I guess this second bit would require some knowledge of custom html prompts, of which I have none! Anyway, just putting it out there...

Here's the prompt pictured above.

Hi, @noisneil. You could possibly use some of the logic in Paste Into Restricted Field. The comments include attribution for the timer. One of the nice features is that the subsequent actions can be aborted if the timer is closed.

1 Like

That's a great start!

System Sleep Timer.kmmacros (41.5 KB)

System Sleep - Timer

System Sleep

One issue I've found with it is that entering a large number of minutes doesn't really work. I entered 90 and the timer maxed out at 30min. Entering 100 gave me 40min.

It'd be really nice to be able to select hours and minutes and have a big cancel button on it, I think. Not sure how difficult the latter would be to do.

I adjusted the Custom HTML Prompt to accommodate hours, minutes, and seconds:

DOWNLOAD Macro File:
Countdown Timer (H-M-S).kmmacros (5.5 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

Brilliant!

I ended up removing seconds from the prompt as I think it's probably overkill, for my needs anyway.

Sleep-Shutdown.kmmacros (20 KB)

Macro screenshot

Sleep-Shutdown - Timer.kmmacros (24 KB)

Macro screenshot

1 Like

Do you happen to know how to set it up so that clicking one button (Sleep/Shutdown) will deselect the other? I'd prefer this to a dropdown list as it's one click, and it's something I've often wondered how to do.

image

Edit: Looks like you can't.

1 Like

Quick workround (unless you want to go the Custom HTML route) would be to have no radio buttons or checkboxes, and instead have 3 buttons -- "Cancel", "Shutdown", and "Sleep"

3 Likes

Yep, Nice!!

As @Nige_S suggested, you can employ multiple buttons to dismiss the Prompt For User Input dialog.

Note also that I was incorrect above: the subsequent actions are not automatically aborted if the dialog is closed. Thus I modified the HTML to set local_TimerStatus to expired if the timer completes (i.e., it is not closed prior to expiration).

DOWNLOAD Macro File:
Sleep-Shutdown - Timer.kmmacros (9.7 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

1 Like

Edit: This isn't actually working for me. When the timer expires, nothing happens... Not sure why; presumably it's working for you, @_jims?

Fantastic! I guess we could get fancy with the timer, but this is good enough for me...

In case anyone who likes messing around with HTML stuff sees this at some point, here's my personal wish list for the HTML timer:

  • Cancel button
  • Title, saying something like: "Time until Sleep/Shutdown" (if one of these options was selected).
  • Mayyyybe the window itself should be nice and big so it's hard to forget you've set a timer to shutdown. Also mayyyybe it could be a bit prettier. Not important, but hey, I'm shallow.
  • Ultra-luxury feature: A second button that allows you to cancel and restart the timer (i.e. retriggers the macro from the start)

Yes, it behaves as expected for me: Pings|Shutdown|Sleep

I'm sure you noticed that I eliminated the dependency on your second macro. BTW, that second macro had the Sleep and Shutdown actions disabled.

Weird. If I put a Display Text in a Window action at the end of the macro, it displays text when the timer expires, but none of the actions in the Switch group trigger.

Yes, nice work!

This was kind of a safety thing. Things like shutdowns don't really want to be triggered accidentally during testing.

1 Like

So, are you saying the local_TimerStatus = expired and local_Action contains the expected value? If so, then I have no idea why the Sleep|Shutdown|Play Sound would not be executing.


I just added this action after the Pause action...

Display Text.kmactions (1.3 KB)

Keyboard Maestro Export

which yielded...

Well, upon further review, I was able to occasionally recreate the behavior: local_TimerStatus not set to expired

I suspect the following version will work. For the Custom HTML Prompt action I deselected Asynchronously and deleted the Pause action. Sorry, I should have done this before.

DOWNLOAD Macro File:
Sleep-Shutdown - Timer [v2].kmmacros (12 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

This version covers all but the third bullet.


2022-05-22 01.44.51


DOWNLOAD Macro File:
Sleep-Shutdown - Timer [v3].kmmacros (27 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

Jim, you clever man!

Works great!!!! Love that it remembers the previous setting too!

:grimacing:

What's the if TriggerValue is "Restart" thing all about? Are you calling the macro externally with a parameter?

1 Like

No, it's there to handle to Reset button, i.e., it launches itself but skips the Prompt For User Input dialog. (Now that I think about it, since I've deselected the Asynchronously option, a slicker way would be to handle this logic exclusively within the embedded JavaScript in the Custom HTML Prompt action. :thinking:)

On a related note, I just updated the above and simplified the first If action as follows:

That simplified will not need to be modified if a hotkey trigger is added to launch the macro.


So, in summary, the Reset button will reset the countdown timer using the Hours and Minutes previously specified in the Prompt For User Input dialog. Contrastingly, the Restart button will cancel the countdown timer and relaunch the shortcut, presumably to change one of the dialog settings.

1 Like

Continuing on my learning curve :smiley:...

This version includes the Reset logic in the embedded HTML/CSS/JavaScript in the Custom HTML Prompt. This simplifies that remainder of the macro.

Also, @noisneil, to address your third bullet—at least partially, I've added some CSS specifications which, of course, can be adjusted to accommodate personal preference. Just for fun, I added a little animation when the Reset button is clicked.


2022-05-23 00.01.28


DOWNLOAD Macro File:
Sleep-Shutdown - Timer [v4].kmmacros (23 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

1 Like

Very cool!