 |
 |
Spotlight command-line - Saved Search access?
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2004
Status:
Offline
|
|
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
|
|
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/&/&/g;
$query =~ s/</</g;
$query =~ s/>/>/g;
}
elsif ( $searchlocation_next && m/<string>([^<]+)<\/string>/ ) {
my $thisloc = $1;
$thisloc =~ s/&/&/g;
$thisloc =~ s/</</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;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2004
Status:
Offline
|
|
dude... dude.... DUDE this is awesome! Thank you so much man, it works like a charm.
Rob
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|