Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Spotlight command-line - Saved Search access?

Spotlight command-line - Saved Search access?
Thread Tools
Forum Regular
Join Date: Dec 2004
Status: Offline
Reply With Quote
Jun 19, 2005, 02:58 AM
 
Hi all,

I was under the impression saved searches would look like directories with files in them to the command line. Instead they're files named "dirname.savedSearch".

Is there a way to query a savedSearch for the files it contains?

mdfind would be PERFECT for this, if only it took a -savedSearch option as a source for the search attributes.

Any ideas guys? Thanks,

Rob
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Jun 19, 2005, 05:36 AM
 
Here, I wrote this awhile ago. Save it as `spotlightls' or any other name, and either use it by calling it separately, or just add `alias ls=/path/to/spotlightls' to your .bashrc.

Code:
#!/usr/bin/perl # # FILE: spotlightls # AUTHOR: Mithras The Prophet (mithras.the.prophet, which is a gmail account) # DATE: April 2005 # # DESCRIPTION: a `ls' replacement that will automatically expand # any 'foo.savedSearch' parameters into the list of files produced # by that Spotlight search # # e.g. # [mithras] ls -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 # # CHANGELOG # v.1.0.1 - 4/24/2005, used fork to pass query to mdfind without shell interfering # v.1.0.0 - 4/24/2005 # use warnings; use strict; my @newargs; for (my $i=0; $i < scalar @ARGV; $i++) { # if this is a .savedSearch, get the results! if ($ARGV[$i] =~ /\.savedSearch$/) { push(@newargs, execute_savedSearch($ARGV[$i])); } else { # otherwise, just add this directly to our arglist push(@newargs, $ARGV[$i]); } } # if we didn't get any results from saved searches, # we won't run `ls', (otherwise it will do a `ls .', which we don't want) # # but note that if we were passed no parameters at all, # we do indeed want to run a plain `ls' with no parameters # if ((scalar @ARGV == 0) || scalar @newargs > 0) { exec("ls",@newargs); } sub execute_savedSearch { my @results; while (scalar @_ > 0) { my ($thisarg) = shift (@_); # print STDERR "Expanding saved search: $thisarg\n"; # parse the searchfile my $query = ""; my @searchLocations; open (INFILE, "$thisarg") or die("Can't read saved search: '$thisarg'\n"); my $query_next = 0; my $searchlocation_next = 0; while (<INFILE>) { if (m/<key>RawQuery<\/key>/) { $query_next = 1; } elsif (m/<key>FXScopeArrayOfPaths<\/key>/) { $searchlocation_next = 1; } elsif (m/<\/array>/) { $query_next = 0; $searchlocation_next = 0; } elsif ( $query_next && m/<string>([^<]+)<\/string>/ ) { $query = $1; $query_next = 0; $query =~ s/&amp;/&/g; $query =~ s/&lt;/</g; $query =~ s/&gt;/>/g; } elsif ( $searchlocation_next && m/<string>([^<]+)<\/string>/ ) { my $thisloc = $1; $thisloc =~ s/&amp;/&/g; $thisloc =~ s/&lt;/</g; $thisloc =~ s/^gt;/>/g; $thisloc =~ s/kMDQueryScopeHome/$ENV{HOME}/ge; $thisloc =~ s/kMDQueryScopeComputer/\//g; push (@searchLocations, $thisloc); } } close (INFILE); # now, run and mdfind over each of the search locations: # print STDERR "\tquery: $query\n"; foreach my $searchLoc (@searchLocations) { # print STDERR "\tlocation: $searchLoc\n"; # we'll fork, and run mdfind # so we can pass the query parameter to mdfind without # the shell interpreter interfering my $pid = open(KID_TO_READ, "-|"); if ($pid) { # parent while (<KID_TO_READ>) { # add each result to our final results array chomp; push (@results, $_); } close(KID_TO_READ) || warn "kid exited $?"; } else { # child exec("mdfind", '-onlyin',$searchLoc,$query) || die "can't exec program: $!"; # NOTREACHED } } shift; } return @results; }
     
rslifka  (op)
Forum Regular
Join Date: Dec 2004
Status: Offline
Reply With Quote
Jun 19, 2005, 11:15 AM
 
dude... dude.... DUDE this is awesome! Thank you so much man, it works like a charm.



Rob
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:48 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2