dfay Posted October 29, 2018 Posted October 29, 2018 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))
TomBenz Posted May 27, 2022 Posted May 27, 2022 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
vitor Posted May 28, 2022 Posted May 28, 2022 (edited) @pankajsz You need Homebrew and to brew install tag. Then replace line 23 with the path to it: tagCmd = '/opt/homebrew/bin/tag' if on Apple Silicon, tagCmd = '/usr/local/bin/tag' if on Intel. Also, you may need to install Python 2 or that script may work on Python 3 without modification. Edited May 28, 2022 by vitor
TomBenz Posted May 28, 2022 Posted May 28, 2022 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.
vitor Posted May 28, 2022 Posted May 28, 2022 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.
giovanni Posted May 28, 2022 Posted May 28, 2022 6 hours ago, pankajsz said: But unable to see any Smart Folders created. have you replaced plistlib.writePlist (Python 2) with plistlib.dump (Python 3)?
TomBenz Posted June 17, 2022 Posted June 17, 2022 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'
giovanni Posted June 17, 2022 Posted June 17, 2022 (edited) this works for me. as @vitor says, you will need to replace line 23 with tagCmd = '/opt/homebrew/bin/tag' if on Apple Silicon Edited June 17, 2022 by giovanni TomBenz 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now