Here is the recursive version of the previous tags.py, which I am now calling rtags.py (r for recursive). From a user perspective, you will be prompted just as before — for the file containing a list of files to tag, and a second prompt for the starting point for the operation. When it is done, it will just print a line in the terminal indicating how many files were processed. I have added some more comments.
If you want to undo the tag setting, revise the color to none, and run as you did before. If you happen to have Sublime Text 3 editor, open and save a blank Python file, and then from the Edit menu : Paste and Indent. Otherwise, as you did before, and again, try it on a test directory first.
Code:
#!/usr/bin/python
# coding: utf-8
'''
program: rtags.py
Summary: Read master file containing files to be tagged in selected
folder. Utilizes AppleScript for user file and folder access.
Recursively descends into specified folder starting point,
finds file types that match original list and tags them.
Tested: Default OS X 10.11.3 Python v2.7.10.
Version: 1.2
Author: VikingOSX, Mar 2, 2016, Apple Support Communities
'''
from Foundation import NSAppleScript, NSURL, NSURLLabelNumberKey
import os
import sys
colors = {'none': 0, 'grey': 1, 'green': 2, 'purple': 3, 'blue': 4,
'yellow': 5, 'red': 6, 'orange': 7}
# set this to 'none' to recursively remove all tag colors just set
tag_color = 'orange'.lower()
file_type = '.mov'
mov_paths = []
tag_paths = []
AS = '''
property defloc : path to desktop folder
property msg1 : "Provide the input file with .mov filenames:"
property msg2 : "Select folder containing .mov files to tag:"
tell application "Finder"
activate
set movFile to POSIX path of (choose file with prompt msg1 default location defloc invisibles no)
set movFolder to POSIX path of (choose folder with prompt msg2 default location defloc invisibles no)
end tell
return {movFile, movFolder}
'''
def set_tag(afile, acolor):
fileURL = NSURL.fileURLWithPath_(afile)
fileURL.setResourceValue_forKey_error_(colors[acolor],
NSURLLabelNumberKey, None)
return
try:
t = NSAppleScript.alloc().initWithSource_(AS)
result = t.executeAndReturnError_(None)
mov_file = result[0].descriptorAtIndex_(1).stringValue().encode('utf-8')
tag_folder = (result[0].descriptorAtIndex_(2).stringValue().
encode('utf-8').rstrip('/'))
except (AttributeError, TypeError):
sys.exit('User pressed cancel… ending application')
# read in the file with list of .mov to tag.
with open(mov_file, 'r') as f:
mov_list = [x.strip() for x in f.readlines()]
# recursively search inside tag folder for .mov files that match file list
for root, dirs, files in os.walk(tag_folder):
for file in files:
full_path = os.path.join(root, file)
# capture only valid extension file_type
if os.path.splitext(full_path)[1].lower() == file_type:
mov_paths.append(full_path)
# retain only the folder hierarchy where files match original tag list
tag_files = [f for f in mov_paths if os.path.basename(f) in mov_list]
# put specified tag color on .mov files
[set_tag(f, tag_color) for f in tag_files if tag_files]
print("{} '{}' files tagged with {} color\n".format(len(tag_files),
file_type, tag_color))
sys.exit()