Script to toggle off and on webcam access for apps in System Settings?

Sure! Why not! If I had thought of that, I would have mentioned it, but I didn't. I don't use the Button action very often, because for it to work the app has to support AppleScript access, which System Settings does.

You might want to inform the user of the change.

Is that just a toggle macro? It looks like it only toggles. If you need absolute settings, you may need to modify this.

Yeah, instead of a toggle, the ideal would be to have one macro to turn on (or, if already turned on, then leave as is), and another macro to turn off (or, if already turned off, then leave as is). Is that what you mean by "absolute"? But I'm not sure how you'd tweak the macro I shared to do that.

Yes. You understood me.

You don't necessarily need two macros. I suppose you could pass a parameter to the macro with a "1" or "0". Or make your macro triggered as a subroutine. And there may even be more choices than that.

Right, but I guess what I mean is that in order to take an absolute approach, you'd need to be able to determine the current state of the toggle, right? That way, for a "turn off" version of the macro, for example, you would do...

• If on, then toggle off.
• If already off, then do nothing.

But how would you determine the current state of the toggle in the first place?

If you are using an absolute approach, you don't need to determine its current state first. You just set it to 1 or 0. It doesn't matter if you overwrite a value with the same value.

If you are taking a toggle approach, you do what I did in the example above, in which I read the value into a variable called Local1, toggled the variable, and wrote the result back.

Oh, I'm talking about the macro that I shared here...

Fair enough. Your method doesn't "read" the button. If you want to modify your method, you're going to have to start reading the screen with Find Image. Instead of going down that deep rabbit hole, why not try using my simple method instead?

Actually, there might be an AppleScript method to read that button. I'm not sure. You'll have to consult one of the AppleScript experts here.

This AppleScript will read the status of that toggle button. (click to expand/collapse)
if application "System Events" is not running then tell application "System Events" to launch
tell application "System Events" to tell application process "System Settings"
	if value of attribute "AXValue" of checkbox 1 of group 1 of scroll area 1 of group 1 of sheet 1 of window "Content & Privacy" is 1 then
		set camEnabled to true
	else
		set camEnabled to false
	end if
end tell
return camEnabled

Then, you could use that in an if action to do whatever you wanted it to.

Download Action(s): If Then Else.kmactions (2.6 KB)

Action-Image

Action-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
  • The user must ensure the macro is enabled.
  • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 13.6
  • Keyboard Maestro v11.0.1
1 Like

I envy all you who can use AppleScript so well.

Oh my skills are nothing compared to other people’s here!

Also, I just realized that AppleScript can actually be simplified... (click to expand/collapse)
if application "System Events" is not running then tell application "System Events" to launch
tell application "System Events" to tell application process "System Settings" to return value of attribute "AXValue" of checkbox 1 of group 1 of scroll area 1 of group 1 of sheet 1 of window "Content & Privacy"

Then just change the true to 1 in the results field of the action.

1 Like

Thanks so much, @cdthomer. It seems like something here is not working, though. I'm testing this, and regardless of whether "System Settings > Screen Time > Content & Privacy > App Restrictions > Allow Camera" is enabled or disabled, the script you shared seems to be returning that it's disabled, at least based on the if/then results I'm seeing.

I'm attaching the macro I'm attempting to use, in case it's helpful.

Toggle webcams and microphones.kmmacros (16.4 KB)

(Related to this, perhaps you know of an AppleScript that can more easily accomplish what I'm doing in the first part of the macro.)

Hi, @cdthomer. I'm still struggling to figure this one out. As mentioned, regardless of whether "System Settings > Screen Time > Content & Privacy > App Restrictions > Allow Camera" is enabled or disabled, the script you shared seems to be returning that it's disabled. Any idea why this might be happening?

I tried the script and got the same result as you. I don't think I can debug it, though.

1 Like

Thanks for confirming, @Airy! Good to know it's not just me.

No idea. What OS are you on? (I’m on Ventura, so if you’re on something else then no doubt that is the issue).

Run this AppleScript in your script editor app and tell me what the results are.

if application "System Events" is not running then tell application "System Events" to launch
tell application "System Events" to tell application process "System Settings"
	tell group 1 of scroll area 1 of group 1 of sheet 1 of window "Content & Privacy"
		tell (first checkbox whose name is "Allow Camera")
			return value of attribute "AXValue"
		end tell
	end tell
end tell

This is what I got when I ran your last script:

Screenshot 2023-11-12 at 9.38.09 AM

Thanks Airy, you did have the Content & Privacy window open at the time, right? I can’t see the full error message in your screenshot but it looks it might not have been able to find either the window or one of it’s elements...

I just re-tested. Your macro works when I open the following dialog box, but not when the "Content and Privacy pane" was open. Can you modify your macro to open this pop-up window? It should work then.

I should have been clearer with my previous comment, my apologies. That pane needs to be open, not just the Content and Privacy window.

I believe the OP already had a method for opening that pane, so using AppleScript to do it isn’t really necessary. If that’s not the case, then I can certainly provide more info on that, but later this evening or tomorrow since I’m about to step out for the rest of the day.

Okay. It looks like a solution has been arrived at.