The macfile module is part of the appscript module and has been renamed mactypes to "2006-11-20 - 0.2.0"
Using this module, here you can find two functions for getting and setting finders labels with appscript version 1.0:
from appscript import app from mactypes import File as MacFile # Note these label names could be changed in the Finder preferences, # but the colours are fixed FINDER_LABEL_NAMES = { 0: 'none', 1: 'orange', 2: 'red', 3: 'yellow', 4: 'blue', 5: 'purple', 6: 'green', 7: 'gray', } def finder_label(path): """Get the Finder label colour for the given path >>> finder_label("/tmp/example.txt") 'green' """ idx = app('Finder').items[MacFile(path)].label_index.get() return FINDER_LABEL_NAMES[idx] def set_finder_label(path, label): """Set the Finder label by colour >>> set_finder_label("/tmp/example.txt", "blue") """ label_rev = {v:k for k, v in FINDER_LABEL_NAMES.items()} available = label_rev.keys() if label not in available: raise ValueError( "%r not in available labels of %s" % ( label, ", ".join(available))) app('Finder').items[MacFile(path)].label_index.set(label_rev[label]) if __name__ == "__main__": # Touch file path = "blah" open(path, "w").close() # Toggle label colour if finder_label(path) == "green": set_finder_label(path, "red") else: set_finder_label(path, "green")
source share