First Script - Find files in a specific place

I’ve looked and so far haven’t found an answer to this.

I am trying to write an applescript in OS X 10.3.4 that will

  1. open the Find function of Finder, and select a specific folder to search in.
  2. set the search criteria to “contents”
  3. remove any additional search criteria left from the last search

If we want to get real fancy, the specific folder is on a server, so if the user is not currently connected we could do some error handling and open the dialog to connect to the server (this would be using the Finder’s Go → Connect to server function)

I am experienced in writing C++, and am having trouble switching to this “natural” language.

Likely, if someone could tell me how to find out what the different fields and objects in a particular window are, then I could write it myself.
But I will take all the help I can get.

Thanks in advance for the help. By the way, this is the best A.S. Forum i’ve found, so keep it up!

Dan

Unfortunatelly, the find-panel of the Finder is not scriptable, but you can try some *nix trick, such as:

paragraphs of (do shell script "find '/path/to/dir' -type f -exec grep -i 'searchTerm' {} /dev/null \;")

(substituting “/path/to/dir” to your selected directory and “searchTerm” with your own search term)

OK, thank you so much, jj, for the code you sent me. It does it’s job well. I have two more questions.

  1. How do I enter the path to a server location? The server is mounted, but I’m just not sure how to specify the path.

  2. How can I parse out the reults into a window or text file so that it shows the path to the file, and the filename, but not the other information. Making these clickable links (alias’) to the files would also be VERY handy.

Please excuse my naiveness, I am a very recent Mac Convert too.

Thanks again.

I’m trying to put in an error handler that verifies the path exists first.

something like this (this code doesn’t work, btw)


if not (exists path '/users/blah/desktop/dsld') then
	activate application "Finder"
	tell application "System Events"
		tell process "Finder"
			keystroke "k" using command down
		end tell
	end tell
end if

You can use this handler:

(I’ll come back later, I must take care now of a little baby)

Usually, “/Volumes/DISKNAME/dir/subdir” (that is, disks are mounted under the “/Volumes” folder).

This may get you started:

However, I’ve seen that the “find” method doesn’t work properly if you search for special characters… I’m writing a suite of utilities, so I added this one:

Cool. I’ve got everything working the way I want it, except for one little thing.
Each time results are returned to the finder, the first result is always to an index file. I do not want that to show up each time, and I’m not sure how to get rid of it.
Here’s the slightly modified code I’m working with now.


set x to quoted form of POSIX path of alias "path:name"
display dialog "Search Criteria" default answer "" buttons {"OK"} default button 1
set y to text returned of the result

try
	set theResult to paragraphs of (do shell script "find " & x & " -type f -exec grep -i " & y & " {} /dev/null \;")
	
	set aliasizedStuff to {}
	repeat with var in theResult
		set aliasizedStuff's end to text 14 thru ((offset of " matches" in var) - 1) of var as POSIX file as text
	end repeat
end try

if aliasizedStuff is {} then
	display dialog "No matches"
else
	choose from list aliasizedStuff
	if result is not false then tell application "Finder" to open {result as text as alias}
end if

If you don’t want show the first file, you can:

choose from list (rest of aliasizedStuff)

There is also a “default items” parameter for “choose from list”:

set x to {1, 2, 3}
choose from list x default items {x's item 2}
--> which will error, of course, if the list does not contain at least 2 items