2

It's easy to write an Applescript to open a Finder window to a certain folder, e.g.

tell application "Finder" to open "Macintosh HD:Users:Username:Downloads"

Is it possible for Applescript to open a Finder window showing all files tagged, for instance, green?

Thanks!

  • Why do you wish to do this? You can simply select green tag from the left bar in finder... – willem.hill Jan 18 '18 at 4:18
  • 1
    I need it as part a hardware-triggered workflow. – PHL Jan 18 '18 at 4:35
  • If the answer given below isn’t suitable for whatever reason, and you explicitly need the tagged files displayed, you might have to resort to GUI scripting. If you need to be shown how to do this, I can draft an answer for you. Another option is to use Automator. – CJK Jan 18 '18 at 10:38
0

Here's the workflow as I'm doing it now: I copy a phrase, open Finder and click on green tags. I copy/paste that phrase to find the filename that contains it. It then gets opened in Preview, from whence i print it. I do this hundreds of times a day, and have a programmable keypad that saves me lots of clicks. It'll run Applescripts.

Since it's now clear that you don't actually require the files to be displayed in a Finder window in order to achieve your ultimate objective, I borrowed the suggestion from @apple9321 and used mdfind to retrieve the files paths and match the filenames against the contents of the clipboard.

The first half of the script below performs these tasks. Sorry the script appears so long—it's actually not, as most of it taken up by comments that I added to help guide you through what each part of the script does. Copy-n-Paste it into Script Editor and run it (with suitable text already sitting on your clipboard).

    -- Get the text from the clipboard
    -- to use for searching filenames
    set SearchTerm to the clipboard as text
    set SearchTerm to quoted form of (["*", SearchTerm, "*"] as text)

    -- Run a bash command to retrieve all
    -- files with a green tag and a filename
    -- that includes the above search term
    -- (case-insensitive)
    set Command to ["mdfind ", ¬
        "\"((kMDItemUserTags = '*Green*'cd) ", ¬
        "&& (kMDItemDisplayName = ", ¬
        SearchTerm, ¬
        "cd))\""] as text

    try -- Run the bash command and get a list of file paths
        set FileList to paragraphs of (do shell script Command)
    on error -- No matching filenames, script terminates
        return 0
    end try

    -- Convert the string file paths to a class
    -- that AppleScript can use to reference the files
    repeat with PathToFile in FileList
        set end of FileList to POSIX file PathToFile
        set FileList to rest of FileList
    end repeat

    -- Open the files in Preview and print the documents.
    -- You may not need to actually need to include the
    -- `open` command, as I believe Preview will still
    -- be able to run a `print` command if you hand it a
    -- list of file references, e.g. print FileList
    tell application "Preview"
        open FileList
        -- You can omit `with properties` or just
        -- include the print settings you want
        -- (see the AppleScript "Preview" dictionary
        -- for details of other available settings)
        print documents with properties ¬
            {copies:1, target printer:"Epson Stylus Photo PX830"}
    end tell

The latter half of the script does some housework with the file types to make them useable, but then it's simple enough for AppleScript to open the file(s) in Preview and print them off.

I have to put a disclaimer here, and state that I haven't been able to test the print command for Preview in AppleScript. This is because I don't have a printer. But my strong feeling is that this ought to work with few, if any, tweaks, and would probably even allow you to print without opening the files first:

    tell application "Preview" to print FileList

But, as I said, I cannot physically test it myself and I'm going by what's implied from Preview's AppleScript dictionary (which also contains a list of available print settings should you need to specify these from inside the script—I specified copies and target printer merely as two examples that appeared to be the most useful, but these aren't required).

Does this sound like it meets your needs ?

Your programmable keypad sounds like a good way to trigger this script after copying the search phrase to the clipboard. The alternative method I would have suggested for others who don't have such a device would be to create an Automator service, to which a shortcut (hotkey) can be assigned to trigger the script that way; or allow the service to be triggered after highlighting some text and right-clicking in order to send this text straight into the service as an alternative to copying it to the clipboard.

One final note: I chose mdfind in this instance because you implied that you wanted to search through every green-tagged file on your hard drive, and this does it faster than builtin AppleScript commands. However, if you only need to search green-tagged files in a specific folder or a couple of folders, it may be better to use builtin AppleScript commands (depending on how many files are in these folders). If you find files missing from the results of mdfind, you may need to re-index Spotlight (sudo mdutil -Ea from Terminal). It takes a while to rebuild the metadata store but any files omitted from the search results that should appear will now be there. You can preview the full list of search results in Terminal by entering this command: mdfind "kMDItemUserTags == 'Green'", which ought to list all green-tagged files.

Should you need any help or have any further queries, leave a comment and I'll get back to you. I can tweak the code if you have specific after-thoughts.

3

If you can process an array of file paths (instead of opening a Finder window), you can use the mdfind utility:

set greenFiles to paragraphs of (do shell script "mdfind \"kMDItemUserTags == 'Green'\"")
  • Hi CJK- perhaps I need to back up since I know what I know about Applescript and Automator, but it's not much and my knowledge falls off fast when I encounter the unfamiliar. Here's the workflow as I'm doing it now: I copy a phrase, open Finder and click on green tags. I copy/paste that phrase to find the filename that contains it. It then gets opened in Preview, from whence i print it. I do this hundreds of times a day, and have a programmable keypad that saves me lots of clicks. It'll run Applescripts, and I'm hoping to write or pay someone to write at least the script I'm discussing. Thx! – PHL Jan 18 '18 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .