Can an applescript delimiter fix my problem?

Discussion in 'Mac Programming' started by thighofjustice, May 12, 2008.

  1. thighofjustice macrumors newbie

    Joined:
    May 12, 2008
    #1
    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 the files to be marked, can I search for filenames contain 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.

    The current code I have (thanks to Hhas in another thread) allows me to get it to work, if I add the information after the underscore manually into the text file.

    Code:
    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 3
    	end tell
    on error theError
    	display dialog theError buttons {"OK"} default button 1
    end try
    
    Anyone out there know how I can do this?
     
  2. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #2
    Give this a try.


    Code:
    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)
    repeat with aFile in fileNames
    	tell application "Finder"
    		try
    			set label index of (every item whose name starts with aFile) of entire contents of theFolder to 3
    		on error theError
    			--display dialog theError buttons {"OK"} default button 1
    		end try
    	end tell
    end repeat
     
  3. thighofjustice thread starter macrumors newbie

    Joined:
    May 12, 2008
    #3
    thanks for the quick reply, but I ran into a problem with it.

    it isn't setting the label for the items not in the list, it is marking everything still.

    I have been apple-scripting for about a week and a half now, so please bear with me, as most of these things are over my head.
     
  4. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #4
    Can you live with using the files that don't get marked?
     
  5. thighofjustice thread starter macrumors newbie

    Joined:
    May 12, 2008
    #5
    This issue is now solved.

    Thanks to Everyone for your help.

    Solution: (provided by James Nierodzik)

    Code:
    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
     
  6. MyndCraft macrumors newbie

    Joined:
    Feb 14, 2003
    Location:
    Chicago
    #6
    And here was the other solution I provided.

    Code:
    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
    	if (count aFile) is greater than 0 then 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
    
     

Share This Page