Jump to content

Create Smart Folders by Tag (File Action)


Recommended Posts

Here's a script ready to be hooked into a file action to create smart folders for tags.  

 

Set up the script action as /usr/bin/python with input as argv .

 

Select a folder & launch the file action -- it will identify every tag that's used on any file in the folder, and create a corresponding smart folder.  Minimally adapted from http://leancrew.com/all-this/2018/10/a-little-tagging-automation/

 

#!/usr/bin/python

import plistlib
import sys
import os
import subprocess as sb

query = sys.argv[1]

# adapted from http://leancrew.com/all-this/2018/10/a-little-tagging-automation/
# 2018-10-10

# The tag command can be found at https://github.com/jdberry/tag
# This is where I have it installed 
tagCmd = '/opt/local/bin/tag'

# Set the working directory to the object of the file filter
cwd = query
os.chdir(cwd)

# then get all of the tags in files under it

tagString = sb.check_output([tagCmd, '--no-name', '--recursive']).strip()
tagString = tagString.replace(',', '\n')
tags = set(tagString.split('\n'))

for t in tags:
	# Build the dictionary for the smart folder
	rawQuery = '(kMDItemUserTags = "{}"cd)'.format(t)
	savedSearch = {
	'CompatibleVersion': 1,
	'RawQuery': rawQuery,
	'RawQueryDict': {
		'FinderFilesOnly': True,
		'RawQuery': rawQuery,
		'SearchScopes': [cwd],
		'UserFilesOnly': True},
	'SearchCriteria': {
		'CurrentFolderPath': [cwd],
		'FXScopeArrayOfPaths': [cwd]}}

	# Make the smart folder
	plistlib.writePlist(savedSearch, '{}.savedSearch'.format(t))

 

Link to comment
  • 3 years later...

Is it possible to fix this for Monterey 12.4. Tried and getting below error message:

 

ERROR: Smart Tag Folder[Run Script] Traceback (most recent call last):

  File "/Users/xyz/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/9FD0BD0C-C5F4-4F63-A9F4-1A0B6971E437", line 23, in <module>

    tagString = sb.check_output([tagCmd, '--no-name', '--recursive']).strip()

  File "/Users/xyz/.pyenv/versions/2.7.18/lib/python2.7/subprocess.py", line 216, in check_output

    process = Popen(stdout=PIPE, *popenargs, **kwargs)

  File "/Users/xyz/.pyenv/versions/2.7.18/lib/python2.7/subprocess.py", line 394, in __init__

    errread, errwrite)

  File "/Users/xyz/.pyenv/versions/2.7.18/lib/python2.7/subprocess.py", line 1047, in _execute_child

    raise child_exception

OSError: [Errno 2] No such file or directory

Link to comment

Thanks Vitor. I am on Apple Silicon and have Python and tag installed via Homebew. I changed the path and getting success message as below

 

[11:43:32.725] Smart Tag Folder[File Action] Processing complete

[11:43:32.742] Smart Tag Folder[File Action] Passing output '/Users/xyz/Downloads/Temp-img' to Run Script

 

But unable to see any Smart Folders created. How do we debug this Python code? I have some understanding of AppleScript and keen to learn Python skills to atleast tweak stuff. Pls advise.

Link to comment

I don’t use tags or Smart Folders (or Python, really). The code is from 2018 and is writing a plist; maybe you simply need to restart the Finder, or maybe Apple broke this or changed how you can do it programmatically (that happens a lot between major macOS versions). Check the original blog post (linked at the top) for clues.

Link to comment
  • 3 weeks later...
On 5/28/2022 at 7:22 PM, giovanni said:

have you replaced plistlib.writePlist (Python 2) with plistlib.dump (Python 3)?

Yes, I did but following error message in debugger. Possible to upload this as finished workflow? thanks in advance

 

[07:09:25.865] Logging Stopped.

[07:09:27.772] Logging Started...

[07:10:00.390] Smart Tag Folder[File Action] Processing complete

[07:10:00.394] Smart Tag Folder[File Action] Passing output '/Users/kpz/Downloads/Temp' to Run Script

[07:10:00.520] ERROR: Smart Tag Folder[Run Script] Traceback (most recent call last):

  File "/Users/kpz/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/32A34899-9FA5-4DE6-A893-48328C273836", line 43, in <module>

    plistlib.dump(savedSearch, '{}.savedSearch'.format(t))

AttributeError: 'module' object has no attribute 'dump'

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...