Search code examples
objective-citunesscripting-bridgemacos-mojave

Grab audioCDPlaylist via ScriptingBridge does not work with macOS Mojave Beta


I have a application which gets audioCDPlayList from iTunes. This app works fine up to macOS High Sierra, but does not work correctly on macOS Mojave Beta 3 (18A326h).

I have investigated the reason and then found that the following strange behavior:

GetAudioCDInfoFromiTunes.h

#import <Foundation/Foundation.h>
#import <ScriptingBridge/ScriptingBridge.h>
#import "iTunes.h"

@interface GetAudioCDInfoFromiTunes : NSObject

   - (NSMutableDictionary *)getAudioCDInfoFromiTunes;

@end

GetAudioCDInfoFromiTunes.m

- (NSMutableDictionary *)getAudioCDInfoFromiTunes {

    // Declear iTunes scripting bridge variable
    iTunesApplication *iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

    SBElementArray *sources = [iTunesApp sources];
    NSLog(@"sources=%@", sources);
    NSLog(@"count=%ld", [sources count]);

    iTunesPlaylist *aAudioCDPlayList = nil;

    for (iTunesSource *src in sources) {

        NSLog(@"src=%@", src);

        SBElementArray *playlists = [src audioCDPlaylists];

        NSLog(@"playlists=%@", playlists);

        for (iTunesPlaylist *aPlaylist in playlists) {

            NSLog(@"aplaylist=%@", aPlaylist);

            if ([aPlaylist isKindOfClass:[NSClassFromString(@"ITunesAudioCDPlaylist") class]]) {

                aAudioCDPlayList = [aPlaylist get];

                break;
           }
       }
   }

... SNIP ...

}

Executing the above code, NSLog of Line.8, count of sources is 0. And therefore for loop of Line.12 don't work. Then the result [aPlaylist get] is null.

Does anyone know the reason why the count of sources is 0?

Plase let me know how can I run my ScriptingBridge code on Mojave Beta...


Solution

  • Mojave has tightened data security and privacy and that includes scripting. See WWDC 2018 session 702.

    The first time your app tries to control iTunes, Mojave will prompt to get your confirmation to allow that. It will remember your choice so it doesn't ask again.

    I guess you must have denied it permission once. After that, it just always prevented your app from controlling iTunes.

    Since developers need to test their app's behavior when this prompt is displayed, when permission is denied, and when it's granted, Apple has included a command-line utility, tccutil, to reset the remembered user choices. The command to reset permissions regarding which apps may control other apps is tccutil reset AppleEvents.