Read text file and manipulate list

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."
		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.

Ah – I just read over that. Sorry for the noise.

Hi all,

Thanks for the help and it’s interesting reading all the different was to achieve the same end result.

My scripting ability is very limited and most of what I know I have gleamed from this board so thank you for all your help.

I am not to worried about the csv data as it is being entered via the applescript and this will be copied and pasted from another source or 2.

I have gone with the original script provided thanks. ccstone when I try and compile your script it asks me to choose application. I presume that that is for ASObjc Runner and I do not know which one to choose. I am currently running OSX 10.6.


Hello Andy.

I am on Snow Leopard as well, and the version of AsObjC Runner that works for me is 1.9.12.

I think that if you just download, or has it installed, it will automagically upgrade to the latest version, when you use it, and are online.