def find_apps_in_dirs(dirlist): """Do spotlight search for type applications within the list of directories provided. Returns a list of paths to applications these appear to always be some form of unicode string. """ applist = [] query = NSMetadataQuery.alloc().init() query.setPredicate_( NSPredicate.predicateWithFormat_('(kMDItemKind = "Application")')) query.setSearchScopes_(dirlist) query.startQuery() # Spotlight isGathering phase - this is the initial search. After the # isGathering phase Spotlight keeps running returning live results from # filesystem changes, we are not interested in that phase. # Run for 0.3 seconds then check if isGathering has completed. runtime = 0 maxruntime = 20 while query.isGathering() and runtime <= maxruntime: runtime += 0.3 NSRunLoop.currentRunLoop( ).runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.3)) query.stopQuery() if runtime >= maxruntime: display.display_warning( 'Spotlight search for applications terminated due to excessive ' 'time. Possible causes: Spotlight indexing is turned off for a ' 'volume; Spotlight is reindexing a volume.') for item in query.results(): pathname = item.valueForAttribute_('kMDItemPath') if pathname and not is_excluded_filesystem(pathname): applist.append(pathname) return applist
def find_apps_in_dirs(dirlist): """Do spotlight search for type applications within the list of directories provided. Returns a list of paths to applications these appear to always be some form of unicode string. """ applist = [] query = NSMetadataQuery.alloc().init() query.setPredicate_( NSPredicate.predicateWithFormat_('(kMDItemKind = "Application")')) query.setSearchScopes_(dirlist) query.startQuery() # Spotlight isGathering phase - this is the initial search. After the # isGathering phase Spotlight keeps running returning live results from # filesystem changes, we are not interested in that phase. # Run for 0.3 seconds then check if isGathering has completed. runtime = 0 maxruntime = 20 while query.isGathering() and runtime <= maxruntime: runtime += 0.3 NSRunLoop.currentRunLoop().runUntilDate_( NSDate.dateWithTimeIntervalSinceNow_(0.3)) query.stopQuery() if runtime >= maxruntime: display.display_warning( 'Spotlight search for applications terminated due to excessive ' 'time. Possible causes: Spotlight indexing is turned off for a ' 'volume; Spotlight is reindexing a volume.') for item in query.results(): pathname = item.valueForAttribute_('kMDItemPath') if pathname and not is_excluded_filesystem(pathname): applist.append(pathname) return applist
def get_apps(tag, removal): """use spotlight to find apps by custom tag""" # main dictionary removals = {} # set NSMetaDatQuery predicate by your custom tag with value of true predicate = "%s = 'true'" % tag # build and execute the spotlight query query = NSMetadataQuery.alloc().init() query.setPredicate_(NSPredicate.predicateWithFormat_(predicate)) query.setSearchScopes_(['/Applications']) query.startQuery() start_time = 0 max_time = 20 while query.isGathering() and start_time <= max_time: start_time += 0.3 NSRunLoop.currentRunLoop().runUntilDate_( NSDate.dateWithTimeIntervalSinceNow_(0.3)) query.stopQuery() # iterate through the results to grab spotlight attributes for item in query.results(): app = item.valueForAttribute_('kMDItemFSName') path = item.valueForAttribute_('kMDItemPath') customtag = item.valueForAttribute_(removal) if customtag: # build nested dictionary of tagged apps and attribute values removals[app] = {} removals[app]['path'] = path removals[app]['method'] = customtag return removals
def get_apps(): # credit to the Munki project for borrowing code, thanks Munki! apps_dict = {} query = NSMetadataQuery.alloc().init() query.setPredicate_( NSPredicate.predicateWithFormat_( "(kMDItemContentType = 'com.apple.application-bundle')")) query.setSearchScopes_(['/Applications']) query.startQuery() start_time = 0 max_time = 20 while query.isGathering() and start_time <= max_time: start_time += 0.3 NSRunLoop.currentRunLoop().runUntilDate_( NSDate.dateWithTimeIntervalSinceNow_(0.3)) query.stopQuery() # check if the app returns a None value for this query, some apps may have embedded Applescripts # that will return a None value and will be set to blank for app in query.results(): name = app.valueForAttribute_('kMDItemDisplayName') if name: version = app.valueForAttribute_('kMDItemVersion') or '' apps_dict[name] = version return apps_dict
from Foundation import NSObject, NSNotificationCenter, NSMetadataQuery, NSPredicate, NSMetadataQueryUserHomeScope, \ NSMetadataQueryDidFinishGatheringNotification from PyObjCTools import AppHelper # from ConsoleReactor import ConsoleReactor # host = '127.0.0.1' # port = 0 # interpreterPath = sys.executable # scriptPath = unicode(os.path.abspath('tcpinterpreter.py')) # commandReactor = ConsoleReactor.alloc().init() # interp = AsyncPythonInterpreter.alloc().initWithHost_port_interpreterPath_scriptPath_commandReactor_(host, port, interpreterPath, scriptPath, commandReactor) # interp.connect() query = NSMetadataQuery.alloc().init() query.setPredicate_(NSPredicate.predicateWithFormat_( 'kMDItemKind = "Aperture Library"' )) scopes = [NSMetadataQueryUserHomeScope] query.setSearchScopes_( scopes ) class ThisEventLoopStopper(NSObject): def interpFinished_(self, notification): AppHelper.stopEventLoop() stopper = ThisEventLoopStopper.alloc().init() NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(stopper, 'interpFinished:', NSMetadataQueryDidFinishGatheringNotification, query) query.startQuery() # NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(stopper, 'interpFinished:', u'AsyncPythonInterpreterClosed', interp) AppHelper.runConsoleEventLoop(installInterrupt=True)