Skip to content

Commit 92650e7

Browse files
committed
feat: add askForMusicLibraryAccess
1 parent 61d3278 commit 92650e7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ askForMicrophoneAccess().then(status => {
256256
})
257257
```
258258

259+
## `permissions.askForMusicLibraryAccess()`
260+
261+
Returns `Promise<String>` - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
262+
263+
Example:
264+
```js
265+
const { askForMusicLibraryAccess } = require('node-mac-permissions')
266+
267+
askForMusicLibraryAccess().then(status => {
268+
console.log(`Access to Apple Music Library is ${status}`)
269+
})
270+
```
271+
259272
## `permissions.askForPhotosAccess()`
260273

261274
Returns `Promise<String>` - Current permission status; can be `authorized`, `denied`, or `restricted`.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = {
4242
askForRemindersAccess: permissions.askForRemindersAccess,
4343
askForCameraAccess: permissions.askForCameraAccess,
4444
askForMicrophoneAccess: permissions.askForMicrophoneAccess,
45+
askForMusicLibraryAccess: permissions.askForMusicLibraryAccess,
4546
askForPhotosAccess: permissions.askForPhotosAccess,
4647
askForSpeechRecognitionAccess: permissions.askForSpeechRecognitionAccess,
4748
askForScreenCaptureAccess: permissions.askForScreenCaptureAccess,

permissions.mm

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,51 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
649649
return deferred.Promise();
650650
}
651651

652+
// Request Apple Music Library access.
653+
Napi::Promise AskForMusicLibraryAccess(const Napi::CallbackInfo &info) {
654+
Napi::Env env = info.Env();
655+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
656+
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
657+
env, Napi::Function::New(env, NoOp), "musicLibraryCallback", 0, 1);
658+
659+
if (@available(macOS 10.16, *)) {
660+
std::string auth_status = MusicLibraryAuthStatus();
661+
662+
if (auth_status == kNotDetermined) {
663+
__block Napi::ThreadSafeFunction tsfn = ts_fn;
664+
[SKCloudServiceController
665+
requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
666+
auto callback = [=](Napi::Env env, Napi::Function js_cb,
667+
const char *granted) {
668+
deferred.Resolve(Napi::String::New(env, granted));
669+
};
670+
671+
bool granted =
672+
status == SKCloudServiceAuthorizationStatusAuthorized;
673+
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
674+
tsfn.Release();
675+
}];
676+
} else if (auth_status == kDenied) {
677+
NSWorkspace *workspace = [[NSWorkspace alloc] init];
678+
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
679+
@"security?Privacy_Media";
680+
681+
[workspace openURL:[NSURL URLWithString:pref_string]];
682+
683+
ts_fn.Release();
684+
deferred.Resolve(Napi::String::New(env, kDenied));
685+
} else {
686+
ts_fn.Release();
687+
deferred.Resolve(Napi::String::New(env, auth_status));
688+
}
689+
} else {
690+
ts_fn.Release();
691+
deferred.Resolve(Napi::String::New(env, kAuthorized));
692+
}
693+
694+
return deferred.Promise();
695+
}
696+
652697
// Request Screen Capture Access.
653698
void AskForScreenCaptureAccess(const Napi::CallbackInfo &info) {
654699
if (@available(macOS 10.15, *)) {
@@ -706,6 +751,8 @@ void AskForAccessibilityAccess(const Napi::CallbackInfo &info) {
706751
Napi::Function::New(env, AskForCameraAccess));
707752
exports.Set(Napi::String::New(env, "askForMicrophoneAccess"),
708753
Napi::Function::New(env, AskForMicrophoneAccess));
754+
exports.Set(Napi::String::New(env, "askForMusicLibraryAccess"),
755+
Napi::Function::New(env, AskForMusicLibraryAccess));
709756
exports.Set(Napi::String::New(env, "askForSpeechRecognitionAccess"),
710757
Napi::Function::New(env, AskForSpeechRecognitionAccess));
711758
exports.Set(Napi::String::New(env, "askForPhotosAccess"),

0 commit comments

Comments
 (0)