Read text file and manipulate list

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.

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.

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.

Cheers
Andy

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.