Read text file and manipulate list

Hi guys,

Could I please have some help.

I have a text file that looks like this:
TestPrinterName1,sftp://secure.gi-soltuionsgroup.com,secure,testUserName1,TestPassword1,ZipTestPassword1
TestPrinterName2,sftp://secure.gi-soltuionsgroup.com,secure,testUserName2,TestPassword2,ZipTestPassword2
TestPrinterName3,sftp://secure.gi-soltuionsgroup.com,secure,testUserName3,TestPassword3,ZipTestPassword3

I have managed to read the text file which is a start. However I want the get a list of item 1 from each line to let the operator choose which printer we are sending a file to. Plus an additional entry for if the printer does not exist so it can be added.

Once that has been selected apply the other 5 variables in that line to different variable names.

Could you please help me.

Cheers
Andy

Hello.

Here it is.

set theText to paragraphs of "TestPrinterName1,sftp://secure.gi-soltuionsgroup.com,secure,testUserName1,TestPassword1,ZipTestPassword1" & linefeed & "TestPrinterName2,ftp://secure.gi-soltuionsgroup.com,secure,testUserName2,TestPassword2,ZipTestPassword2" & linefeed & "TestPrinterName3,sftp://secure.gi-soltuionsgroup.com,secure,testUserName3,TestPassword3,ZipTestPassword3" & linefeed
set L to {}
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {",sftp:", ",ftp:"}}
repeat with aPar in theText
	try
		tell text item 1 of aPar
			if it is not linefeed and it is not return then
				set end of L to it
			end if
		end tell
	end try
end repeat
set AppleScript's text item delimiters to tids
set L to L's text

set thepr to choose from list L default items item 1 of L
if thepr is false then error number -128
# we'll have to find the rest of the line from our selection
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, thepr as text}
set thechoices to text items of (theText as text)
set AppleScript's text item delimiters to tids
# the select item starts with ""
set i to 0
repeat with achoice in thechoices
	set i to i + 1
	if contents of achoice is "" then exit repeat
end repeat
if i = (count thechoices) then error "Can't happen, item not found."
set theParams to text item (i + 1) of thechoices
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
# The extracted data for the selected printer
set lprLocalName to thepr as text
set lprDnsName to text item 2 of theParams
set lprSecurity to text item 3 of theParams
set lprUserName to text item 4 of theParams
set lprPwd to text item 5 of theParams

Thank you for the quick reply,

I have been searching the web on how to do this without much luck.

Reading though the code trying to work out whats happening am i right in thinking that you are getting the list item by setting the delimiter to ,sftp: what would happen if one was ftp (ie not a secure connection). I think all are sftp so not a problem but there is always a trouble maker. :slight_smile:

Cheers
Andy

Hello Andy.

The easiest way to solve that, would be to have two list of printers, one secure, the other on ftp, then you’d just boilerplate-copy this script, and change the text item delimiter to “,ftp” if everything else is the same.

Text item delimiters, is really a key component of Applescript, if not the heart and soul. You can read an intro about them in this fine article from the Unscripted section here at Macscripter’s

I hope it helps. :slight_smile:

Hi McUsrll,

Thanks for that, I keep trying to get my head around them.

My scripts are getting more complex as I use it and I am starting to move away from stuff that I can easily find on the net.

I will have a read of that link and try to get my head around it.

Thanks for your help.

Andy

Hello.

I changed the script for you so it should work with both ftp and sftp printers, without any ado.

The text delimiter can be a list of text delimiters, which should be just fine if your version of OS X is Snow Leopard or newer.
Be sure to keep the old script, should the number of parameters change for the two test configurations.

Hi,

Thanks again.

I have now added the script to what I have done so far. Its coming along nicely now to get it to play nicely with transmit (which I have done before) and our filemaker database (which I have managed to successfully do before) so hopefully all good.

I have never delved into a text file and split out the text before.

Thanks again for your help.

Hey Andy,

A slightly different take.

set _text to "TestPrinterName1,sftp://secure.gi-soltuionsgroup.com,secure,testUserName1,TestPassword1,ZipTestPassword1" & linefeed ¬
	& "TestPrinterName2,sftp://secure.gi-soltuionsgroup.com,secure,testUserName2,TestPassword2,ZipTestPassword2" & linefeed ¬
	& "TestPrinterName3,sftp://secure.gi-soltuionsgroup.com,secure,testUserName3,TestPassword3,ZipTestPassword3" & linefeed
set _cmd to "sed -En 's!^([^,]+),.*!\\1!p' <<< " & quoted form of _text
set pList to paragraphs of (do shell script _cmd) & "Add_Printer"
set pickIt to choose from list pList with title "¢¢ Printer_List ¢¢" with prompt ¬
	"Pick a Printer:" default items {last item of pList} with empty selection allowed
if pickIt ≠ false and pickIt ≠ {} then
	if pickIt = {"Add_Printer"} then
		return "Add a printer."
	else
		set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {pickIt}}
		set _record to text items of _text
		set _record to text item 2 of _text
		set AppleScript's text item delimiters to {linefeed}
		set _record to text item 1 of _record
		set AppleScript's text item delimiters to {","}
		set {var1, var2, var3, var4, var5} to text items 2 thru -1 of _record
	end if
else
	false
end if
set AppleScript's text item delimiters to oldTIDS
{var1, var2, var3, var4, var5}

Hey Andy,

Personally I’d use the Satimage.osax to read the file and parse the text:

# Read the file directly with the Satimage.osax:
set _file to alias ((path to home folder as text) & "test_directory:andy_test:a_text_file.txt")
set printerList to find text "^\\S.+" in _file with regexp, all occurrences and string result
set nameList to (find text "^.+?(?=,)" in printerList with regexp and string result) & "Add_Printer."
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 _record to find text "^" & pickIt & " *, *(.+)" in (join printerList using linefeed) with regexp and string result
		set {var1, var2, var3, var4, var5} to text items 2 thru -1 of (splittext _record using " *, *" with regexp)
	end if
else
	false
end if
{var1, var2, var3, var4, var5}

Hello.

Chris’s code is far more readable, and all that, but I just wonder about one thing:
An Osax like Satimage.osax, -every osax is made with the help of Carbon, and the Carbon Framework was deprecated in Mountain Lion, will then Osaxes work with Maverick?

I don’t like to bring it up, but it seems timely to do so with the announcement of the next version of OS X:
is the era of osax’s over?

We won’t know until Mavericks comes out (unless it leaks beforehand). :slight_smile:

If it does happen then I’ll have to port a lot of stuff to Shane’s ASObjC_Runner and get on with life, and the above script would be complicated by 2 or 3 more lines.

I have been thinking of this for a while, and I think that maybe Apple has some sort of mechanism for making Osaxen’s still work.

I tend to not use Osaxen’s but rely on the delivered tools, (like sed,awk and the rest of the shell utilities.) the thing is that using an external utility is undeniably slower than using an Osax, but on the other hand, machines are incredibly much faster now, than say 5 years ago, so the overhead of do shell script, or a call to ASObjC runner isn’t that obvious.

Yes. You keep belaboring this point. By that argument you should do FTP from the command-line and not use a 3rd party client - etcetera, etcetera.

I too use the shell when I feel it’s the best tool for the job, but I’ve used the osaxen from Satimage for over a decade to do practical work - tasks they excel at. I like them and will use them until they break - unless something more useful comes along.

That said - I’m very pleased with Shane’s ASObjC_Runner, even if I use it mostly for testing at this time.

Alright. Here’s a version of the script that uses ASObjC_Runner.

I was right. Two lines more for the tell-block.

set _file to alias ((path to home folder as text) & "test_directory:andy_test:a_text_file.txt")
# Use ASObjC_Runner to read the file and parse the text:
tell application "ASObjC Runner"
	set printerList to link strings (look for "\\S.+" in _file) by inserting linefeed
	set nameList to (look for "^.+?(?= *,)" in 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 _record to (look for "^" & pickIt & " *, *(.+)" in (link strings printerList by inserting linefeed)) as text
			set {var1, var2, var3, var4, var5} to text items 2 thru -1 of (look for "[^,]+([ ,]|$)" in _record)
		end if
		{var1, var2, var3, var4, var5}
	else
		false
	end if
end tell

Hello. :slight_smile:

That was a very interesting comparision. For the record, I used to use Satimage osax for the cases where it was feasible to use it, but I ceased to use it deliberately, when I realized that it was to break. I have even written scripts for detecting it, and download it if it weren’t installed.

The Carbon Framework was not deprecated in Mountain Lion. The Carbon Core Framework, which is a subframework of CoreServices Framework, was deprecated. A different thing entirely.

If the APIs scripting additions require are removed without a suitable replacement, we’re going to have a hell of a lot more than scripting additions to worry about. I suspect security zealots are a bigger threat.

Thanks. I’d be inclined to move the choose from list outside the app tell block, because if you cover it up with another app, not having a Dock icon can make it hard to retrieve.

You may want to change “soltuionsgroup” to “solutionsgroup”.

Thanks Nigel,

Lucky that data was to test the applescript getting the information and for placing on the board as an example. Once I have this thing written I will delete it anyway.

Cheers
Andy

Hey Shane,

Good point, but ‘tell me’ will use the script-runner as the agent for the choose from. So that shouldn’t be a major problem.