 |
 |
Can't Get "open for access file" to do anything
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status:
Offline
|
|
Hey,
I'm writting a small script that will just take the conents of a file, and make it a variable i can return later.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">set msg_cache to contents of (open for access file "users:xxx  ublic:message_cache.txt&q uot 
close access msg_cache </pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">That's really all ive got, wheni return the value, i get a number in the 40s, if i ptu it in a tell application finder, i get a numberin the 1040s, and sometimes if i run it more than once, i get 'duplicate file' error.
Anything in particular i am doing wrong aor is there somethign totallydifferent i shoudl try?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
Not sure where you got that code but try...
read "your hd:users:xxx  ublic:message_cache.txt"
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status:
Offline
|
|
should i be doing a 'set sumVariable to read "a:b:c:d"
or do i just do this read and then somehow call file later?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status:
Offline
|
|
ok, i think ive got teh read thing going rigght, but now im getting an 'end of file' error,waht do i do about that?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by cpk0:
<strong>ok, i think ive got teh read thing going rigght, but now im getting an 'end of file' error,waht do i do about that?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well I'd need to see the script..
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2002
Location: san francisco
Status:
Offline
|
|
the AppleScript command 'open for access' returns a file reference, which is an integer. so in your code you're trying to assign the 'contents of' a file reference integer to your 'msg_cache' variable. syntactically there is nothing wrong with this, so you're getting an unexpected value.
what you need to do is assign the result of 'open for access' to a file ref variable, then read the contents into another variable by accessing the file through your fileRef variable, as in:
set fileRef to open for acccess file 'my path:myfile..."
set fileContents to read fileRef
close access fileRef
you can also do this: set fileContents to read (open for access file "mypath:myfile...")
but in that case you'll then have to close the file by repeating the explicit file path, as in: close access "mypath:myfile...". this is not a good way to manage file access through file refs, as you've created one but don't have that file ref anywhere in a variable which you can later use.
the shortest and fastest way is: set fileContents to read file "mypath:myfile...", where the read command implicitly handles file open and file close for you.
this last option is simpler if you only need to perform a single read operation on this file at only one time in the course of your script. if you need to have a file open for multiple reads (and/or writes), then creating a file reference to which you can easily refer at any time in the course of your script is clearly advantageous.
note that it's also better practice to throw "try....on error" handlers around all attempts to open and close files through file reference variables.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2002
Location: san francisco
Status:
Offline
|
|
sometimes if i run it more than once, i get 'duplicate file' error.
that's because you've successfully opened the file, but have never closed it. you'll need to explicitly close the file first with 'close access "mypath:myfile...", then open it.
thus the importance of wrapping file open statements in "try...on error" handlers, as in:
set f to file "mypath:myfile..."
try
set fileRef to open for access f
.....
..... code that does stuff with the file
.....
close access fileRef
on error
-- an error in the code interrupted the script
-- so your close access statement above never executes
close access fileRef
end try
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status:
Offline
|
|
Thanks all, the script works flawlessly now
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|