Tags and copies

In yesterday’s post, I talked about how I’ve been using file tags to organize my work photographs according to both date and subject.1 This works pretty well, but sometimes a set of Smart Folders that collects photos according to their tags isn’t the right solution. In those cases, I have a couple of scripts that allow me to replicate my set of Smart Folders into a set of real folders with copies of the photos organized by subject.

The problem with tagging and Smart Folders is that they are too Mac-centric. If I need to share project photos with a colleague or a client—who are almost always Windows users—I can’t just copy a file structure like this onto a USB stick and give it to them:

Photo organization with smart folders for tags

The JPEGs are fine, as are the dated directories, but the Smart Folders are just gibberish on a Windows machine—a set of XML files with .savedSearch extensions. If I need to have the photos broadly available in folders organized by subject, I need to make real folders and put copies of the photos in them.

Fortunately, this is pretty easy to do if I’ve already done the tagging and created the Smart Folders for each tag. I have a command-line script called tags2dirs which, when run from the parent directory (e.g., the test directory in the example above), creates a set of traditional folders that parallel the Smart Folders. After running tags2dirs, I get this:

Photo organization with folders for copies

The tags2dirs script is this short bit of Python:

python:
1:  #!/usr/bin/python
2:  
3:  import os
4:  import glob
5:  
6:  tagDirs = glob.glob('*.savedSearch')
7:  newDirs = [ x.split('.')[0] for x in tagDirs ]
8:  map(os.mkdir, newDirs)

Line 6 looks through the current directory and collects the names of all the Smart Folders into the tagDirs list. Line 7 goes through that list, strips the .savedSearch extension off of each name and saves the result to the newDirs list. Line 8 then makes new directories with the names in newDirs. Simple.

Now comes the harder part: copying each photograph in the dated folders into the corresponding folders named for the tags. As you might expect, I have a script for doing this, too. It’s called cp2tags, and when invoked like this from the test directory,

cp2tags */*.jpg

it will copy every JPEG file one directory level down into all of the appropriate directories that were created by tags2dirs. For example, the left track folder will look like this:

Left track copies folder

This uses a lot of disk space—if a photo has six tags, it will be copied six times—but both of my Macs have 3 TB Fusion Drives, so I can afford to be profligate, at least occasionally.

Here’s the source code for cp2tags:

python:
 1:  #!/usr/bin/python
 2:  
 3:  import os.path
 4:  import subprocess as sb
 5:  import sys
 6:  import shutil
 7:  
 8:  tagCmd = '/usr/local/bin/tag'
 9:  for f in sys.argv[1:]:
10:    tagString = sb.check_output([tagCmd, '--no-name', '--list', f]).strip()
11:    if tagString:
12:      tags = tagString.split(',')
13:      for t in tags:
14:        shutil.copy(f, t)

It goes through the arguments, which are expected to be file names, one at a time. For each file, it runs James Berry’s tag command, mentioned in yesterday’s post, to determine all the tags applied to each. The output of tag is a string of comma-separated tags, which is split apart on Line 12 to create a Python list of the file’s tags. Line 14 then copies the file to all the directories named after those tags.

For most projects, I don’t need tags2dirs or cp2tags, because I don’t need to send others my photos. But it’s nice to have the scripts ready.

One last thing: Tags created on the Mac can be used to filter files in the iOS Files app but only if the files are saved in iCloud Drive. File tags are synced to Dropbox, but the Files app doesn’t seem to know it. And I haven’t seen anything in the Dropbox app to suggest that it can filter by tags.

I’ve added tags to the sidebar through the clumsy tap-and-hold technique, but I haven’t worked out a quick, automated way to get a tag list into the Files sidebar.

Tags in Files sidebar

It’s too bad the Files app knows no more about Smart Folders than Windows does.


  1. For me, the subject of a photo is usually a piece of machinery, a structural component, or a fragment of a broken part. Many of my photos include more than one subject, which makes them a natural for tagging.