Trigger on sleep?

I’m looking for a way to trigger a macro as the system goes to sleep. What I want to achieve is that a certain app (Scrivener) gets closed as I put the machine to sleep. If it isn’t closed, I get a problem editing the same file on another machine.

Apps like Jetbrains IDE should also always be closed, since they don’t allow multiple instances to be open under a single license.

I could probably define a hot key to close everything and then sleep the machine, but I just know I’ll forget. Old habits are hard to break.

I don’t know if this can even be done. It’s possible the system won’t allow enough time to close these apps, but one can only hope, right?

Edit: just checked it out. OSX does allow for up to 30 seconds delay in going to sleep if an app demands it. That should be more than enough to make such a trigger useful.

Hi Martin

Maybe it is easier to make a macro, which quits the programs you need to make sure are closed, and the set it to sleep.

Like this:

And then use this hotkey or menu item instead of sleep.

Yes, that’s what I did, but I had hoped to do something more elegant. But it works.

I haven't tried it, but something like this might work:

Keyboard Maestro has no sleep trigger, since there is no guarantee the macro would have time to complete if the Mac is going to sleep, and a danger that it would stop the Mac going to sleep.

@Seganku and @JimmyHartington have given some good ideas on alternatives, though neither will help if you just close your MacBook to put it to sleep.

The OSX docs specify that you can delay sleep for up to 30 seconds, which is a lot of time to get things done. So it could be quite useful even without a hard guarantee.

1 Like

Other solutions that may be worth considering are:

  1. System Preferences -> Security and Privacy -> General -> Advanced -> “Log out after N minutes of inactivity.”
  2. Scenario “allows you to run AppleScripts when certain events happen. AppleScripts can be launched on Login, on Logout, when your Mac becomes Idle, no longer is Idle, goes to Sleep or Wakes up or when you hit a special Keystroke.”
    - Then you could launch some AppleScript onSleep like:
    tell application “Keyboard Maestro Engine” to do script “Sleep”’
  3. See this post.
1 Like

I went with creating a hot key to shut down the apps and then sleep the machine. Like crtl-opt-cmd-backspace, which is not too far from the regular opt-cmd-eject.

But the “Scenario” app looks really interesting. Thanks for the tip!