Resolved Applescript: List/txt.file question

Discussion in 'Mac Programming' started by h1068, Jul 3, 2012.

  1. h1068, Jul 3, 2012
    Last edited: Jul 3, 2012

    h1068
    Expand Collapse
    macrumors newbie

    Joined:
    Jun 10, 2012
    #1
    Can I set the_user_list to read a text file of users separated by commas?

    Code:
    
    set the_user to do shell script "logname"
    set the_user_list to 
    if the_user is in the_user_list then
    	
    
     
  2. wrldwzrd89
    Expand Collapse
    macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    Absolutely you can!

    Assuming you know where the text file with the list of users in it is located, this little helper function will allow you to format the returned raw data into an AppleScript list:

    Code:
    on text_to_list(txt, delim)
    	set saveD to AppleScript's text item delimiters
    	try
    		set AppleScript's text item delimiters to {delim}
    		set theList to every text item of txt
    	on error errStr number errNum
    		set AppleScript's text item delimiters to saveD
    		error errStr number errNum
    	end try
    	set AppleScript's text item delimiters to saveD
    	return (theList)
    end text_to_list
    
    To use this:
    Code:
    set the_user_list to my text_to_list(text_var, ",")
     
  3. kryten2
    Expand Collapse
    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    Try this :

    Code:
    set theFile to POSIX file "/Users/test/testtxt.txt"
    set theUserList to words of (read theFile)
     

    Attached Files:

  4. wrldwzrd89
    Expand Collapse
    macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    Wow. I did not know you could do that in AppleScript with "words of". Learn something new every day. ;)
     
  5. h1068
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Jun 10, 2012
    #5
    Thank you, you just saved me from posting 500 users into my applescript!
     
  6. kryten2
    Expand Collapse
    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    @wrldwzrd89

    Your handler is more robust if the usernames contain spaces. But that can be solved by putting every username on a new line in the txtfile and using this :

    Code:
    set theUserList to paragraphs of (read theFile)
     

Share This Page