PDA

View Full Version : [Resolved] Applescript: List/txt.file question




h1068
Jul 3, 2012, 04:15 PM
Can I set the_user_list to read a text file of users separated by commas?



set the_user to do shell script "logname"
set the_user_list to
if the_user is in the_user_list then



wrldwzrd89
Jul 3, 2012, 05:48 PM
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:


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:
set the_user_list to my text_to_list(text_var, ",")

kryten2
Jul 3, 2012, 05:49 PM
Try this :

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

wrldwzrd89
Jul 3, 2012, 05:54 PM
Try this :

set theFile to POSIX file "/Users/test/testtxt.txt"
set theUserList to words of (read theFile)
Wow. I did not know you could do that in AppleScript with "words of". Learn something new every day. ;)

h1068
Jul 3, 2012, 06:24 PM
Thank you, you just saved me from posting 500 users into my applescript!

kryten2
Jul 3, 2012, 06:30 PM
@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 :

set theUserList to paragraphs of (read theFile)