Re: read a file in same directory
Re: read a file in same directory
- Subject: Re: read a file in same directory
- From: "Paul F. Henegan" <email@hidden>
- Date: Mon, 04 Mar 2002 00:48:56 -0500
Hello,
on 29.01.2002 01:22, email@hidden at email@hidden wrote:
>
I'm having problems getting my AppleScript application to read a text file
>
that is in the same directory. For whatever reason, I get a "Finder got an
>
error. Duplicate file name." error when I run this. I'm not sure I
>
understand what it means by "duplicate file name".
On repeated testing, the file remains open; you need to close access while
debugging.
>
>
>
tell application "Finder"
>
activate
This changes the focus, as it were, from this script to "Finder", so any
reference to me refers to the process "Finder". I've included a handler to
deal with specifying the containing folder of the script file.
>
set file_in_string to "finisherparms.txt"
This variable doesn't refer to the file, but rather the file's name.
>
>
try
Here's the "Duplicate File Name" error.
>
set originalfile to open for access file file_in_string
Because of putting this in a "Finder" tell block, an empty file in the
System Folder is now being referenced. Beware that in AS 1.6 if the file
specified doesn't exist, an empty one will be created.
>
on error error_message number errNum
>
display dialog "2: " & errNum & " " & error_message
>
end try
>
>
try
This errors several ways, but most significantly because the file the script
is reading is not the one in the same directory as the script file itself.
>
read originalfile
>
copy the result to readstring
>
>
display dialog "readstring = " & readstring
>
>
on error error_message number errNum
>
display dialog "3: " & errNum & " " & error_message
>
close access originalfile
>
end try
>
end tell
If I'm not mistaken, the following should be very close to what you seek:
======================================================================
set file_name_in_string to "finisherparms.txt"
set my_path to (path to me) as text
set my_container_path to path2container(my_path)
set file_path_in_string to ((my_container_path as text) &
[NO-BREAK]file_name_in_string)
--display dialog file_path_in_string--for debug;
try
set originalfile to open for access file file_path_in_string
[NO-BREAK]--with write permission--for debug ; see next line
--write "test" to originalfile--for debug; give the app something
[NO-BREAK]to read ;-)
on error error_message number errNum
display dialog "2: " & errNum & " " & error_message
end try
try
read originalfile from 1 --change or omit this parameter as needed
[NO-BREAK]to avoid eof error
(*
test for empty file goes here
*)
copy the result to readstring
close access file file_path_in_string --for debug; else previous
[NO-BREAK]try block errors because file is already open
display dialog "readstring = " & readstring
on error error_message number errNum
display dialog "3: " & errNum & " " & error_message
end try
on path2container(thisPath)
(*
this takes the path to an item and returns a path to its containing folder
*)
(*
test for valid Mac OS file path goes here
*)
-- test for file or folder
set lastItem to 0
--path to a folder ends with ":"; since this shall be used as a
[NO-BREAK]TID later, it will add a text item to the end of tempPath
if text item -1 of (thisPath as text) is ":" then
set lastItem to -3
else
set lastItem to -2
end if
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set tempPath to text items 1 thru lastItem of thisPath
set contPath to (tempPath as text) & ":"
set AppleScript's text item delimiters to oldTID
return contPath
end path2container
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
HTH,
paul
--
Paul F. Henegan
<email@hidden>
_______________________________________________
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.