Adding tags to files in Siri shortcuts

Hi everyone,

I think there might be no solution yet, but maybe anyone has a workaround. I am trying to set up a Siri shortcut that renames and saves PDFs to an iCloud folder. Everything works fine so far. Now here is the question: is there any chance to add tags to a file in Siri Shortcuts that I select from a pre-populated drop down menu?

Thanks for your help,
Felix

Not what you want to hear but you’re correct. Shortcuts doesn’t have an action to tag files.

Do you have access to ssh on a Mac connected to iCloud? If so, you can back door this via a shell script that calls AppleScript. You will have to experiment on this, because I lost my documentation and I really never use tags/labels. Also, I think Mojave might have broken this?

Syntax is " # file file file" where 1 is orange, 2 is red, 3 is yellow, 4 is blue, 5 is purple, 6 is green and 7 is grey.

    osascript - "$@" << EOF
    on run argv
        set myLabel to (item 1 of argv as number)
        repeat with i from 2 to (count of argv)
          tell application "Finder"
              set theFile to POSIX file (item i of argv) as alias
              set label index of theFile to myLabel
          end tell
        end repeat
    end run
EOF

(I have it in isascriopt because I call it (theoretically) from a bash script. I did just test this and while I got an odd error, the label was applied to the test file. Also, note that this uses the old finder label method, so only one label is attached to the file, not multiple tags.)

Hi, thanks for the good advice. I found a solution that works for me.

I save my files now from the iOS devices to a folder in iCloud. I have Siri shortcuts put a string called „tag_[name_of_the_tag]“ according to my selection into the file name. When I switch on my Mac, Hazel looks at this temporary folder, tags the file, deletes the „tag_…“ part in the file name and stores the file in the correct place.

:grinning:

3 Likes

Hazel is a fantastic tool for automation, and I should really use it more.

Yeah, I’ve done something very similar with Hazel only I used a separate text file instead of encoding in the file name.

This sounds very promising. Any chance you could share (a screenshot of) the Hazel settings you’re using? Also, does the tagging shortcut use a “Choose from List” action, with a list of options for the “name_of_the_tag” variable?

I made a short little video, which should help. This may not be the best way to do it, but in this example if I name a file “My file_taggit_work_.ext” it will get tagged “Work” and renamed to “My file.ext”

6.3MB

1 Like

Very useful! Thanks so much.

If you have only a handful of tags (as Apple seems to assume we do), then it wouldn’t be so hard to make a separate Hazel rule for each and every tag. But if you have a couple hundred tags, as I do, then that’s not going to work.

So, inspired by “Automators 8: File Automation” I starting thinking whether there might not be more to get out of Hazel. (I’m sure I’m reinventing the wheel here…). I figured there must be a way to break down the syntax of the the “Work” attribute in Hazel (tagit_work, in your example) to have three elements this: “tagit” “abc” “_”, so that the matching is agnostic about the “abc” part. If there were then a way to generate a variable from that string, in each case, and to use that variable as the tag that is used in the “Add tags” action.

VoilĂ !

Here’s an example: to apply my tag “keyreference”, then I append to the filename this:

filename_-_keyreference.ext

I then have a Hazel rule that matches the text after the - and then uses that text (“USETHIS” in the screenshot) to add a tag. I’ve also added a tag “tagconversion”, just so I can troubleshoot cases where it doesn’t work properly. I’m attaching a screenshot. Hope this is clear! It’s certainly helping me out.

For the shortcuts selection I use the “menu” action, as my hazel rule can only handle one tag per file so far.
Here is the Hazel screenshot (should be clear even if it is in German…):

Details:

Hazel looks for a PDF file with the respective “Tag_…” string in the file name. Then it adds a tag according to this string. It also adds a “Steuer” tag (Steuer = tax) with the current year. Subsequently it replaces the text “Tag_…” with ‘nothing’ in the file name. A following Hazel rule moves all files in this folder to the standard action folder I use.

With the few tags I use in my file system this works well for me. It would be nice however to have multiple tags per file and also only one Hazel rule that handles them all instead of one rule per tag.

Here is the script I use, run from Hazel as an embedded script with Shell: /bin/bash

This assumes 1) you’ve installed https://github.com/jdberry/tag and 2) that your tags are contained in a comma-delimited text file named according to the following convention:

File.pdf (or whatever - the actual file to be tagged)
File.pdf.wftags.txt (containing the tags)

theFile=$1
ext=${theFile##*.}
tagFileName=`basename "$theFile" .$ext`.wftags.txt
if [ -f "$tagFileName" ]
	then
		theTags=`cat "$tagFileName"`
		tag -a "$theTags" "$theFile"
		mv "$tagFileName" ~/.Trash/
	fi

The shortcut I use to generate the text file has a two stage selection process – first there’s a
“Choose from List” object then the choice from there is presented as the default answer in an “Ask for Input” object. Output from that gets written to the text file.

I would be careful with just using _-_ to match, this is a pretty common string in randomly downloaded files. There are 84 files matching that pattern in my home folder, and not one that I named that way. This rule as written will create a lot of random tags that you may not want.

How do you append the text string to the filename using Shortcuts? Thanks!

The German syntax for this command is “Namen konfigurieren”. It should be something like “adjust name” or “configure name”. With this command you can put together a couple of variables to build the new file name.

Hope this helps…

Hi,

Thank you. I am unable to pass the composite variable of “filename” + “extension” to the Save File action. I wind up just getting a blank text file that saves with the correct name, but not saving the file.

Essentially what I am trying to do is the following:

  1. Open a .pdf from within some app
  2. Select the extension, which takes the filename, and then appends an extension to the filename
  3. Save the file to Dropbox

I am able to get the filename and create the variable, but not then replace the filename and save the .pdf.

Thank you for your assistance.

Use the Set Name action before Save File. And in Save File just provide the folder path where you want to save it.

That is what it should look like. Tap on the magic variable “shortcut input” and select “name” to only adjust the name of the file. Then you should be good to go.

I have set that, and unfortunately, it still does not work. Instead of saving the .pdf file, it saves a .txt file (with the correct file name).

Here’s the workflow. I have it accepting type “.pdf”. When I select a .pdf file and trigger the shortcut, I select a category tag that I concatenate with the file name and then want to save it with the combined title.

Thank you for your help.

You’re setting the name of the category, not the file. You need to get variable and set that to the file before you set the name.

You were almost there. Here is the fix…

1 Like