3

I have a folder of about 350 images (they are scanned recipes). To make finding them easier, I have written a search program in bash shell script. I have bash version 3.2 on Mac OS X 10.8 Mountain Lion.

The basic idea of my program:

  1. Ask the user for search terms (using osascript).
  2. Search the file (names) for the search terms, using mdfind.
  3. Send the matching files to ln (through xargs).
  4. Make a hardlink of matching files in a "results" directory. This directory is contained within the same folder of images (this directory is cleaned at the beginning of the program).

What the directories look like:

+Recipes
  -files
  -files
  -files
  +.search
     -search (shell script)
     +results
        -links
        -links

Here's my code:

#!/bin/bash
#
# Search program for recipes.
# version 2

# Clean out results folder
rm -rf ~/Google\ Drive/Recipes/.search/results/*

# Display the search dialog
query=$(osascript <<-EOF
        set front_app to (path to frontmost application as Unicode text)
        tell application front_app to get text returned of (display dialog "Search     for:" default answer "" with title "Recipe Search")
EOF)

# If query is empty (nothing was typed), exit
if [ "$query" = "" ]
then
    exit
fi

echo "Searching for \"$query\"."

# Search for query and (hard) link. The output from 'ln -v' is stored in results
# 'ln' complains if you search for the same thing twice... mdfind database lagging?
results=$(mdfind -0 -onlyin ~/Google\ Drive/Recipes/ "$query" | xargs -0 -J % ln -fv % ~/Google\ Drive/Recipes/.search/results)

if [ "$results" ]
then
    # Results were found, open the folder
    open ~/Google\ Drive/Recipes/.search/results/
else
    # No results found, display a dialog
    osascript <<-EOF
        beep
        set front_app to (path to frontmost application as Unicode text)
        tell application front_app to display dialog "No results found for \"" & "$query" & "\"." buttons "Close" default button 1 with icon 0
    EOF
fi

It works fine—the first time. If you search for the same thing twice, it breaks.

Explanation: let's say I search for "chicken". 34 files match, and hard links are made in the results directory.

Now, I run the program again, and search for the same thing—"chicken". The directory is emptied (by rm). But now, the finding/linking stops working—only 6 or 7 recipes will be linked. What seems to be happening is that mdfind is finding the results in the search directory, after they have been deleted, and then ln can't make links. But it doesn't find the main files... I get

ln: ~/Google Drive/Recipes/.search/results/recipe.jpg: no such file or directory

I looked at mdfind used for creating symlinks not working as expected; they have a similar problem (but no help).

Thanks for any help... this has been annoying me for a long time.

Community
  • 1
  • 1
baum
  • 959
  • 13
  • 27
  • 1
    Interesting...what happens if you place the search results outside the search tree, so that `mdfind` would never look at the search result files in their linked location? Does it behave any better? Since the `mdfind` command 'consults the central metadata store and returns a list of files that match the given metadata query', the metadata repository may be out of date in some way, having just added files that no longer exist and it hasn't yet noticed their absence. I'm not sure how that would lead to the symptoms you see, but caching can lead to confusion when things change unexpectedly. – Jonathan Leffler Aug 29 '12 at 00:47
  • The problem in the linked question is to do with the way they've generated and executed the command; it is different from the problem you're describing. – Jonathan Leffler Aug 29 '12 at 00:53
  • would `rehash` apply here? Good luck to all. – shellter Aug 29 '12 at 01:23
  • @Jonathan: I tried moving the results directory outside the search tree... and mdfind still searched inside it! (even though I told it not to.) I'll try that again, but I get the same error. I agree that the problem is probably with a slow database; just not sure how to update it. – baum Aug 29 '12 at 19:28
  • @shellter: I don't have that on my system (osx 10.8) – baum Aug 29 '12 at 19:29

2 Answers2

1

You can re-index directories or files with mdimport.

$ touch aa
$ mdfind -onlyin . -name aa
/Users/lauri/Desktop/aa
$ rm aa
$ mdimport .
$ mdfind -onlyin . -name aa
$ 

Spotlight doesn't index directories that start with a period, so you could just rename the .search directory.

Lri
  • 26,768
  • 8
  • 84
  • 82
0

The Spotlight commands use a cached index of the file contents. I don't know of any way to force mdfind not to do so. The brute force approach is to clear the cache with mdutil -E after removing the files, but you may not want to do that.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175