Hey Andy,
Here’s another version that employs read with delimiters and uses only stock OSX tools.
set AppleScript's text item delimiters to {""}
set _file to alias ((path to home folder as text) & "test_directory:andy_test:a_text_file.txt")
# Using 'read' with a delimiter to populate the initial list:
set printerList to read _file using delimiter linefeed
# Trim any extraneious linefeeds:
repeat while last item of printerList = ""
set printerList to items 1 thru -2 of printerList
end repeat
set AppleScript's text item delimiters to linefeed
set printerList to quoted form of (printerList as text)
set nameList to paragraphs of (do shell script "awk -F' *, *' '{ print $1 }' <<< " & printerList) & "Add_Printer."
tell me to set pickIt to choose from list nameList with title "¢¢ Printer_List ¢¢" with prompt ¬
"Pick a Printer:" default items {last item of nameList} with empty selection allowed
if pickIt ≠false and pickIt ≠{} then
if pickIt = {"Add_Printer."} then
return "Add a printer."
else
set _cmd to "awk -F' *, *' 'BEGIN{ OFS=\"\\n\" } /" & pickIt & "/ { print $2,$3,$4,$5,$6 }' <<< " & printerList
set {var1, var2, var3, var4, var5} to paragraphs of (do shell script _cmd)
end if
end if
One of the problems with comma-delimited data is that sometimes spaces are accidentally (or intentionally) introduced during the data-entry.
Being able to use a regular expression to allow for that possibility is handy.