Applescript: open finder window displaying all files with certain tag. Possible

applescriptfinder

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!

Best Answer

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.