Folks
I’m trying to search for (theString) in a folder of PDF files. I’ve assembled the following script which returns 0 results.
the folder i’m pointing to has 6 files in it, 3 of which contain the string “theString”
set thefolder to (choose folder)
set thefolder to quoted form of POSIX path of thefolder
tell application "Finder"
set theFiles to (do shell script "find " & thefolder & " -type f -name '*" & "theString" & "*.pdf' ! -name '.*'")
end tell
return count of theFiles
¢ if I get it to work, will this shell script search all subfolders or do I need to invoke “entire contents”?
¢ i also need to copy theFiles to another location- is it best to do an entire search, and and then a bulk copy, or one at a time (repeat count of items of thefolder)?
thanks,
Ralph
Well your code works for me in terms of finding all the files, but to get the count your one line should read as such
set theFiles to paragraphs of (do shell script "find " & thefolder & " -type f -name '*" & "theString" & "*.pdf' ! -name '.*'")
Thanks James-
what does “the paragraphs” do for this search?
thanks,
Ralph
the shell command without paragraphs returns a string of all the results. Paragraphs of splits the returned line at the breaks giving you a list of the files.
Thanks James-
I’ve been running the following script on a folder of 6 pdfs, three of which have the string “theString” in it their text, not their file names.
set sourceFolder to choose folder
set found to paragraphs of (do shell script "mdfind -onlyin " & (sourceFolder as alias) & space & "theString")
and getting “” as a result…
should this be working?
-Ralph
That should work, but you are referring to the source folder incorrectly, give this a try
set sourceFolder to (choose folder) as Unicode text
set found to paragraphs of (do shell script "mdfind -onlyin " & (quoted form of POSIX path of sourceFolder) & space & "theString")
Note that while using mdfind is extremely powerful and fast it will not work on volumes that does not have spotlight indexing turned on… namely server volumes.
James-
on the PowerPC G-5 with OS X 10.4.1.0
that still immediately returns “”
I’m just in the process of rebuilding my spotlight index (5 hours), per:
http://www.thexlab.com/faqs/stopspotlightindex.html#Anchor-49575
I’ll see if that helps.
On the MacBook Core 2 with OS X 10.5.2, the script takes 5 seconds to run or so, and then returns {}
An Acrobat and a spotlight search of the files finds theString
-Ralph
{several emails later}
Wow- that did it! Thanks!!
Here’s the whole subroutine. The file movement isn’t elegant, but (item 1 of found) is the folder. Now to see if this search works on a DVD-RAM disk…
Thanks James- I’m going to post this on the newsgroup as well.
-RL
set sourceFolder to (choose folder) as Unicode text
set destinationFolder to (choose folder) as Unicode text
set found to paragraphs of (do shell script "mdfind -onlyin " & (quoted form of POSIX path of sourceFolder) & space & "theString")
set x to 2
repeat ((count of items of found) - 1) times
set anitem to item x of found as Unicode text
tell application "Finder"
do shell script "cp " & quoted form of POSIX path of anitem & " " & quoted form of POSIX path of destinationFolder
end tell
set x to x + 1
end repeat
thanks again! 
-Ralph
Two alternate versions as well.
Version 1: Just a cleaner way to handle the loop
set sourceFolder to (choose folder) as Unicode text
set destinationFolder to quoted form of POSIX path of ((choose folder) as Unicode text)
set found to paragraphs of (do shell script "mdfind -onlyin " & (quoted form of POSIX path of sourceFolder) & space & "theString")
repeat with anitem in found
do shell script "cp " & (quoted form of POSIX path of anitem) & space & destinationFolder
end repeat
Version 2: Since we’re in the shell lets do it all there.
set sourceFolder to quoted form of POSIX path of ((choose folder with prompt "Choose a source folder") as Unicode text)
set destinationFolder to quoted form of POSIX path of ((choose folder with prompt "Choose a destination folder") as Unicode text)
do shell script "mdfind -onlyin " & sourceFolder & " -0 " & "theString" & " | xargs -0 -J {} cp {} " & destinationFolder
wow- that was super fast.
I had to wrap it in a “try” block because it was erroring on folders.
now i need to wrap my head around xargs…
-ralph
Doh, forgot about the try statement LOL =)