Get and configure Python macro and folder file shortcuts

I am trying to figure out how to get and set the color of file shortcuts from python.

The closest thing I found for a solution was this , but I cannot find the module file anywhere. I just don't look complicated enough?

Is there any other way to achieve this if not?

+6
source share
3 answers

You can do this in python using xattr .

Here is an example taken mainly from this question :

from xattr import xattr from struct import unpack colornames = { 0: 'none', 1: 'gray', 2: 'green', 3: 'purple', 4: 'blue', 5: 'yellow', 6: 'red', 7: 'orange', } attrs = xattr('./test.cpp') try: finder_attrs = attrs[u'com.apple.FinderInfo'] flags = unpack(32*'B', finder_attrs) color = flags[9] >> 1 & 7 except KeyError: color = 0 print colornames[color] 

Since I colored this file with a red mark, 'red' printed for me. You can use the xattr module to write a new label to disk.

+11
source

If you follow the favoretti link and then scroll down a bit, there is a link to https://github.com/danthedeckie/display_colors , which does this via xattr , but without binary manipulations. I rewrote his code a bit:

 from xattr import xattr def set_label(filename, color_name): colors = ['none', 'gray', 'green', 'purple', 'blue', 'yellow', 'red', 'orange'] key = u'com.apple.FinderInfo' attrs = xattr(filename) current = attrs.copy().get(key, chr(0)*32) changed = current[:9] + chr(colors.index(color_name)*2) + current[10:] attrs.set(key, changed) set_label('/Users/chbrown/Desktop', 'green') 
+4
source

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") 
0
source

Source: https://habr.com/ru/post/902758/


All Articles