Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting and setting mac file and folder finder labels from Python

I have been trying to find out how to get and set the colour of file labels from python.

The closest thing I've found to a solution was this, but I can't seem to find the module macfile anywhere. Am I just not looking hard enough?

Is there a different way to achieve this if not?

like image 798
GP89 Avatar asked Nov 30 '11 15:11

GP89


People also ask

How do I get a list of files in a folder on a Mac?

First, open up the folder you want to create the list from, and select all of the files and folders you want to include. Once all of the relevant files and folders are selected, copy the list by either selecting Edit then Copy Items in the menu or the Command-C keyboard shortcut.

How do you find the directory of a file in python Mac?

(Mac) Right-click a file in your desired directory > Click Get Info > Highlight and copy the path listed next to "Where:" (Alternate Mac) Right-click a file in your desired directory > Hold down the Option key > Click Copy "file_name" as Pathname.

How do I add Tags to Finder on Mac?

Tag a file on the desktop or in the Finder: Select the item, then open the File menu. You can also Control-click the item, or tap it with two fingers. Choose a color above Tags (the name of the tag replaces Tags as you move the pointer over the color), or click Tags to choose from more tags or to enter a new tag.

How do I automatically sort files by name on Mac?

Open a Finder window and choose List view (which I prefer over Icons view and Columns view. Select the menu item View > Arrange By > Name. Click the Name field at the top of the list view. An upward-pointing arrow following Name indicates alphabetical sorting with numbers first, then A to Z.


2 Answers

You can do this in python using the xattr module.

Here is an example, taken mostly from this question:

from xattr import xattr

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['com.apple.FinderInfo']
    color = finder_attrs[9] >> 1 & 7
except KeyError:
    color = 0

print colornames[color]

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

like image 130
jterrace Avatar answered Oct 06 '22 04:10

jterrace


If you follow favoretti's link and then scroll down a bit, there's a link to https://github.com/danthedeckie/display_colors, which does this via xattr, but without the 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')
like image 27
chbrown Avatar answered Oct 06 '22 04:10

chbrown