Re: reading paragraphs from a file
Re: reading paragraphs from a file
- Subject: Re: reading paragraphs from a file
- From: "Nigel Garvey" <email@hidden>
- Date: Thu, 6 Dec 2007 22:34:06 +0000
Alex Morken wrote on Thu, 6 Dec 2007 08:49:35 -0800:
>Hello,
>
>When executing the script below I am getting the following error --
>"Cannot get every paragraph of 697". Each time I run the script the
>number increases by one. The txt file I am reading from contains 1
>line and will never grow beyond that. I am not sure why the number
>of paragraphs would be increasing (if that is what it is saying).
>
>set tempdir to "/private/tmp/p.txt"
>set fpath to POSIX file tempdir
>set f to open for access file fpath
>set r to paragraphs of f
>display dialog r
>set r to nothing
>close access f
697 and successive numbers are the reference numbers of the successive
accesses you're opening to the file. These accesses, by the way, aren't
being closed, because the script's erroring and stopping before the
'close access' line. You can close all the lost accesses by quitting
Script Editor (or whatever you're using to run the script).
fPath is defined as a file in the second line, so you shouldn't put
'file' before it in the following one.
It's not possible to get the paragraphs of a file without reading it. You
have to read text from the file and get the paragraphs of the result.
Once you do get the 'paragraphs', the result will be a list. You can't
'display dialog' a list.
Unless you've previously set your variable 'nothing' to something, you
can't set 'r' to it.
It's a good idea to put everything that happens while a file's open for
access in a try block, so that the script keeps going long enough to
close the access.
set tempdir to "/private/tmp/p.txt"
set fpath to POSIX file tempdir
set f to (open for access fpath)
try
set r to paragraphs of (read f) -- Assuming the file contains plain,
ASCII text.
display dialog (item 1 of r)
on error msg
display dialog msg buttons {"OK"} default button 1
end try
close access f
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden