Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: More expressive saved searches System 10.4
Tiger only hintI wanted to create a saved search (aka smart folder) of all papers -- PDF and Postscript -- I've downloaded in the last week. Unfortunately, the saved search functionality provided by the Finder didn't seem powerful enough to express this search: I could search for PDF files, or I could search for Postscript files, but not both. This hint describes how to access the more powerful expressive power of Spotlight.

Spotlight queries can be expressed using a simple query language, described here. The canned search criteria in smart folders use only a subset of the language. You can experiment with the language by using the command-line tool mdfind; you can also see example queries by invoking "Get Info" in the Finder on the saved search file. See the webpage above for more details.

To use the query language in a saved search, add a new criterion to the search and click "Other..." for the search attribute (the first field of the criterion, usually something like "Kind" or "Last Opened"). Then select "Raw Query" from the list of search attributes. Enter your query in the text field.

For example, to create the new papers query, I created a saved search with the criteria:
  • Created: This Week
  • Raw Query: ((kMDItemContentTypeTree = 'com.adobe.pdf') || (kMDItemKind = 'PostScript document'))
Presto!

[robg adds: This is an expansion of a previous hint that referenced a page of Spotlight search tips. The comments included the link to the above page on Apple's developer site, but I felt it was worth calling out in a hint of its own. The Raw Query is where the real power of Spotlight is hiding in the Finder...]
    •    
  • Currently 2.17 / 5
  You rated: 2 / 5 (6 votes cast)
 
[19,975 views]  

10.4: More expressive saved searches | 20 comments | Create New Account
Click here to return to the '10.4: More expressive saved searches' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: More expressive saved searches
Authored by: hellmachine on May 06, '05 08:49:05PM

doesn't "and" work?
like postscript and pdf?



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: restiffbard on May 06, '05 10:20:32PM

if you used and then the document would have to be both a pdf and a postscript. not many of those around. :)



[ Reply to This | # ]
10.4: Query syntax
Authored by: riccard0 on May 07, '05 09:14:09AM
Siracusa's review of Tiger lists the operators available for querying Spotlight.

[ Reply to This | # ]
10.4: Constants for your query
Authored by: Han Solo on May 07, '05 11:37:51AM
Yes, although a comprehensive discussion of the syntax is available from Apple. The article by Siracusa also notes that:
Each piece of metadata is stored in the Spotlight index using a key string. Apple has defined keys for many common attributes. These keys are defined as symbolic constants in a header file, using the "k"-prefixed Carbon naming convention for constants. They range from generic attributes like kMDItemKeywords, kMDItemTitle, and kMDItemComment to format-specific attributes like kMDItemAudioBitRate, kMDItemMaxAperture, and kMDItemLyricist. There are over 100 of these constants.
So knowing the query syntax, while important, is (arguably) less than half the battle. Turns out that a list (I don't know how complete) of these constants (keys) can be found in an Apple PDF as well. There are some unusual ones, including a whole host of audio-based ones (such as kMDItemAppleLoopsRootKey) -- perhaps an update to Garageband and/or Logic that will integrate Spotlight is in the works.

The bottom line, however, is that if developers do not adhere to Apple's keys, and instead start using their own overlapping and redundant ones, Spotlight will end up being much less useful than it could be. Sure, Apple can exhort developers to "play along," but why do I envision kMDMicrosoftAuthors, etc., suddenly appearing in the next version of Office...?

[ Reply to This | # ]

10.4: More expressive saved searches
Authored by: adrianm on May 07, '05 02:55:15AM
Weird mix you've got there; surely:

(kMDItemContentTypeTree = 'com.adobe.pdf') || (kMDItemContentTypeTree = 'com.adobe.postscript')
would be better?

[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: njnystrom on May 07, '05 01:10:41PM
Yes, that's much cleaner. I noticed this too a few hours after submitting the hint. ObHint: To figure out the type of a particular file, you can just do mdls on the file from the terminal.

---
Nate

[ Reply to This | # ]

10.4: Using Unix 'mdfind' and spaces in names different from old 'find' syntax, but how?
Authored by: meckardt on May 09, '05 10:36:47AM
Okay here is the Script, which does not work because of the spaces in filenames and paths:

for i in `mdfind "kMDItemKind=='*Adobe Photoshop file*' && kMDItemColorSpace =='*CMYK*' && kMDItemResolutionWidthDPI > '72'"`

do
cp $i /Users/meckardt/Desktop/
done
 
Result:
cp: Tiger: No such file or directory
cp: 2005/3908497213.psd: No such file or directory
cp: /Volumes/Macintosh: No such file or directory

I know it must be possible, but how? I am totally stuck. How am I supposed to write a script with mdfind and use the results for cp or mv? The pathnames do have a lot Spaces and mdfind & xargs do not behave the same as find & xargs did before.

Thanks, Matt

[ Reply to This | # ]

10.4: Using Unix 'mdfind' and spaces in names different from old 'find' syntax, but how?
Authored by: huzzam on May 10, '05 04:06:34AM

Try putting double quotes around the $i, thus:

cp "$i" /Users/meckardt/Desktop/



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: captainhaddock on May 07, '05 06:55:12AM

That's awesome! I've been trying to make a smart folder to hold just images and PDFs that I use as client proofs. Choosing two file types should default to "or" logic, since it makes no sense to ask for a file that is two different things at once.

So how do you know what to put in the Spotlight query? What are all the available flle types?



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: fabrizio on May 07, '05 07:44:32AM
And it supports wildcards! Try kMDItemKind = '*PDF*'

[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: bnmnetp on May 07, '05 09:08:21AM

Now if only there was a way to get to this expressive functionality in Mail.app rules. I'm tired of making multipe rules just to get (x and y and z) or (a and b)
I don't see a way to get to a Raw query throught the mail interface. Is it there and I'm missing it?



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: rguerra on May 07, '05 04:55:09PM

I too would like to use more expressive/complex searches in Apple Mail 2.0. I look forward to hear if it's possible, and if so - how...



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: bkperry on May 08, '05 09:18:01AM

I'm trying to use a saved folder to eliminate a pet peeve of mine. I have a folder for all downloads, which at times may hold up to hundreds of files. I always view it by date modified, and if I am lucky, newer downloads float at least near the top because they were created recently. However, if I download a file or say a PDF that was created a year ago, it shows up far down the list and can be hard to find. I get around this by clicking on the "Show in Finder" button in the downloads list in Safari. But...

I would like to have a smart folder that can grab all downloads from the past two weeks, a much shorter and more manageable list. However, it's proving difficult b/c I can't find any identifier that would label a file's date of download. Date created doesn't work, as far as I can see- it goes off of the date the actual file was created by the author. Date modified doesn't work b/c the file is not modified as it is downloaded. I tried using Automator to hit the file with a comment or color label as it comes into the downloads folder, but no dice- the file doesn't reflect a new modification date.

Any suggestions? Thanks!



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: DistantThunder on May 08, '05 01:51:11PM

I almost hate to suggest a haxie as a solution (although they've never given me any problems), but since it comes so close to what you are trying to do, here it is: The "Labels X" haxie from Unsanity.com has an option you can enable called "Changing Label Changes File Modification Date" -- which I think would work for what you are trying to do. Perhaps there is a way to enable that functionality without the haxie, but someone else would have to pitch in that solution.



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: tanvach on May 08, '05 07:06:02PM

Attach this script as Folder action to your download folder. It uses do shell script to call 'touch' on every items added to the folder.

on adding folder items to this_folder after receiving these_items
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
do shell script "touch \"" & POSIX path of this_item & "\""
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to


As the web browser saves the downloaded files, this script is called and the file will have the modification time updated.



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: tanvach on May 08, '05 03:46:23PM

You could try using run applescript block, with use 'do shell script' and 'touch' shell command. I'm not sure how to put everything together since I'm still waiting for my copy of tiger, but once I've got the copy I would love to have this functionality!



[ Reply to This | # ]
Improvement on tanvach's script + instructions
Authored by: sjonke on May 09, '05 12:30:11PM
I really like tanvach's idea for a folder action, and I'll offer this small improvement over it, which will make the Finder refresh it's view of the touch'd file and thus update the listing immediately:
on adding folder items to this_folder after receiving these_items
  try
    repeat with i from 1 to number of items in these_items
      set this_item to item i of these_items
      do shell script "touch \"" & POSIX path of this_item & "\""
      tell application "Finder"
        update this_item
      end tell
    end repeat
  on error error_message number error_number
    if the error_number is not -128 then
      tell application "Finder"
        activate
        display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
      end tell
    end if
  end try
end adding folder items to
How to use: copy and paste the script into a new script in ScriptEditor. Save as as compiled script to the folder: /Library/Scripts/Folder Action Scripts/ Then follow the instructions to attach it to your Downloads folder. Note that I would only recommend using this if you have a dedicated downloads folder - I wouldn't, for example, attach this to your Desktop!!

---
--- What?

[ Reply to This | # ]

Improvement on tanvach's script + instructions
Authored by: bnmnetp on May 09, '05 06:31:52PM

You can do this really easily with Automator.
1. Create an automator action that runs a shell command
2. Click the button that says use input as arguments
3. the command to run is touch
4. Save as plug-in (for folder actions) Attach to your download folder.



[ Reply to This | # ]
Some clients set the DL date
Authored by: VRic on May 10, '05 12:11:04PM

The problem you describe depends on the software used to download files: sure Safari TRIES to set the local file's date to the original's (and fails when the server won't provide that info), but others (like iCab) always set the local file's creation date to its, well, creation date (downloads aren't *copies* in the Finder's sense, as they don't occur between 2 filesystems but from one indeterminate data stream to a file).

I would strongly encourage web browser's authors to pick the only reliable way of dating downloads for reasons noted below. Both methods have benefits, but experience has shown me that *trying* to replicate the original dates was:

(a) not worth the effort - since it often fails or results in meaningless information (for example many files "generated" on the fly server-side revert to the date of DL, which tells nothing of their content's creation date: 2 downloads of the same "file" will then have different dates)

(b) confusing - since (a) means you can't guess the result beforehand (some will have "proper" creation dates and other won't, whatever "proper" means to you, but there's no way you could tell without checking)

(b') even more confusing! - since (a) also means you can't guess AFTER the fact: when, after a few days, you'll have forgotten the instant where you donwloaded a file, there will be absolutely no way of knowing if a file's dates come from the original dates or were reverted to the download date by lack of such info from the server

(c) counter-intuitive and user-hostile - since a user's guess and most likely question wouldn't be the orginal date (unknown to him) but the moment he downloaded the file (I realize this point is debatable because most OTHER means of transfering files dutifully preserve dates, because they actually transfer files between filesystems or provide dedicated front-ends to replicate that effect, but this was never the purpose of HTTP)

(d) mostly useless - since most files downloaded from web pages are archives, which creation dates are usually meaningless to the end user (their content's isn't, but their content will have proper dates after extraction; the archive's dates only have sense for authors and webmasters, who don't use HTTP to transfer files to/from their own sites so they have no problem preserving dates anyway)

(e) extremely annoying in actual use - being forced to use both methods (reverting to Safari when iCab beta 3 still chokes on some pages or when my iCab ads filters prove too harsh), I discovered that for the reasons above Safari's way added close to zero usefulness and a LOT of confusion, while iCab's systematic (thus reliable) dating of downloads allowed plenty convenient uses with minimal brain drain (meaning bozos immediately grasp the idea of sorting by date to find what they just clicked on, and uber-bozos like me soon realize that the delta between creation and modification dates also has meaning, exposing the actual data throughput of the DL, this last feature being probably alien to Safari's temporary-downloads-in-bundles-to-be-replaced-later way of breaking things).


This not to suggest the use of iCab, which has its own set of problems (especially since beta 3 still isn't publicly available even though it dates back to last christmas), but maybe to consider other smarter browsers than Safari, or to file bug reports requesting iCabification of other browsers on that front, too, in addition to download manager, pages saving, content filters, searches, etc.



[ Reply to This | # ]
10.4: More expressive saved searches
Authored by: edchrist on May 11, '05 03:10:49AM

In Spotlight there is Last Opened which is when file or folder was accessed which may give you a truer list?



[ Reply to This | # ]