NSMetadataQuery in macOS 10.15 can't work correctly

Here's my simple code to search apps installed in specified directory, it works perfect before macOS 10.15.
After upgrade to the beta version of 10.15, many apps can't be found, is there any new security system affects
the behaviour?

#import 
#import "Result.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        if (argc != 2) {
            fprintf(stderr, "lsapp \n");
            exit(1);
        }
        NSString* appName = [NSString stringWithUTF8String:argv[1]];
        Result* result = [[Result alloc] init];
        NSMetadataQuery* query = [[NSMetadataQuery alloc] init];
        [query setSearchScopes: [NSArray arrayWithObjects:@"/Applications",
                                                          @"/Library",
                                                          NSMetadataQueryUserHomeScope,
                                                          nil]];
        [query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemKind == 'Application')"]];
        [[NSNotificationCenter defaultCenter] addObserver:result
                                                 selector:@selector(queryDidFinishGathering:)
                                                     name:NSMetadataQueryDidFinishGatheringNotification
                                                   object:query];
        [query startQuery];
       
        CFRunLoopRun();
       
        if (result.resultMap[appName]) {
            printf("Found it!\n");
            printf("%s", [result.resultMap[appName] UTF8String]);
        } else {
            printf("App is not exist!\n");
        }
    }
    return 0;
}

Replies

Did you read this thread ? Maybe it can help in some way:


https://stackoverflow.com/questions/24280878/how-can-i-get-list-of-installed-applications-on-mac-os-x-programmatically

Have you tried by allowing "Full Disk Access" to your app? I believe this may now be required for apps that scan global directories.

The only way to do this now is execute Apple’s lsregister tool and parse the output.