Skip to content

Instantly share code, notes, and snippets.

@jdp
Created February 4, 2016 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdp/72944e01e3b1aa0a3b7c to your computer and use it in GitHub Desktop.
Save jdp/72944e01e3b1aa0a3b7c to your computer and use it in GitHub Desktop.
Run Spotlight queries from console in Python
#!/usr/bin/env python
# encoding: utf-8
from AppKit import *
from Foundation import *
from Cocoa import *
from PyObjCTools import AppHelper
class Delegate(NSObject):
def queryNotification_(self, note):
if note.name() == NSMetadataQueryGatheringProgressNotification:
items = note.userInfo()['kMDQueryUpdateAddedItems']
for item in items:
print item.valueForAttribute_('kMDItemPath')
delegate = Delegate.alloc().init()
query = NSMetadataQuery.alloc().init()
notecenter = NSNotificationCenter.defaultCenter()
notecenter.addObserver_selector_name_object_(delegate, 'queryNotification:', None, query)
query.setDelegate_(delegate)
query.setPredicate_(NSPredicate.predicateWithFormat_('kMDItemIsScreenCapture = 1'))
query.startQuery()
try:
AppHelper.runConsoleEventLoop(installInterrupt=True)
except KeyboardInterrupt:
AppHelper.stopEventLoop()
finally:
query.stopQuery()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment