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

Please correct me if I'm wrong, but I don't believe there's a way to toggle off (or on) webcam access for your entire system, i.e. for all apps.

Instead, I believe you have to do it app by app in "System Settings > Privacy & Security > Camera".

Is there a script that can do this for a specified app, e.g. Zoom?

Hey @alexcr, just an FYI: I adjusted your title because it said “microphone’, not webcam.

1 Like

My Mac doesn't have a camera, but if you insert an image of your "Camera" pane in System Settings, I may be able to give more advice. I can't see the pane because I don't have a camera and therefore macOS doesn't show me any pane.

But there may be another approach to solving this. Last month I wrote a macro that can determine what is the corresponding "key" in the macOS defaults file to a button that is located in the System Settings pane. (That was a complicated sentence.) I was thinking about uploading that macro. It's pretty amazing.

1 Like

Thanks, @cdthomer. Sorry about that.

Thanks, @Airy. Please see the screenshot below. This is my "System Settings > Privacy & Security > Camera".

Some panes in the macOS System Preferences have both a master switch and a "per program" switch in them. I can see now that yours does not have a master switch. However there could very well be a master switch hidden in the defaults app in macOS which stores tons of configuration data such as whether devices are "turned on" or "turned off".

My Mac doesn't have a camera, and yours does, so the best thing to do now is to run the following command on your Mac in a Terminal window:

defaults read | grep -i camera

What this will do is show us the names of all the keys in the defaults app that contains the string "camera." You will probably have dozens of results. You should look at the results, or post them here, and see if any of them sound like a variable that stores whether the camera is enabled, eg, "CameraEnabled" would be a good candidate if there was one.

If you find one, then we can pursue this line of thought and probably give you a way to enable and disable the camera as a whole, which I think is what you want. Moreover, since you have six apps that have access to your camera, we'll probably find one for each app. Those will correspond to the buttons in your pane, above. What we want is a key that sounds more like a master switch, rather than a per-app switch.

Thanks, @Airy. I ran this in Terminal, and this is what I got in response...

    kDefaultsFollowSystemCameraEnabled = 1;
"com.apple.camera" =     {
            "bundle-id" = "com.apple.cmio.ContinuityCamera.notifications";
            path = "/System/Library/UserNotifications/Bundles/com.apple.cmio.ContinuityCamera.notifications.bundle";

Oddly enough, I have more lines than you, even though I don't have a camera. The first line (which I also have) looks a little like the name of a key that might, just might, be a key that is used by macOS to determine whether your camera is enabled. It says "1" which probably means the camera is enabled. Do you know of a way to disable your camera, and if so, you can run the command again and see if it has changed to "0". But if you don't know of a way to disable the camera, then we might want to experiment with changing this value "manually". I'm not going to tell you how to toggle that key unless we are comfortable with taking a risk. I've toggled keys in my defaults before, and if it didn't do what I hoped, I just put it back and nothing was permanently damaged.

I don't know what the risk is if we go ahead with toggling this variable. Before we discuss that idea further, tell me if you know of a way using macOS to disable your camera. (I think the reason we are here is that you don't know, but I have to ask to be sure.)

No, I haven't been able to find a way to disable the camera for the entire system. It seems like the only manual option is to shut off camera permissions app by app, in System Settings.

Aha! I think I found a way to disable the camera in macOS. It's in the Screen Time section of System Settings. It's under Content and Privacy, which you have to enable. Then there's a sub-page called App Restrictions. Then there's a toggle for "Camera". This is the button you should toggle when running my macro. You may get a different result from your result above. Try it using this toggle button and tell me what happened.

But in fact maybe you don't even need to go through all this work because I think you might be willing to use Keyboard Maestro to click on the button in this window using the mouse. I'm not sure which solution you prefer. Let me know.

I just ran my macro using the toggle I named above, and I got a more likely to be correct result:

10894c10894
<         "NSStatusItem Visible AudioVideoModule" = 0;
---
>         "NSStatusItem Visible AudioVideoModule" = 1;

In my opinion, the name of that key sounds exactly like what you want.

I'm 80% sure that this approach will fix your problem, but I'm out of time for today. However this is a great lead for you and if you can figure out how to modify a key in defaults, you could take it from here. It's slightly risky, (more likely because you could make a mistake rather than have the value doing something bad) so it's probably best to test it on a system that isn't too critical to you.

1 Like

Thanks, @Airy. I see what you're saying. "System Settings > Screen Time > Content & Privacy > App Restrictions > Allow Camera".

Is there a script or some other efficient way I can toggle this button on and off? (As opposed to having to manually navigate to this part of System Settings every time.)

Yes, I was going through this whole thread to prepare you for a way to create a simple one-line KM Macro to do exactly what you want. When you see how easy it is, you will be thrilled. The trick is assembling the command, which we haven't finished doing yet.

What we did above was just find out if the key exists, and I think we found it. The next stage is assembling the command to modify the key. Normally I can assemble it in a few minutes, but I've run out of time for today, and it's been a few weeks since I've done this before, so I'm a little rusty and may need 15 minutes to remember the procedure. If you or someone else wants to try, you can go ahead. I'm sure there are a dozen people on this site with the wizardry skills to complete the rest of this. But I'm out of time today.

No worries, @Airy! Thanks for your help so far. Maybe someone else will chime in with ideas, but if not and you have some time in the next few days to share some more guidance, that would be much appreciated.

Hi, @Airy. No one else wound up chiming in here. :sweat_smile:

Any chance you can help me with this macro, and specifically a script command that would toggle on and off "System Settings > Screen Time > Content & Privacy > App Restrictions > Allow Camera"? :pray:t3:

I kinda forgot about this thread. Are you willing to take a risk and modify that setting in your defaults to see if it solves your problem? I can't guarantee 100% that it will work, and I can't even guarantee 100% that you won't trash your system.

If I recall correctly, it's quite easy to do this, but it will take me a few minutes to remember the command.

Is there instead a script or some other way to simply automate the toggling of this switch in System Settings? ("System Settings > Screen Time > Content & Privacy > App Restrictions > Allow Camera.")

Or is the only option to mess around with system defaults?

If the latter, I might just throw together some janky macro using keystroke or found image actions to accomplish this. I'm just assuming System Settings is at least somewhat scriptable. But maybe I'm wrong!

I'll show you the code I used for another configuration entry. If you like it, you can adapt it to your situation (and I will help you make the change!) We will need to make a small change or two to make it work for your situation. Here's what I did....

image

Notice how this macro was designed to toggle a setting in my defaults every time I ran it. Or you can create two macros, one to turn it on, and another to turn it off.

I'm not sure if this macro is doing anything right now. It might be that Sonoma changed the location of the setting, so I might need to modify the names. Anyway, this is the method I came up with on my own. This is a script. You are asking if there is a script, and this is my answer. It is a script. I guess I don't know what you mean by a script.

Are you willing to adapt this approach? I will help you adapt it. It will take me 5 minutes to find the correct data for you to put in there.

1 Like

And here is what I think you will need to enter yourself... bear in mind that I cannot test this because I don't have a camera.

The text strings you need are:

defaults read com.apple.controlcenter "NSStatusItem Visible AudioVideoModule"
defaults write com.apple.controlcenter "NSStatusItem Visible AudioVideoModule" $KMVAR_Local1

I've done this with other settings and I didn't break my macOS, but I have to disclaim any liability here. You should always make backups before experimenting.

Every time you run this macro, if it does what I think it will do, it will toggle some functionality of your camera. I presume it will toggle its availability. I have good reason to think that, but can't guarantee it. You can remove the Display action if you like. Or you can replace it to say:

Camera setting is now toggled.

Hmm, I suppose my macro could be updated to detect error conditions. If it works, I'll update it for you.

1 Like

"Messing around with system defaults" is literally the only way to modify that value, and that's exactly what the "System Settings" app written by Apple does. If you use some KM macro using Find Image, you're still modifying the file, but you are doing it through Apple's reliable app instead.

You've got me wondering if there's an AppleScript way to achieve the same thing. There probably is, but it's probably going to be doing exactly what I'm doing here.

You've also got me wondering if I should create a KM macro that will let me browse through the entire defaults file and let you view (or edit?) items inside it. As the person who would be testing my own macro, I'd probably damage my system fatally while debugging this.

1 Like

It's not as instantaneous as a script would be, but here's a macro that seems to get the job done.

What do you think, @Airy ?