limitation in linecount? strange error scripting AppleWorks

Hi all,

I have an AppleWorks 6 text file with 1258 lines which I try to process. Here is the crucial part of my script which is going wrong somewhere:


set inFileName to “DSLKosten”
set inFile to alias ((path to desktop as string) & inFileName)

tell application “AppleWorks 6”
set totLines to number of paragraphs in text body of document inFileName
set i to totLines
end tell

Now for no obvious reason the filecount stops at 135 paragraphs, although the file contains roughly a thousand more. I saved and stored both the file and the script and ran it again several times. I checked with the invisible characters in the AppleWorks file and see no difference to the lines before or after. Is there a limitation as to linecount???
(Can’t believe that - but where am I going wrong?) Thanks a lot in advance,

Dirk

Dirk,

You can define the path to your file in one line.

set inFileName to "Mac HD:Desktop Folder:DSLKosten"

If you are after the count of lines in your file, and it is a text file, perhaps the Finder could get it for you. I do not have AppleWorks here so I am unable to test but for your standard text file the following should return the count of lines in your text file.

set intLineCount to read inFileName as text using delimiter return

*Maybe your text file is delimited with a character other than return. Just substitute whatever character, (in quotes), for the return. And of course, change the drive name to whatever yours is.

I will test this when I am at work tomorrow and closer to an AW text file.
I’ve never scripted AppleWorks but will check to see if there is a rhyme or reason to paragraph acquisition.

I can see no apparent need for this line. You already assigned the count of lines to a variable.

Warmest regards,

I mis-posted. The sytem is actually reading the file, not the Finder.

I checked and appleworks text files read pretty much the same as other text files so my post above should work for your purposes.

I got your code to work on my machine and was able to detect 10,000 lines of text.
I did not test the limit on line count with AppleWorks.

How is your text file structured?

Hi Mytzlscript,

and thank you for your suggestions! However, I still can’t get it to work on my side:
When I run your code


tell application “Finder”
set inFileName to “Mac HD:Desktop Folder:DSLKosten”
set intLineCount to read inFile as text using delimiter return
end tell


I first get an error message, saying that the ‘text is too long to be displayed,
only the first 20000 characters will be displayed’. The display then looks like

{"¨«(p«(H?ÄÔH
(«(ÄPØÏ(dÅ
d+Eë(˫(?
?į0«(??ĉѫ(¿PØÏ?á·L·…}

When I run the script against a BBEdit file I get an “End of file error”.
I can’t find any useful helpfiles for file handling via AppleScript, so I would
be really greatful if we could work it out via this forum. What I am trying to do
is to process my online bill. I download the bill and save it as a text file.
One line displays the download amount, the next line the corresponding online time.
For now I only need the online time, that is every other line. I wrote the following
code to process the file but since the loop stops at line 135 I don’t get the
right result. As far as I can see, all lines (including line 135, the line before and
the line after) have the same return delimiter. Here is the whole program:

tell application “Finder” --creating the output file
if exists (file “MacOS9:desktop folder:DSLKosten2”) then
deleteFile “MacOS9:desktop folder:DSLKosten2”
delay 2 --seconds
end if
end tell
tell application “Finder” to make file at desktop with properties {name:¬
“DSLKosten2”, creator type:“R*ch”, file type:“TEXT”}

set inFileName to “DSLKosten”
set inFile to alias ((path to desktop as string) & inFileName)
set outFile to alias ((path to desktop as string) & “DSLKosten2”)
set sumTime to 0

tell application “AppleWorks 6”
set totLines to number of paragraphs in text body of document inFileName
set fref to (open for access outFile with write permission)

repeat with i from 2 to totLines by 2
	set myRec to paragraph i in text body of document inFileName as string
	set myDay to (characters 1 thru 10 of myRec as string)
	set onlineTime to (word 1 of (characters 20 thru 34 of myRec as string) as integer)
	set sumTime to (sumTime + onlineTime)
	set outRec to myDay & "	" & onlineTime & "

"
write outRec to outFile
end repeat
close access fref
end tell

set myHours to sumTime div 60
set myMinutes to sumTime mod 60

display dialog "Time online: " & sumTime & “Minutes " & return & myHours & ¬
" hours and " & myMinutes & " minutes”

Thanks in advance for any help!

Dirk,

If this is the result of reading the file then I don’t think it is a straight text file.
How are you downloading this file? Via browser? What is actually writing the file inFile? And what is the code you are using for doing it?

Mytzlscript,

the mysterious signs are the reading result when I use your code. When I use my original code, the reading is ok, only that it stops at line 135.

Since I use a Mac (of course), I cannot use the option of saving the browser window’s contents directly as an Excel file. Instead I copy the columns with the mouse and use cut and paste into a textfile - here I tried both AppleWorks and BBEdit Lite.

With the invisibles made visible, the text file shows no abnormal signs thus looks like a normal textfile.

As to your second question: The inFile is thus only read, not written. Only the outfile is written from within the script. And as I said: The script worked fine the other day, when the inFile was somewhat smaller in content. I don’t know why it wouldn’t work now. (And as you said: You can read in a very large file on your machine - so there should be no limitation as to # of lines. )

Dirk,

You can, in fact get the source code for the page you are getting your data from and then post process with Applescript.

tell application "Internet Explorer"
	--get the source code for the current page 
	set pageSource to GetSource --should result in the source of the page you are viewing
--you can also refer to the window identifier to get the source

	--or download the page to your hard drive
	GetURL "[url=http://www.someurl.com]www.someurl.com[/url]" to "Mac HD:Desktop Folder:Some File"

	
	--or using Sandi's Addition's TypeText command copy the current window to the clipboard
	tell application "Internet Explorer"
		Activate--bring IE, or Netscape to the front
		update
		TypeText "a" with Command--invoke Select All command
		TypeText "c" with Command--invoke Copy command
	end tell
end tell

--If you use Netscape Communicator the code is the same for downloading the file
tell application "Netscape Communicator™"
	GetURL "[url=http://www.someurl.com]www.someurl.com[/url]" to "Mac HD:Desktop Folder:Some File"
end tell

If you are logging into something to get this data chances are downloading the URL will not work. But once you have it open some of this code should do the work of getting the contents of the page.

You answered my question. You are copying and pasting into this file.

**The only thing I can think to do to get the read to work would be to start over with the text file. Open Appleworks, create a new, empty document, and save as using the file type “TEXT”, not “RTF”. Then copy and paste your data and see if this format change helps.

--tell application "Finder" 
set inFileName to "Mac HD:Desktop Folder:DSLKosten" 
set intLineCount to read inFile as text using delimiter return 
--end tell 

FYI - You do not have to nestle the read code in a Finder tell block. It actually works better if you do not. The Finder runs out of memory…

BTW- I have Appleworks 6 running under OS9.2 on an iMac 500Mhz with 320MB ram here.

Good luck!

Hello Mytzlscript,

your last suggestion to get the source code of the page does not seem be too advisable because I have to log into a secure area for getting my bill. But desperately searching and browsing this forum for other possibilities I came across the following, which also works for me:


set inFile to (read file “MacOS9:Desktop Folder:DSLBBEdit”)
set AppleScript’s text item delimiters to return
set theList to every text item in inFile
set i to length of theList --this will indirectly get the linecount
set indx to 0

repeat with indx from 1 to i
–process the inFile linewise via item indx in theList
end repeat

(* The alternative suggestion in the same thread


set foo to choose file with prompt “Choose a plain text file.”
set bar to (read foo using delimiter return)
set moreFoo to item 1 of bar – line 1
display dialog moreFoo

results in a “parameter error” in the read command. (Although I do have several scripting additions like osax, Jon’s Commands etc. !?)
*)

So the whole file is first read into a variable (in this case: a list) and then this list is processed item for item. I would have thought it more elegant to process the file linewise, and thought I had a solution with my AppleWorks script (which worked once but then shows this obscure bug in that it stops at line 135) but as long as the above solution works I stick to that for a while.

Once again, thanks a whole lot to you and to all the helpful souls in this forum answering newbie questions with patience and competence.

Best greetings,
Dirk

I tried your suggestion once more:

set foo to choose file with prompt “Choose a plain text file.”
set intLineCount to read foo as text using delimiter return

but still do NOT get a line count: The result of intLineCount is NOT an integer giving the number of lines but instead the whole contents of the file as a text string??

I agree, an applescript to go log into the page probably wouldn’t work too well but once you are on the page you can automate the extraction of the data you are looking at. Rather than copy and paste manually you could get the source and have it saved to a file via applescript. Then have your applescript work with that file.

Did you try my suggestion with starting over with the format of the text file?

Or, use Simpletext to get your starter text file, or have AS create it for you:

tell application "Finder" to make file at desktop with properties {name:¬ 
"DSLKosten2", creator type:"ttxt", file type:"TEXT"} 

If this still does not work then maybe there is something else we are missing.

Let’s try the following test. The script below will make a TEXT file with the creator type “ttxt”. Then it will append 1500 lines of copy to it. And after closing access to the file it reads it as text with return as a line delimiter. I tested it and it flawlessly creates a text file on my desktop, appends to the file 1500 times and the last line returns 1501 items in a list. Hopefully this will work on your end but either way we’ll know which way we have to go.

tell application "Finder"
	set newFile to make file at desktop with properties {name:"DSLKosten2", creator type:"ttxt", file type:"TEXT"}
	set myFile to open for access file newFile with write permission --open the file with write permission
end tell

set lineToWrite to return & "Some text to append just for fun" as string --set up some dummy text

repeat 1500 times
	tell application "Finder" to set eofFile to (get eof myFile) + 1 --get the end of the file
	write lineToWrite to myFile starting at eofFile --write our test line of copy to the file starting at 1 past the end of the file
end repeat

--now check to see if the read returns 1501 lines (1 extra for the initial append)
close access myFile --close access so we can work with the file
set readMe to read newFile as text using delimiter return --read the file as text using return as a line delimiter

Good luck Dirk,

Had a look a the source of the page: I can’t see anything but the source of the frames:
The information I am after seems to come in somewhere else.

Yes: Just now I ran your script (which took some time :slight_smile: and it created a file with 1501 lines. (This I found out by opening it with BBEdit, since SimpleText couldn’t open it.)

However: The last command in your script got an “EOF error”.

I then commented out the first part (making and filling of the file), adjusted the file name and ran just the
set readMe to read “MacOS9:desktop folder:DSLKosten2” as text using delimiter return

This got me the following:
→ An error of type -20013 has occurred.
(and a ‘file too large to be displayed in log window’ message).
And from the event window I see that the command did not return an integer of 1501, as expected, but the first 20000 characters of the file:
{"
Some text to append just for fun
Some text to append just for fun
Some text to append just for fun
Some text to append just for fun
Some text to append just for fun
Some text to append just for fun
Some text to append just for fun
etc. …
"}

So I just cannot get a linecount from a file. ???

Anyhow: After copying and pasting the needed information from the web page into a BBEdit textfile, I managed to write a script which separates the online-time information from the download-volume information and writes out an outfile for each. (Using

set AppleScript’s text item delimiters to return
set theList to every text item in inFile
set i to length of theList

and using i to process each item in the list, i.e. each line in the file.)

I then succeeded (after some struggling) to write another script with a group control change (“Gruppenwechsel” ?) which takes the time-file as input and sums up all
online-minutes for each day respectively. I guess I leave it at that - it is not the most elegant solution, but after all, it’s what I wanted.

(I do find file processing easier in other languages - REXX or PERL or even C or Java, though.)

As a little aside to those who might be following our discussion here: I found a (beginner’s) manual dealing with scripting AppleWorks:
[color=blue]http://www.tandb.com.au/applescript/tutorial/[/color].
But that does not contain a whole lot about textfile-processing (at least as yet).

Thanks again, and best greetings,

Dirk

How would I go about getting a line number printed for each line of a word processor document per page. (This is similar to the idea of what a court reporter’s transcript looks like). In other words I want each line of each page numbered beginning with #1 for each new page. It seems to me somebody somewhere must have scripted this by now. I am running, OSX 10.2.2 with AW6.2.7
Thanks