Created
February 4, 2016 13:39
-
-
Save jdp/72944e01e3b1aa0a3b7c to your computer and use it in GitHub Desktop.
Run Spotlight queries from console in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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