newbie to applescript, needs help.

Hey, I am trying to get a script to search a file name for only a numeric or alphanumeric combination. my files are in these formats: “2802282_hho_s.tif” “2802282_hho.tif”, or “h2802282_hho.tif”
I need to be able to search the numbers only before the underscore.

example.

if I have a list of numbers:
2820052
2846645
f2265688

That correspond to files:
2820052_chb.tif
2846645_acr_s.tif
f2265688_hho.tif

and I want any files that are not in my list to be marked, can I search for filenames containing numbers in txt file, instead of having to add the “_chb.tif” to every number in the list. I am going to be searching about a terabyte or more of images with different suffixes.

The current code I have allows me to get it to work, if I add the information after the underscore manually into the text file.

try
	set theFolder to (choose folder with prompt "Choose a folder.")
	set textFile to (choose file with prompt "Choose a text file." without invisibles)
	
	set fileNames to every paragraph of (read textFile as string)
	tell application "Finder"
		set label index of (every item whose name is not in fileNames) of entire contents of theFolder to 1
	end tell
on error theError
	display dialog theError buttons {"OK"} default button 1
end try

Anyone out there know how I can do this?

Well here goes, it isn’t very pretty, but in my limiting testing it’s working.

set theFolder to quoted form of POSIX path of ((choose folder with prompt "Choose a folder.") as Unicode text)
set textFile to (choose file with prompt "Choose a text file." without invisibles)

set fileNames to every paragraph of (read textFile as string)

set theCommand to "find " & theFolder & " -type f ! \\( -name \".DS_Store\" "

repeat with aName in fileNames
	set theCommand to theCommand & "-o -name \"*" & aName & "*\" "
end repeat

set theCommand to theCommand & "\\) -exec osascript -e 'on run argv' -e 'tell application \"Finder\" to set label index of (POSIX file argv as alias) to 1' -e 'end run' '{}' \\;"

do shell script theCommand

And here is a variation on the same theme, but should actually be faster for large file sets as we are only making one osa/finder calls rather than one for each [not] found file.

set theFolder to (choose folder) as Unicode text
set fileNames to paragraphs of (read (choose file))

set theOSAcommand to "'tell application \"Finder\" to set label index of files of entire contents of folder \"" & theFolder & "\" whose ("
repeat with aFile in fileNames
	set theOSAcommand to theOSAcommand & "name does not contain \"" & aFile & "\" and "
end repeat
set theOSAcommand to (text 1 through -6 of theOSAcommand) & ") to 1'"

do shell script "osascript -e " & theOSAcommand

Hopefully one of these two scripts should be enough to get you started!

Thanks for the replies, but I came across a couple of problems.

The first one didn’t seem to do anything.

On the second one I got this error:

33:350: execution error: Finder got an error: Can’t set label index of every file of entire contents of folder “Imac2:Users:toddrone:Desktop:Scripting:testfolder:” whose not 32 and not 32 and not 32 and not 32 and not 32 and not 32 to 1. (-10006)

The error highlighted this code:
do shell script "osascript -e " & theOSAcommand

A lot of the stuff you threw into these two are way over my head. I have been apple-scripting for about a week and a half now, out of necessity, but I am really starting to like it. amazing things can be done with it.

Hmm, both versions ran fine on a small test environment. Are you running on Tiger or on Leopard? (I’m on Leopard btw)

Let me know and I can take a look at it when I get into the office this morning.

Im on tiger.

I have a leopard machine, I will try it on there right quick, see if that makes a difference.

I received the same response in leopard.

I might be missing something, or screwing it up somehow.
I don’t know.

I doubt that you screwed anything up, but rather I made some assumption that is incorrect.

I sent you an email through the forum. Can you reply to that and include the text file you are using as well as some of the files you are testing with? (the full file names would suffice as well)

–sent from iPhone–

Yeah, I replied a few minutes ago, I had to upload the files to a host, my email wasn’t wanting to attach it.
I provided the links for each file.

Issue Solved!

Thanks James, I really appreciate it.

Solution: (incase anyone wanted to know)
James’ two scripts DID work, the reason they didn’t for me is because I had ‘empty lines’ in my text files.