Change brightness?

I am looking for macros that:

  1. Display: Change the display brightness from XXX nits to YYY nits and/or can load display press as I have different calibrations for day / night. I know that there are KM actions for brightness but they do not specify the increment and, from what have read, they are monitor dependent!
  2. Screen Saver: Disable the screen saver when the camera is use and/or when on a Teams video meeting, neither of which I know how to detect. The problem is that I use Teams within Parallels (and please, do not get me started on Teams in macOS) and there is no (working) setting to disable the screen saver when on a Teams call. Workarounds are also welcome.

Apologies in the event that the above exists as I looked but could not find any.

You can't -- unless you also have a light meter and a way of reading the level back into your Mac! Otherwise you're stuck with "from 0 to 100% of display's available brightness".

If you want to set by percentage, try the brightness utility -- note the caveats on the GitHub page.

Otherwise, a lack of Apple public APIs makes this... difficult. The KM Actions work the same as the hardware keys, changing brightness by (for me, iMac with built-in Retina) +/-6.666% per invocation.

Which leaves UI manipulation using AppleScript and System Events. You could pop Control Centre and set the brightness slider there -- this works for me but you may have to play with the "path" to target the slider:

tell application "System Events"
	tell process "Control Centre"
		tell menu bar 1
			tell item 1 of (every menu bar item whose description is "Control Centre")
				click
				delay 0.1
			end tell
		end tell
		tell slider 1 of group 1 of group 1 of window 1
			set value to 0.3
		end tell
	end tell
end tell

In a Windows VM window, or in Coherence mode?

I thought of this but it is a no go for me.

I calibrated my monitor and load d number of presets, all of which differ only in their NIT settings. The result is that the brightness slider is greyed out.

Coherence mode.

If you can load them manually I'm sure you can find a way for KM to automate that process, whether by script using System Events in a similar fashion to the above or via a sequence of KM "Press a Button" or "Click on Found Image" Actions.

For Teams -- the easiest (Only? I hope not!) way is test if the "Camera in use" icon is showing in the menu bar. This will return 0 if the camera is not in use:

tell application "System Events"
	tell process "Control Centre"
		tell menu bar 1
			return count of (every menu bar item whose description is "Audio and Video Controls")
		end tell
	end tell
end tell

...which is handy, because KM treats 0 as false and anything_else as true. That does assume you're sharing the camera through macOS to Parallels, not somehow bypassing macOS and connecting directly to the VM.

You might be able to do something with the window name, since you are using Coherence. There are only a limited number of Teams windows with only one | character in their name, for example, so if there's a a window with only one | and it's not one of the known set you're probably on a call.

If either of those detection methods work for you, you can turn the screensaver off with

defaults -currentHost write com.apple.screensaver idleTime 0

and turn it back on with

defaults -currentHost write com.apple.screensaver idleTime 300

...where 300 is the idle time in seconds before the screensaver kicks in (so 5 minutes).

Or you may prefer to caffeinate -- especially if you're on a Managed Device and your IT team enforce a screensaver idle time! caffeinate is a built-in utility:

caffeinate -i -t 300

...will set a "not idle" assertion (-i) for the next 300 seconds (-t).

For both, I'm thinking a macro in a Group that's only "available" when Teams is running and with a "Periodic" trigger that fires every couple of minutes -- if you are in a call then it either turns the screensaver off or caffeinates for slightly longer than your trigger interval, if you aren't it runs the screensaver on again or doesn't caffeinate.

Unfortunately Teams test calls are limited to 15 seconds so I can't really test!

@Nige_S Appreciate the awesome ideas! Something for me to work towards. Will keep you posted.

PS. I wish I had your AppleScript knowledge and skills!!

In case there's any confusion -- defaults and caffeinate are both shell utilities, not AppleScript commands.

You don't need to know this stuff -- you only need to know it's there and then how to search the web to find it! Ironically, such a search will often lead you back to a post on this Forum...

It does help that most of my users have already finished for Xmas and it's really quiet here :wink:

All good with me.

I am finishing up today / tomorrow and will try to get through some of this,

I know that CHatGPT + searching the web is very helpful BUT I do not only want to build the macros, I want to learn the functionality, syntax, etc. so it is easy and fast to do!

Really do appreciate the help.

A noted in title, one of the macOS that I wanted to build woful change the display presets on main display (an Apple Studio Display) at sunrise to sunset.

I had three initial challenges i) how to calculate sunset and sunrise as there is no such trigger (should there be) ii) how to change teh presents (i.e., AppelScript, Keyboard Maestro through control centre, Keyboard Maestro through System Settings, etc.).

I tried all three, failed at two and succeeded on one that being through System Settings. I tried to use a few "tricks" at least for me in them by including Move Cursor action with both light and dark images and ensures the macro does not fail when the action that is in the wrong appearance is fired, is this proper technique?

The two macros (which I have included) are identical other than:

  1. The trigger times (one is sunrise, one is sunset)

  2. The preset changes (i.e., sunrise → 300 nits, sunset → 100 nits)

Would like some assistance / input with the following tweaks:

  1. Is there a better way to calculate the TimeTriggerSeconds and, if yes, what is it?

  2. Is there a better / more efficient way to structure write teh macro

  3. And most importantly, the positioning / sizing of the Parallel's windows that are running in Coherence mode on the Studio Display monitor (monitor whose display preset is being changed) are moved and resized. What would be the best way to determine which such windows exists and move and resize those windows (i.e., this would really polish the macro).

  4. And still under the heading of important, how do I build in a test / conditions that determine a) if main monitor is not the Studio Display monitor then the macro does exits at that time; and

b) if the main monitor is connected and between the trigger time and the sunrise / sunset that the macro still fires (i.e., is that through a USB connected trigger noting I would need to find determine the device name)?

Looking forward to your feedback.

THANKS TO ALL WHO MADE THIS POSSIBLE!!!

The macros are a tad long so I have excluded screenshots but here they are:

_JBC Display Presets Macros.kmmacros (319.2 KB)

Why are you calculating it? You know what it is -- 14,400 for the sunrise macro, 57,600 for sunset -- so why not store it as a constant? Or are you going to be frequently changing the cron rule and are afraid you might forget to change the constant to match?

Have you seen there's an "Open a System Preference Pane" Action?

image

If you do want a "calculated" TimeTriggerSeconds, I don't think there's an ICUDateTime option for "seconds in a day" -- but there is one for "milliseconds in a day" so:

Whether that's any better than your method is another matter!

You're always doing the second in each pair of image detections, even when the first matches -- better to use either an "If... Then... Else" or "Try/Catch".

You can save a couple of Actions (and increase reliability) by using the "Quit Application" Action instead of keystroking System Settings away:

image

The important bit is "Coherence mode" -- IIRC, KM will treat each Windows app just like a "normal" macOS app, complete with getting and setting window frames, getting window titles, and so on. Are they not working for you?

Under what circumstances is it not the main monitor? Do you actually case about it being the main monitor, or just whether it's connected? Do you ever connect an external display that's not the Studio Display? That'll help decide your test(s).

Those answers will also determine whether "Display Layout Changed" is a suitable alternative trigger.

Ahhh, I am calculate it because sunrise and sunset vary by hours where I live (i.e., sunset is current around 4:30 PM whereas it July can be as late as 9:05 PM). This way the macro is good the entire year.

I had not. I will adjust both macros according. Thank you!

I will incorporate your method, it is more elegant.

I will incorporate this in the next iteration.

[/quote]
You can save a couple of Actions (and increase reliability) by using the "Quit Application" Action instead of keystroking System Settings away:

image
[/quote]

I did not know that existed.

I will incorporate it in the next version!

Agree, you are correct and yes they are.

The issue is that not all the Parallel windows are on the main screen. The issue / questions is i) how to determine which are on the Studio Display screen (i..e, there is a finite set of 8 applications) and ii) reposition the one they are to one of my Window Move and Size macros.

Hope that helps?

I do care whether the Studio Display is the main monitor because:

  1. If I am travelling and on my laptop the Studio Display presets look watched out on my MBP. I do not want to load the Studio Display display presets ever in this case.

  2. If I am at my external office then there are two monitors neither of which is a Studio Display and, for similar reason I do not want the Stuio display presets to load.

Ideally we should find a way to determine whether the Studio Display is present but failing that, Is there a way to condition the macros on being connected to my home wifi network as that would solve this problem and ,if yes, how?

Thank you.

@Nige_S

Apologies for jumping on before you have a chance to respond to teh above but attached are revised macros which incorporate the changes / efficiencies you suggestd.

Much thanks!

On to the remaining refinements and next macro!!

_JBC Display Presets Macros.kmmacros (487.6 KB)

@ALL:

I have changed the title of this thread to Change Brightness and moved / started a new thread on Disabling Screen Savers to try to keep the threads more focused.

I trust that is okay and makes sense.

Wrong calculation! TimeTriggerSeconds is the time the macro was triggered, is a constant (unless you change the cron rule), and is 04:00 or 14,400 seconds for the sunrise macro or 16:00 (57,6000 seconds) for the sunset macro.

See the other thread for more and for why it's not wrong to derive/calculate the time as long as you know why you are doing it.

Main has a specific meaning here. So the question is -- do you care if it is the Main screen, if it's connected regardless of whether it is Main, something else?

I don't have a Studio Display, but it's an USB hub so you should be able to use the "USB Device" Condition to test for it and the "USB Device" Trigger to fire off the macro outside of your cron times. There are other methods, but that'll be both most efficient and easiest.

But you could also use screen counts -- from your description there will only be one screen (internal) when you are travelling, three at the office (two external and one internal), and two only when you have the Studio Display connected. Not as precise as using "USB Device", but could do at a pinch.

First you have to define "on the Studio Display screen". Would that be the top-left corner of the window, the mid-point of the window, something else?

If your Studio Display is actually your Main then the top-left corner of the screen has the coordinates 0,0 (that alone may be enough, or you can get other values with the SCREEN() Function). You can get the frames of your Coherent windows. Then it's just maths to work out if a window is "on the Main screen" and position/size as appropriate.

Agreed, on all point.

I initially setup the macro hoping that the trigger time could be set to be sunrise and sunset, which it could not. Peter, any chance?

No disagreement (see point two below) that the Cron trigger or any time based trigger is no longer needed and can be removed! :slight_smile:

Agreed with Main having a specific meaning. The Studio Display is always connected as the Main scree, full stop.

Appreciate the references. I have not looked at them but will.

Is the suggestion that:

  1. A USB Device trigger should be used to deal with the concern that the display presets only be changed when the Studio Display is attached; and

  2. The CRON trigger can be removed as it will no longer be needed.

  3. This should fully address the concern I have about when the Macro does / does not fire.

Would the following be correct:

.....based on the below information

If the above USB device is correct then that seems like the best route as it solves all the problems and is sooooooo very elegant!

Hmmm, interesting observation, I I'll have to test that and come back to you as to where they are located.

Hmmm, I will be back, I think you are on to something!

@Nige_S

A few observations:

  1. If a Parallels Coherence window is minimized then the change to the display settings has no impact on the window positioning or size.

  2. If a Parallels Coherence window is visible then the change to the display settings places the windows in teh top left corner, not all windows are at the exact (0,0) corner but the are close. Now what?

  3. I have been thinking and I don't think the USB device trigger will work as described above because currently at 10:00 PM pause time for sunset is less that than the pause time fro sunrise which is a a problem!

I have therefore modified the macros as indicated in the attached:

a. Added / changed the trigger to the Studio Display being detected to address the Studio Monitor being connected problem.

b. Added IF...THE..ELSE statements so that when the actual time > sunrise / sunset times the pause each macro for 999,999 so that they do not fire multiple times a day or in the wrong order!

c. Created a new macro that calls the sunrise / sunset macro every hour from 4:00 AM to 10:00 PM to update the pause time so the macros file at the correct time (i.e., once the day changes over the pause times are properly reset).

Please let me know what you think about:

  1. The restructured macros in terms of a) the USB Device trigger so that the macros only fire when the Studio Display is connected and b) the setting of the TimeToSunrise and TimetoSunset pause times.

  2. The structure to ensure that the every hour the TimeToSunrise and TimetoSunset pause times are properly updated. Updated the new macro the TimeToSunrise and TimetoSunset pause times would never be updated!

Thanks.

_JBC Display Presets Macros.kmmacros (493.8 KB)

@Nige_S

Apologies but I had t further refine the macros in that Set Sunrise and Sunset Load Time macro in the above set did not work as the second macro never got called. The reason fior this is that its execution got paused / stop in teh first macro that I called by the long pause (I hope this is clear enough).

I therefore had to create a second such macro; one for updating the Sunrise Load time and one for updating the Sunset Log time.

The leaves us with:

  1. Any ideas on how to handle the Parallel windows that are open when a display present changes baed on their resulting top left main screen location; and;

  2. Any Idas on an easier / more elegant way to update set the Sunrise and Sunset Load Times (i.e., can this be done on 1 macro)?

  3. Unrelated:
    a. Is there a way to print a macro (I certainly cannot find it)

As always HUGE Thanks!

Joel

_JBC Display Presets Macros.kmmacros (498.4 KB)

I think that multiple changes have overcomplicated things and it would be worth starting again using your new, refined, plan.

But before you do -- do you actually need/want the cron triggers? The AM one may be problematic because triggers only fire when your Mac is awake. The PM one would bug the hell out of me, suddenly leaping into action when I'm in the middle of doing something.

If you can find other triggers -- "Wake", "Unlock" or a longish "Idle Time" leap to mind -- you may find things easier.

Then a different approach becomes viable, where you test the current time against your sunrise/sunset times and act accordingly. Something like:

if Studio Display is connected
then
   get riseTime
   get setTime
   if current time > riseTime AND current time < setTime
   then
      if current preset is not dayTime
      then
         set dayTime preset
      end if
   else
      if current preset is not nightTime
      then
         set nightTime preset
      end if
   end if
end if

If you can't get the current display preset in the background then you could set a KM Global on every change and check that instead.

Print what? If you want what's shown in the Editor, "Copy as Image" then load into Preview and print from there. Or you could "Copy as Text", load that into your favourite set editor and print that for the textual representation.

And, of course, you could write a macro to do the above for you :wink:

Appreciate the response noting that I think things are greatly simplified with the current iteration:

1, There is no CRON or other time triggee as there is no need (i.e., it is an unnecessary complication).

  1. The only triggers are:
    a. USB Device in Macro A which:
    i. Ensures the Studio Display is connected; and
    ii. Effects the display setting change ; and

    b. Periodic in Macro B which is used to determine the time until the preset changes.

  2. As a result of the above changes the actions / codes have been greatly simplified and reduced, you can / should take a look.

The only things I don't know and would appreciate your input on:

  1. Are you still of the view that this is too complex as I think it fairly simple with the above modifications.

  2. Any thoughts on managing the Parallels windows off to the top left.

  3. And the only other thing missing is the case in which the user connects the laptop between with the wrong preset (i.e., leave the house with 300 Nits preset and returns when it should be 100 Nits preset). Any ideas other than apple script (which admittedly I may have to revert too)!

As to your comment that the change would bug you it is noted i) whether the change is made in background (i.e., via AS) or in the foreground (i.e., via KM) the screen is going to flicker and ii) there is never one ideal time unless you are always eating / in the gym / etc. at a specific time (which I am not in the evening). What about simply abandonning all timing automation and simply setup a hot key!

As always, HUGE THANKS!

_JBC Display Presets Macros.kmmacros (494.6 KB)

I have a cycling problem, please help.

I have cleaned up the macro extensively today where:

  1. Set Sunrise Preset Load Time acts as the trigger for and calls Load Sunrise Preset

  2. Set Sunset Preset Load Time acts as the trigger for and calls Load Sunset Preset

The problem I am experiencing is demonstrated by below chronological notifications which indicate:

  1. Load Sunset Preset occurred as it should! Great!

  2. Load Sunrise Preset is sending a message which it should not be. It is as if the Load Sunrise Preset has somehow been called but from where?. The Sunrise Preset is not being loaded because of the time conditions (at least this part is working).

  3. Action failed messages make no sense as I am never trying to maniple the Load Sunrise or the Load Sunset windows. The only windows I am trying to manipulate is the System Settings window once in each of Load Sunrise and Load Sunset

  4. And then somehow we bounce back to Load Sunset which is the same as the first notification and where we should have stopped!

And, were that not bad enough, all of this occurs when I run the Load Sunset Preset macro w/o even calling it from the Set Sunset Load Time macro.

I have been staring at this for hours and would appreciate some help in getting this solved!

Thank you!

_JBC Display Presets Macros.kmmacros (499.5 KB)

A little information.

The two action failed commands are in respect of the below action which appears once in each of i) Load Sunrise Preset and ii) Load Sunset Preset which begs the questions:

  1. Why is this action causing failure notice? Is it because the window is already at the front or other?

  2. Why is the Load Sunset Preset jumping to the Load Sunrise Preset (which is the only way to get the notification message) and running through it twice?

The notification message is now:

Looking forward to your help, thank you!

Joel

[UPDATE: I figured this out, The USB Device trigger (which existed to ensure the Studio Display was connected) was causing the cycling because every time the preset was changed the Studio Display was treated as disconnecting and reconnecting. The solution, at least the one I implemented, was to remove the USB Device trigger and wrap the entire macro in an IF THEN ELSE statement conditioned on the Studio Display being connected. The macros that changes the press is called by another macro at exactly sunrise / sunset. I have included update macros for those interested.

_JBC Display Presets Macros.kmmacros (514.7 KB)
]