Mac OS 开发

获取app list

NSMutableArray *urls = [[[NSFileManager defaultManager] URLsForDirectory:NSAllApplicationsDirectory inDomains:NSLocalDomainMask] mutableCopy];    
   NSMutableSet *allApplications = [NSMutableSet set];
   for (NSURL *url in urls) {
       NSArray *array = [[NSFileManager defaultManager]
                        contentsOfDirectoryAtURL:url
                      includingPropertiesForKeys:nil
                                         options:(NSDirectoryEnumerationSkipsHiddenFiles)
                                           error:nil];
       [allApplications addObjectsFromArray:array];
   }
  
   NSMutableArray *applications = [NSMutableArray arrayWithCapacity:allApplications.count];
   for (NSURL *url in allApplications) {
       NSBundle *bundle = [[NSBundle alloc] initWithURL:url];
       NSDictionary *infoDic = bundle.infoDictionary;
       NSString *bundleId = bundle.bundleIdentifier;
       NSString *name = url.path.lastPathComponent.stringByDeletingPathExtension;
       NSString *version = infoDic[@"CFBundleShortVersionString"];
       NSString *iconName = infoDic[@"CFBundleIconFile"];
       NSString *iconPath = [bundle pathForResource:iconName.stringByDeletingPathExtension ofType:@"icns"];
       if (version.length == 0 || iconPath.length == 0 || bundleId.length == 0) {
           //not app
           continue;
       }
       
       NSBitmapImageRep *imageRep = (NSBitmapImageRep *)[NSBitmapImageRep imageRepWithContentsOfFile:iconPath];
       NSDictionary *imageProps = @{NSImageCompressionFactor:@(0.6)};

      //jpg data
       NSData *data = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:imageProps];
       NSString *dataBase64 = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
       NSDictionary *dic = @{@"package":bundleId?:@"null",
                             @"name":name?:@"null",
                             @"version":version?:@"null",
                             @"icon":dataBase64?:@"null"};
       [applications addObject:dic];
   }
{"bundleId":"com.viber.osx","icon":"xxx","name":"Viber","version":"16.7.0"},
{"bundleId":"com.tencent.xinWeChat","icon":"xxx","name":"WeChat","version":"3.2.2"},
{"bundleId":"WhatsApp","icon":"xxx","name":"WhatsApp","version":"2.2147.16"},
{"bundleId":"com.kingsoft.wpsoffice.mac","icon":"xxx","name":"wpsoffice","version":"3.5.0"},
{"bundleId":"com.apple.dt.Xcode","icon":"xxx","name":"Xcode","version":"13.1"},
{"bundleId":"us.zoom.xos","icon":"xxx","name":"zoom.us","version":"5.8.1 (1983)"},
{"bundleId":"com.yinxiang.Mac","icon":"xxx","name":"印象笔记","version":"9.4.14"}
....

获取某一个app的window list

你可能感兴趣的:(Mac OS 开发)