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

10.4: Access Spotlight saved searches via the Terminal UNIX
Tiger only hintTiger's new "Saved Search Folders" are convenient in the Finder. But what if you want to access search results from the command line, without retyping the entire search with mdfind? I've written two Perl scripts to help do just that, made possible by the fact that a .savedSearch file is just an XML file containing your search criteria.

spotlightls (view source) works just like the regular ls, except that it will scan the parameters you feed it, and expand any .savedSearch file to the results of that search.

cache_search_results (view source) will look in ~/Library/Saved Searches/ for .savedSearch files. For each one it finds, it will create a directory of the same name (without the '.savedSearch'), and fill it with symlinks to the results of the search.

Read on for examples of both...

Here's spotlightls (linebreaks added for a narrower display):
$ spotlightls -l NewPDFs.savedSearch
 -rw------- 1 mithras wheel 9002 Apr 21 11:32 /Users/mithras/.Trash/
   Web Receipts/Untitled.pdf
 -rw-r--r-- 1 mithras wheel 225031 Apr 22 12:28 /Users/mithras/
   Documents/alon_liebler_199.pdf
And of cache_saved_searches (linebreaks added for a narrower display):
$ cache_saved_searches
Caching NewPDFs.savedSearch...
        j.0029-4624.2005.00492.x-1.pdf
        j.0029-4624.2005.00492.x.pdf
        science.pdf
$ spotlightls -l NewPDFs/
total 112
lrwxr-xr-x   1 mithras  wheel   45 Apr 24 16:32 Untitled.pdf -> 
  /Users/mithras/.Trash/Web Receipts/Untitled.pdf
lrwxr-xr-x   1 mithras  wheel   86 Apr 24 16:32 alon_liebler_199.pdf -> 
  /Users/mithras/Documents/alon_liebler_199.pdf
To use these scripts:
  1. Save the text of the script into a file with the name you want (obviously, I suggest spotlightls and cache_saved_searches, but anything will do).
  2. Put the files somewhere appropriate, e.g. ~/bin -- that folder should be in your path; add export PATH="$HOME/bin:$PATH" to your .bashrc
  3. Make them executable with chmod a+x scriptname
  4. If you want spotlightls to replace your regular ls, add this to your .bashrc: alias ls="spotlightls"
[robg adds: I haven't tested these scripts...]
    •    
  • Currently 1.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[10,214 views]  

10.4: Access Spotlight saved searches via the Terminal | 2 comments | Create New Account
Click here to return to the '10.4: Access Spotlight saved searches via the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Access Spotlight saved searches via the Terminal
Authored by: baltwo on May 03, '05 04:00:13AM

Looks good, although I haven't tested them. However, I recommend making those environmental path changes/additions to your ~/.profile file. That makes them shell independent.



[ Reply to This | # ]
10.4: Access Spotlight saved searches via the Terminal
Authored by: raider on Oct 11, '05 11:39:03AM
The cache saved searches script works as advertised.

An added plus is that the symbolic links are 40k smaller than a finder "Alias" and also, are not subject to the "too many aliases bug" in Tiger 10.4.2. (more info here and here)

The cache saved searches script can be modified to use the current directory, instead of the users saved searches directory - so that you can run it anywhere, in case you don't keep all your saved searches in one place....

In the cache_saved_searches script change this:
# try to read contents of ~/Library/Saved Searches/
my $saved_searches_dir = $ENV{HOME} . "/Library/Saved Searches";
opendir (SS_DIR, $saved_searches_dir)
	or die("Can't find saved searches in '$saved_searches_dir'n");
to this:
# try to read contents of current directory.
use Cwd;
my $saved_searches_dir = cwd();
opendir (SS_DIR, $saved_searches_dir)
	or die("Can't find saved searches in '$saved_searches_dir'n");


[ Reply to This | # ]