Re: Write to file??
Re: Write to file??
- Subject: Re: Write to file??
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 30 Jul 2002 13:35:29 -0700
On 7/30/02 12:32 PM, "Steve Suranie" <email@hidden> wrote:
>
Hi all:
>
>
I have a script which at the end writes data to a log file. When I run the
>
script on my G4 it works fine. When I move it to a G3 which I am using as a
>
workstation I get an execution error. "Finder got an error. Can't make
>
....[path and filename]... into a file specification. Any clues as to why?
>
Here's my code: [I'm hard coding some info in here for testing purposes]
>
>
tell application "Finder"
WHY is all this in the Finder? Get rid of the Finder.
>
>
--==========================================
>
--WRITING LOG AND SENDING OUT EMAIL MESSAGE
>
--==========================================
>
set thisDate to current date
>
set logPath to the folder "_logs" of the folder "Merion Image Archive" of the
>
disk "Cumulus" as string
(forget the Finder. do it like this:)
set logPath to "Cumulus: Merion Image Archive:_logs"
>
<snip immensely long if/else that can be reduced to a few lines:>
set thisMonth to the month of thisDate
set monthList to {January, February, March, April, May, June, July, August,
September, October, November, December}
repeat with i from 1 to 12
if thisMonth = item i of monthList then
set thisMonth to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
>
>
set theFile to logPath & logFile as string
>
>
--the error occurs here, at either part of the if statement...
>
if not (the file theFile exists) then
>
>
set writeFile to (open for access theFile with write permission)
'open for access' requires its own 'file' word:
open for access file theFile with write permission
But this will conflict with the Finder's 'file' word if you're in a Finder
tell block. It's all made muddier if you happen to have certain osaxen
(Jon's., Akua) in OS 8/9 which will coerce string file paths without it.
Don't do it in the Finder, Instead do this:
try -- only succeeds if the file exists - note 'alias'
set writeFile to (open for access alias theFile with write permission)
write return & recordID to writeFile starting at eof
on error -- make a new file
set writeFile to (open for access file theFile with write permission)
write recordID to writeFile
end try
close access writeFile
The other way to do it is like this (works in all cases):
set writeFile to (open for access file theFile with write permission)
write recordID & return to writeFile starting at eof
close access writeFile
>
>
write recordID to writeFile
>
>
else
>
>
set writeFile to (open for access theFile with write permission)
>
>
write return & recordID to writeFile starting at eof
>
>
end if
>
>
close access writeFile
>
>
end tell
Get rid of that Finder!
--
Paul Berkowitz
_______________________________________________
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.