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
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).
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: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
andtarget 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 ofmdfind
, 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.