如何在macOS中搜索任何卷

时间:2019-05-04 19:29:50

标签: objective-c macos search

 - (void)readFolder:(NSString *)str :(NSMutableDictionary *)dict {
    NSArray *appFolderContents = [[NSArray alloc] init];
    appFolderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:str error:nil];
    for (NSString *app in appFolderContents) {
        if ([app containsString:@".app"])
        {
            NSString *appName = [[app lastPathComponent] stringByDeletingPathExtension];
            NSString *appPath = [NSString stringWithFormat:@"%@/%@", str, app];
            NSString *appBundle = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
//            NSLog(@"%@ -- %@", appPath, appBundle);
            NSArray *jumboTron = [NSArray arrayWithObjects:appName, appPath, appBundle, nil];
            [dict setObject:jumboTron forKey:appName];
        }
    }
}

//This searches for apps
- (void)getAPPList {
    NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];

    [self readFolder:@"/Applications" :myDict];
    [self readFolder:@"/Applications/Utilities" :myDict];
    [self readFolder:@"/System/Library/CoreServices" :myDict];
    [self readFolder:@"/Volumes/Macintosh HD/Applications" :myDict ];

// Volumes not named 'Macintosh HD' doesn't work, I'm trying to make it work
    [self readFolder:@"/Volumes/*/Applications" :myDict ];
//Some apps are stored in the user's Application folder instead of the main one
    [self readFolder:[NSString stringWithFormat:@"%@/Applications", NSHomeDirectory()] :myDict];
//Sometimes people keep apps in Downloads
    [self readFolder:[NSString stringWithFormat:@"%@/Downloads", NSHomeDirectory()] :myDict];
//Some apps are stored in the user's Library/Application Support sometimes
    [self readFolder:[NSString stringWithFormat:@"%@/Library/Application Support", NSHomeDirectory()] :myDict];

我试图使第26行([self readFolder:@"/Volumes/*/Applications" :myDict ])搜索所有卷,而不是仅搜索具有匹配/特定名称的卷。我该怎么办?

我正在使用Xcode 9.2

1 个答案:

答案 0 :(得分:0)

类似的事情应该可以解决(未尝)

   NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeURLKey, NSURLIsVolumeKey, nil];
   NSArray *volumeUrls = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:NSVolumeEnumerationSkipHiddenVolumes];
  for (NSURL *volumeUrl in volumeUrls)
  {
     BOOL mayBeBootVolume = NO;
     NSString* pathToVolume = [volumeUrl path];
     [self readFolder: [pathToVolume stringByAppendingString: @"/Applications"];
  }