Re: Working with text files in AppleScript
Re: Working with text files in AppleScript
- Subject: Re: Working with text files in AppleScript
- From: Andrew Oliver <email@hidden>
- Date: Fri, 12 Sep 2003 15:09:47 -0700
Your specific problem is in the way you're using the 'read' command.
Specifically, the read command requires only the reference to the file to
read (as you'd previously ascertained using the 'open for access' command,
so your line:
> read some_text from the open_target_file
Is being interpreted as 'some_text' as being the reference to the file
whereas you've written the script with some_text being the contents of the
file after being read.
Additionally, the 'from' parameter does not identify the file to read from,
rather the position in the file to read from. i.e. If you said "... from
100", read would start at character 100 in the file.
Instead, you should just call 'read' passing in the file reference. If you
read the dictionary entry for read, you'll find that the result of the read
command is the contents of the file. Therefore what you need to do is:
set some_text to read open_target_file
And you're all set.
Additionally, if all you want to do is read the entire file into a variable,
you can forego all the 'open for access/close access' text and just include
the 'read' command.
Ironically, since you wrapped your code in a try/end try block, you didn't
get to see where the problem was since your error handler masked it. If this
wasn't in a try block, the Script Editor would have highlighted the
offending line and given you an (albeit cryptic) explanation of the problem.
Andrew
:)
On 9/12/03 2:35 PM, "Taner Edis" <email@hidden> wrote:
>
Hi,
>
>
I'm new at AppleScript I thought it'd be a good idea to learn some of
>
its basics after I got myself an OS X Mac. There's a question I
>
haven't been able to resolve by looking at the web and the book I have
>
on hand; perhaps someone here can point me in the right direction.
>
>
I thought I'd write a script to convert the html files authomatically
>
generated by iPhoto, to make them conform to my standard web pages
>
which have some style sheet stuff, and a standard footer at the bottom.
>
So I figured it'd be easy to just read in a text file, do a quick
>
search-and-replace, and rewrite the result out.
>
>
It seems, however, that I can't get past the first hurdle, of
>
successfully reading the contents of a text file into a string. How do
>
you do this? Here's a test script that I wrote, which always ends up
>
in the error part, displaying 'It didn't work," never getting to "text
>
read!". I'm sure I'm doing something wrong which is elementary...
>
>
>
set my_text to "" as text
>
set my_file to (((path to desktop folder) as text) & "Desktop.html")
>
my read_file(my_file, my_text)
>
display dialog my_text
>
>
on read_file(some_file, some_text)
>
try
>
set some_file to some_file as text
>
display dialog some_file
>
set the open_target_file to ,
>
open for access file some_file
>
display dialog open_target_file as text
>
read some_text from the open_target_file
>
display dialog "text read!"
>
close access the open_target_file
>
return true
>
on error
>
display dialog "It didn't work"
>
try
>
close access file some_file
>
end try
>
return false
>
end try
>
end read_file
>
>
Thanks for any help you can offer.
>
>
Taner Edis
>
http://www2.truman.edu/~edis/
>
_______________________________________________
>
applescript-users mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/applescript-users
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.