-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxattr.py
More file actions
49 lines (35 loc) · 1.5 KB
/
xattr.py
File metadata and controls
49 lines (35 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
import plistlib
import subprocess as sp
import argparse
PARSER = argparse.ArgumentParser(description='Script for setting attributes for multiple files in macOS')
PARSER.add_argument('--files', '-f', type=str, required=True, help='File paths, seperated by spaces')
PARSER.add_argument('--attrVals', '-a', type=str, required=True, help='Attribute values, seperated by ;')
PARSER.add_argument('--overwrite', '-o', action='store_true', help='Overwrite existing attributes')
#PARSER.add_argument('--attribute', '-a', type=str, help='xattr to modify')
XATTRS = {
'tags': 'com.apple.metadata:_kMDItemUserTags'
}
attr = XATTRS['tags'] # TODO: cmdline argument
getCurPlistCmd = ['xattr', '-p', attr] # + [filepath]
setPlistCmd = ['xattr', '-w', attr] # + [plistString, filepath]
if __name__ == '__main__':
args = PARSER.parse_args(sys.argv[1:])
files = args.files.split(' ') # NOTE: will fail for paths with spaces
newAttrs = args.attrVals.split(';') # TODO: $1. currently only works with array plist format
for f in files:
currentPlist = None
updatedAttrs = None
try:
cmd = getCurPlistCmd + [f]
currentPlist = sp.check_output(cmd)
except sp.CalledProcessError: # no attributses found
pass
if currentPlist is None or args.overwrite == True:
updatedAttrs = newAttrs
else:
prevAttrs = plistlib.readPlistFromString(currentPlist)
updatedAttrs = newAttrs + prevAttrs # TODO: $1
newPlist = plistlib.writePlistToString(updatedAttrs)
cmd = setPlistCmd + [newPlist, f]
sp.call(cmd)