how to make search script faster ?

Hi

The script below is a portion of a larger script I use to search a external hard drive on a local Mac, for specific post script files, then when found have them duplicated to a hot folder for ripping.

This used to be quite quick when I only had say 500/1000 files to search through, but now I have about 20,000 plus .ps files to search through,(approx 130gb) it has become extremely slowwwwww, and if I have to search the external drive over the network (not very often), it takes ages.

could some one help me to make the code work faster please?

set _path to quoted form of POSIX path of "Macintosh HD:Users:develop:Desktop:RIP FILES:" --FOR TESTING
set _CutOff to "12345-TEST"
set _selecteditem to (_CutOff as string) & ".ps"

set foundfFiles to paragraphs of (do shell script "/usr/bin/find " & _path & " -type f -name '" & _selecteditem & "'")
set FileToOpen to item 1 of foundfFiles as POSIX file

tell application "Finder"
	set anItem to FileToOpen
	
	tell (info for anItem) to set {tName, tEx} to {name, name extension}
	set baseName to text 1 thru ((get offset of "." & tEx in tName) - 1) of tName
	
	if baseName contains "TEST" then
		duplicate FileToOpen to "Macintosh HD:Users:develop:Desktop:HOT FOLDER A:" with replacing
	end if
end tell

Just a comment :

You use ]infos for[/u] from the Standard Additions.

(1) calling it in a tell block isn’t a good idea.

Would be better with :


set _path to quoted form of POSIX path of "Macintosh HD:Users:develop:Desktop:RIP FILES:" --FOR TESTING
set _CutOff to "12345-TEST"
set _selecteditem to (_CutOff as string) & ".ps"

set foundfFiles to paragraphs of (do shell script "/usr/bin/find " & _path & " -type f -name '" & _selecteditem & "'")
set FileToOpen to item 1 of foundfFiles as POSIX file

  set anItem to FileToOpen
   
   tell (info for anItem) to set {tName, tEx} to {name, name extension}
   set baseName to text 1 thru ((get offset of "." & tEx in tName) - 1) of tName


   if baseName contains "TEST" then
tell application "Finder"
       duplicate FileToOpen to folder "Macintosh HD:Users:develop:Desktop:HOT FOLDER A:" with replacing -- ADDED folder !
end tell
   end if


(2) infos for is officially deprecated. Don’t be surprised the day your script will fail.

Yvan KOENIG (VALLAURIS, France) jeudi 29 septembre 2011 22:14:14

thanks for the heads up on “info for” & the tell block Yvan, what do I replace “info for” with?, and how do I make the search faster?

Been nosing around and found this tutorial written by Adam, very interesting, I think mdfind is what i’m looking for, seems ultra fast, could some one show me how to incorporate/change my code to use mdfind to speed up my search set up.?

http://macscripter.net/viewtopic.php?id=24765

System Events is the recommended tool.


tell application "System Events" to tell disk item anItem
set  {tName, tEx} to {name, name extension}
end tell

Yvan KOENIG (VALLAURIS, France) vendredi 30 septembre 2011 09:45:17

thanks for that, ive added the new code, but now I get an error thrown, and im not sure how to correct it.

System Events got an error: Can’t make file “Macintosh HD:Users:develop:Desktop:RIP FILES:12345-TEST.ps” into type integer.

set _path to quoted form of POSIX path of "Macintosh HD:Users:develop:Desktop:RIP FILES:" --FOR TESTING
set _CutOff to "12345-TEST"
set _selecteditem to (_CutOff as string) & ".ps"

set foundfFiles to paragraphs of (do shell script "/usr/bin/find " & _path & " -type f -name '" & _selecteditem & "'")
set FileToOpen to item 1 of foundfFiles as POSIX file

set anItem to FileToOpen

--tell (info for anItem) to set {tName, tEx} to {name, name extension}
tell application "System Events" to tell disk item anItem
	set {tName, tEx} to {name, name extension}
end tell
set baseName to text 1 thru ((get offset of "." & tEx in tName) - 1) of tName


if baseName contains "TEST" then
	tell application "Finder"
		duplicate FileToOpen to folder "Macintosh HD:Users:develop:Desktop:HOT FOLDER A:" with replacing -- ADDED folder !
	end tell
end if

I have been trying various ways to get this system events piece of code to function the way “info for” did in my original post but cannot get it to work at all.

I thought the problem was “set FileToOpen to item 1 of foundfFiles as POSIX file” so went through trying various different ways to eliminate my first error issue, which I think I have solved, but now i’m getting another error, thrown from the finder tell block

Finder got an error: Can’t make “/Users/develop/Desktop/RIP FILES//12345-TEST.ps” into type item.

so now i’m not really sure if my change to "set FileToOpen to first item of foundfFiles " earlier in the code is the issue now, or whether it is correct at all

--set FileToOpen to item 1 of foundfFiles as POSIX file -- origional set up

--set FileToOpen to (POSIX file foundfFiles)  -- failed
--set FileToOpen to (my POSIX file foundfFiles)  -- failed
--set FileToOpen to foundfFiles  -- failed
--set FileToOpen to POSIX file (item 1 of foundfFiles)  -- failed
set FileToOpen to first item of foundfFiles --this appears to work

woohooo progress, re-wrote the finder set up and dropped some code that wasn’t doing anything, pretty fast,
can the code that I have now be improved on to get it faster or is this it?

set _path to quoted form of POSIX path of "Macintosh HD:Users:develop:Desktop:RIP FILES:"
set _CutOff to "12345-TEST"
set _selecteditem to (_CutOff as string) & ".ps"

set foundfFiles to paragraphs of (do shell script "/usr/bin/find " & _path & " -type f -name '" & _selecteditem & "'")
set FileToOpen to first item of foundfFiles

tell application "System Events" to tell disk item FileToOpen
	set {tName, tEx} to {name, name extension}
end tell
set baseName to text 1 thru ((get offset of "." & tEx in tName) - 1) of tName

if baseName contains "TEST" then
	tell application "Finder"
		duplicate file foundfFiles to folder "Macintosh HD:Users:develop:Desktop:A:" with replacing
	end tell
end if

Yvan KOENIG (VALLAURIS, France) lundi 3 octobre 2011 11:01:11