Read from file - Trouble, more Troubles...

Hi Folks,


on awake from nib theObject
set theFile to alias ((path to desktop as Unicode text) & "received.txt")
set num_list to paragraphs of (read theFile)
set myList to {}
set {tid, text item delimiters} to {text item delimiters, ";"}
repeat with num in num_list
set end of myList to text items 1 thru 3 of num
end repeat
set text item delimiters to tid
set content of theObject to myList
end awake from nib

I read from the file “received.txt” data which is separeted by “;” - that means everything which is between “;” should be put into one row -
if the file looks like

abcdef;abcdef;abcdef; ssff;asdfsdfj;adsdfk abcdef;abcdef;abcdef; sdfsd;sdafksdf;dfasdj abcdef;abcdef;abcdef; asdsdf;dafsdf;dafsdfa
everything works fine!

But if the file looks like this:

[code]In;+731552;
57656C636F6D6520746F206D7946617665732E20476F20746F20746865206D61696E2073637265656E20746F207365742075702E205469703A205570206172726F7720666F722063616C6C206C6F672E

OK
;
In;+731552;
Your myFaves contacts have been refreshed.

OK
;
In;+731552;
You’re only allowed to change each contact once a month. Please try again after the 1st of next month. See www.myfaves.co.uk for full T&Cs

OK
;
In;+731552;
Your myFaves contacts have been refreshed.

OK
;
In;+731552;
Your myFaves contacts have been refreshed.

OK
;[/code]
then it stops with an error message:

May you can advise me how to solve this…

Thanks for your help!

Stefan

Exactly what problem are we trying to solve, and what is the expected result?

Of course it doesn’t work, because the script expects at least three semicolon separated items in one line without line breaks.
You need an algorithm to check if it’s an one-liner or contains more lines.

This works with both types.
If the received text has still more types, you must add more conditions :wink:

on awake from nib theObject
	set theFile to alias ((path to desktop as Unicode text) & "received.txt")
	set num_list to paragraphs of (read theFile)
	set myList to {}
	set {tid, text item delimiters} to {text item delimiters, ";"}
	set i to 1
	repeat until i is not less than (count num_list)
		if text item 1 of item i of num_list is "In" then
			set tempList to {}
			repeat until item i of num_list is ";"
				set end of tempList to item i of num_list
				set i to i + 1
			end repeat
			set text item delimiters to ""
			set tempList to tempList as Unicode text
			set text item delimiters to ";"
			set end of myList to text items 1 thru 3 of tempList
		else
			set end of myList to text items 1 thru 3 of item i of num_list
		end if
	set i to i + 1
	end repeat
	set text item delimiters to tid
	set content of theObject to myList
end awake from nib

Hi Stefan,

wow - works perfect!

Thanks a lot!

Stefan