Applescript to label files from a queried list

Discussion in 'Mac Programming' started by The Butsmeister, Jan 17, 2008.

  1. The Butsmeister macrumors newbie

    Joined:
    Jan 17, 2008
    #1
    Hi,

    Does anyone know of a way to label files on OSX using a text file as a reference.
    So I have a text file with a list of Mac files that I want to query and get the Finder to label any files that exist in a designated folder on the Mac BUT only if they appear in the referenced text file.

    thanks
     
  2. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #2
    General example:

    Code:
    set theFolder to alias "MacHD:Users:Foo:foldername:"
    set fileNames to {"filename1", "filename2"}
    tell application "Finder"
        set label index of (every item whose name is in fileNames) of theFolder to 1
    end tell
    If you want more specific advice you'll need to provide a sample of the text file's contents.
     
  3. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #3
    RE: Applescript to label files from a queried list

    Thanks for your response hhas.
    I've attached a screenshot to show what I'm trying to achieve.
    Basically I want to label any eps file in the Images2 folder to red if the name exists in the text file called imagename. So in this instance the only eps image not to get labelled is BLMAH096P.eps
    I guess I need to query each line of text within the text file to see if it matches an image name but I'm not sure how to do this
    Many thanks
     

    Attached Files:

  4. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #4
    See if this works.
    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) as file specification
    	
    	open for access textFile with write permission
    	set fileNames to (read textFile using delimiter "
    ")
    	close access textFile
    	
    	tell application "Finder"
    		set label index of (every item whose name is in fileNames) of theFolder to 2
    	end tell
    on error theError
    	display dialog theError buttons {"OK"} default button 1
    end try
     
  5. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #5
    This can be simplified a little, plus I'd recommend avoiding the 'read' command's 'using delimiter' parameter and splitting the text using 'every paragraph of...' as it's more reliable. Depending on the text file's encoding (primary, UTF8, UTF16), you may also need to specify 'string', '«class utf8»' or 'Unicode text' for the 'read' command's 'as' parameter.

    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 in fileNames) of theFolder to 2
    	end tell
    on error theError
    	display dialog theError buttons {"OK"} default button 1
    end try
    HTH
     
  6. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #6
    Oh cool, I didn't know you could do that! That's much better. :)
     
  7. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #7
    RE: Applescript to label files from a queried list

    Unfortunately can't get this to work for me, get the following error shown in attachment
     

    Attached Files:

  8. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #8
    RE: Applescript to label files from a queried list

    Has anyone got any ideas what the previous finder error message means in respect of the script?
     
  9. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #9
    Your file is saved in rich text format (.rtf). Either:

    1. Resave it as a plain text (.txt) file. I'd suggest using the UTF8 encoding, and modifying the 'read as string' command in the previous script to 'read as «utf8»'.

    Or, if that's not an option:

    2. Have AppleScript open the file in TextEdit and get the text from from there:

    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)
    	
    	tell application "TextEdit"
    		open textFile
    		set fileNames to every paragraph of text of document 1
    		close document 1
    	end tell
    	
    	tell application "Finder"
    		set label index of (every item whose name is in fileNames) of theFolder to 2
    	end tell
    on error theError
    	display dialog theError buttons {"OK"} default button 1
    end try
     
  10. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #10
    RE: Applescript to label files from a queried list

    Hmm, now I get a different message after trying both methods HHas suggested . . . sorry to be a pain, but I'm missing something here
     

    Attached Files:

  11. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #11
    RE: Applescript to label files from a queried list

    Hi all,
    Thanks hhas in helping me with this.
    Now works fine using utf8 and making sure that hidden file extension in image name is included in text file.
     
  12. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #12
    rE: Applescript to label files from a queried list

    Just a further question on this script.
    How do I specify if I want to search for filenames in a subfolder so that rather than looking into the designated folder, I can search through all folders within this folder?

    thanks
     
  13. The Butsmeister thread starter macrumors newbie

    Joined:
    Jan 17, 2008
    #13
    re: Applescript to label files from a queried list

    Answered my own question here . . . but thought I would post it for reference

    set label index of (every item whose name is in fileNames) of entire contents of theFolder
     
  14. thighofjustice macrumors newbie

    Joined:
    May 12, 2008
    #14
    A question regarding this thread.

    I found this thread, and it fits perfectly with something I am working on..

    it works great, but is there a way to have the filenames to be searched with "contains name", instead of the exact name?

    example.

    if I have a list of numbers:
    2820052
    2846645
    2265688

    That correspond to files:
    2820052_chb.tif
    2846645_acr_s.tif
    2265688_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.

    something like:

    Code:
    tell application "Finder"
    		set label index of (every item whose name contents is not in fileNames) of entire contents of theFolder to 2
    
    
    
    Thanks for the help.
     
  15. Jensend macrumors member

    Joined:
    Dec 19, 2008
    #15
    How would I modify this script so it only checks the 4rd through 9th character of the file name against the text list?
     

Share This Page