Stop Sleep / Screen Dim Mac

Hello, it possible to call a code to stop the mac from sleeping within a window (ie a checkbox value), but more specifically, will this stop the screen from dimming? And then another code to return the back to original state (undo it).

I’ve seen this thread below: And tried both methods (MBS / Caffeinate). I’ve got it compile, and asked chatgpt for advice, but it doesn’t seem to work. Apple website seems to suggest that caffeinate will stop screen dimming. Is it because I am trying to alter states within the app? and not on app.openening?

It’s difficult to debug this at my end, because I am having to compile on windows and then transfer to my mac to run (my macos too old to run XOJO). So if anybody could explain to like I am five how this could be achieved, I would be grateful.

Technically, it is possible to do that, but NOT with native Xojo code.

You need to use NSProcessInfo.beginActivity(options:reason:)

The options you want are NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled

This will tell the system that your app wants the second highest performance (the first is reserved for games) and that your app wants the screen to stay on. However, you need to understand that you’re only telling the system what you want, and it may choose to do something else.

You also need to tell the system when you’re done, you’d be surprised how many developers, even from big brands forget to do this.

You can check that it is working as expected by using Sleep Aid, as this will list out processes that are preventing sleep.

I wrote the declares for this some time ago and included it in my now discontinued App Kit, or you can probably use a certain plugin.

If you’re familiar with Objective-C or Swift, you can also write your own plugin or helper process to do this for you.

1 Like

I’m still (BigSur) using Sam’s IOKitSleepControl modul; if you can get it, then you enable and disable sleep with just one comand.

I found this post from 2013, updated in 2019, which offers a solution but it seems that the function declarations have changed again since then…

Otherwise, look at the MBS example “/MACOSX/IOPMAssertionMBS test” which seems to work.

You can use the shell to call “caffeinate” (see “man caffeinate” in terminal).
There is already an old thread about it: Stopping Mac going to Sleep

You’re over-complicating things. Use the Remote Debugger Desktop application. Just download Xojo on your Mac (you don’t need to actually run Xojo, but need to copy it in your Applications folder) then, go in Xojo’s Folder→Extras→Remote Debugger Desktop and launch “Remote Debugger”. Finally, in the Windows’ Xojo app, you can run/debug remotely (look inside the “Project” menu to set it up for the first time).

1 Like

A clarification: when we talk about preventing sleep, generally speaking, are we talking about the screen or the system, or both?

Keeping the screen awake, prevents System sleep. However preventing system sleep doesn’t keep the screen awake.

The second option is NOT recommended, because modern Macs have no state indicator, and so the user can believe that the Mac is sleeping, when in fact it’s actually doing Machine Learning for Apple…

If you’re interested to know what you Mac gets up to when it should asleep, I wrote an app for that… Sleep Aid - Diagnose and solve Mac sleeping issues

1 Like

Thanks so much everyone for your responses. The MBS OPMAssertionMBS test example was was the perfect solution for me. The remote debugger tip is great, but unfortunately it doesn’t seem to work as my mac is 10.11.6 and it still says it wants 10.14 when debugging remotely even though my app min version is set to 10.11.6 and works directly from the mac.