Skip to content

Commit 976d211

Browse files
committed
feat: implement permissions.askForScreenCaptureAccess()
1 parent 73d3574 commit 976d211

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Return Value Descriptions:
3939
**Notes:**
4040
* Access to `contacts` will always return a status of `authorized` prior to macOS 10.11, as access to contacts was unilaterally allowed until that version.
4141
* Access to `camera` and `microphone` will always return a status of `authorized` prior to macOS 10.14, as access to contacts was unilaterally allowed until that version.
42+
* Access to `screen` will always return a status of `authorized` prior to macOS 10.15, as access to screen capture was unilaterally allowed until that version.
4243

4344
Example:
4445
```js
@@ -146,3 +147,13 @@ for (const type of ['microphone', 'camera']) {
146147
})
147148
}
148149
```
150+
## `permissions.askForScreenCaptureAccess()`
151+
152+
There is no API for programmatically requesting Screen Capture on macOS at this time, and so calling this method will trigger opening of System Preferences at the Screen Capture pane of Security and Privacy.
153+
154+
Example:
155+
```js
156+
const { askForScreenCaptureAccess } = require('node-mac-permissions')
157+
158+
askForScreenCaptureAccess()
159+
```

permissions.mm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
}
102102

103103
// Returns a status indicating whether the user has authorized
104-
// Screen Recording access
104+
// Screen Capture access
105105
std::string ScreenAuthStatus() {
106106
std::string auth_status = "not determined";
107107

@@ -310,6 +310,16 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
310310
return deferred.Promise();
311311
}
312312

313+
// Request Screen Capture Access.
314+
void AskForScreenCaptureAccess(const Napi::CallbackInfo &info) {
315+
if (@available(macOS 10.14, *)) {
316+
NSWorkspace *workspace = [[NSWorkspace alloc] init];
317+
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
318+
@"security?Privacy_ScreenCapture";
319+
[workspace openURL:[NSURL URLWithString:pref_string]];
320+
}
321+
}
322+
313323
// Initializes all functions exposed to JS
314324
Napi::Object Init(Napi::Env env, Napi::Object exports) {
315325
exports.Set(Napi::String::New(env, "getAuthStatus"),
@@ -324,6 +334,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
324334
Napi::Function::New(env, AskForFullDiskAccess));
325335
exports.Set(Napi::String::New(env, "askForMediaAccess"),
326336
Napi::Function::New(env, AskForMediaAccess));
337+
exports.Set(Napi::String::New(env, "askForScreenCaptureAccess"),
338+
Napi::Function::New(env, AskForScreenCaptureAccess));
327339

328340
return exports;
329341
}

0 commit comments

Comments
 (0)