Re: open a text file and evaluating in Applescript
Re: open a text file and evaluating in Applescript
- Subject: Re: open a text file and evaluating in Applescript
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 29 Jan 2002 20:29:46 -0800
On 1/29/02 7:58 PM, "email@hidden" <email@hidden> wrote:
>
I've been surfing through the web for code snippets on how to read a text file
>
in Applescript and display the results. I'm sure this is a simple process,
>
but I'm missing something.
>
>
Let's say I have a text file called "text.txt" in a folder called
>
"Experiments" on my Mac OS X drive. I create an applescript and save it as a
>
standalone applicaton, in the exact-same folder, which looks like this:
>
>
>
>
tell application "Finder"
You don't need the Finder. This is nothing to do with the Finder (I suppose
you were trying to record this?)
>
activate
Without the Finder, there's nothing to activate. Don't need activate. (Even
_with_ the Finder, you don't need 'activate'.
>
set file_in_string to "text.txt"
Oops. You need to specify a full file path here - not the name of the file.
Let's suppose it's on your desktop. Whether you're in OS 9 or OS X, you can
get the part of of the file path that leads to your own desktop as
path to desktop as string
Alternately, if you're elsewhere, you can hard-code the pathname to the
folder the file is in:
"Mac HD:A Folder:Another Folder:"
Note the colon at the end of the folder name.
So that line becomes, depending:
set file_in_string to (path to desktop as string) & "text.txt"
or
set file_in_string to "Mac HD:A Folder:Another Folder:text.txt"
From her on in, your script should work OK, except
>
>
try
>
set originalfile to open for access file file_in_string
>
on error error_message number errNum
>
display dialog "2: " & errNum & " " & error_message
>
end try
>
>
try
>
read originalfile
>
copy the result to readstring
>
>
display dialog "readstring = " & readstring
>
>
on error error_message number errNum
>
display dialog "3: " & errNum & " " & errr_message
error_message
>
close access orginalfile
>
end try
>
>
end tell
no end tell without a Finder block:
set filePath to (path to desktop as string) & "text.txt"
set fileRefNum to open for access file filePath
set textContent to read fileRefNum
close access fileRefNum
textContent
After you've done it a few hundred times:
set fp to (path to desktop as string) & "text.txt"
set f to open for access file fp
set r to read f
close access f
r
--
Paul Berkowitz