How do I tag multiple files from a list in a txt file?

Hi everyone,


I would like to tag over 250 Finder items (i.e. label them with the color Orange) whose file names I have in a txt file.


The txt file list looks like this:


1_A69_2015-11-28_1454_C0046.mov

1_A69_2015-11-28_1410_C0045.mov

1_A69_2015-11-28_1408_C0044.mov

1_A69_2015-11-28_1407_C0043.mov

and so on...

I would like to select all of these files that I know exist on a external drive and tag them with a color.

I have tried to do a boolean search in Finder by typing "1_A69_2015-11-28_1454_C0046.mov OR 1_A69_2015-11-28_1410_C0045.mov OR..." and so forth but Finder doesn't seem to return an accurate list (or it sometimes doesn't return one at all – my list seems to long.)


I understand that Automator is too primitive for this but don't know enough Applescript, Shell script or Terminal commands to make this happen.

Please help!

MacBook Pro with Retina display, OS X El Capitan (10.11.3)

Posted on Mar 1, 2016 1:24 PM

Reply
24 replies

Mar 1, 2016 3:39 PM in response to Myslnik

The following script should SELECT all the files to tag in the chosen folder. Just run it from the script Editor window (after replacing “username” with the appropriate name).


set theTextFile to POSIX file "/Users/username/Desktop/myTextFile.txt" as alias


set theFolder to choose folder-- the folder containing the items to tag


set theText to readtheTextFile

set list1 to {}

repeat with thisParagraph in paragraphs of theText

set thisParagraph to contents of thisParagraph

if thisParagraph ends with ".mov" then

copy thisParagraph to the end of list1

end if

end repeat


set list2 to {}

repeat with thisName in list1

set thisName to contents of thisName

try

tell application "Finder" to copy filethisName of theFolder to the end of list2

end try

end repeat


tell application "Finder" to set selection to list2

Mar 1, 2016 6:18 PM in response to Myslnik

A Python/Objective-C script that borrows AppleScript to present a choose file, and choose folder dialog that obtains your list of .mov files to tag, and the target folder where they are to be tagged. Any .mov file that is both in the list, and within that folder will get tagged — in your case orange. This can be set once in the code. This was tested with the default Python 2.7.10 in OS X 10.11.3, and is run from Launchpad : Other : Terminal.


If you have a real programmer's editor (not AppleScript Script Editor), then copy/paste the following code into it and save as tags.py. Otherwise, open a new TextEdit document, select Format menu : Make Plain Text, and then copy/paste into that document. Save as tags.py.


I suggest that you make a test directory with a few .mov files in it, and a paired down list to go with it. Run the Python script against that data and folder.


You still need to make this Python script executable, so in the Terminal window, and then run it.

$ chmod +x tags.py

$ ./tags.py


Code:


#!/usr/bin/python

# coding: utf-8

'''

program: tag.py

Summary: Read master file containing files to be tagged in specified

folder. Utilizes AppleScript for user file and folder access.

Tested: Default OS X 10.11.3 Python v2.7.10. Uses Python/ObjC bridge

Version: 1.1

Author: VikingOSX, Mar 1, 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}


tag_color = 'orange'

mov_type = '.mov'



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()]


# get list of .mov files to be tagged in the selected folder

mov_folder_items = filter(lambda f: not f.startswith('.') and

f.endswith(mov_type), os.listdir(tag_folder))


# list of common .mov files from the file listing and tagging folder

tagged_items = set(mov_list) & set(mov_folder_items)

# put an orange tag on specified .mov files

[set_tag(os.path.join(tag_folder, x), tag_color) for x in tagged_items if tagged_items]

sys.exit()

Mar 2, 2016 9:18 AM in response to Myslnik

Because I haven't posted the revised script here that does what I mentioned previously.


Again, I will ask the question. Do you want the script to recursively drill down into the initial folder that you provide, and tag every '.mov' file in that folder hierarchy that matches your list? If that is what you want, then I will post the updated Python script.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I tag multiple files from a list in a txt file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.