Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Can't Get "open for access file" to do anything

Can't Get "open for access file" to do anything
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status: Offline
Reply With Quote
Jul 12, 2002, 03:13 PM
 
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 &quot;users:xxxublic: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
Reply With Quote
Jul 12, 2002, 09:55 PM
 
Not sure where you got that code but try...

read "your hd:users:xxxublic:message_cache.txt"
     
cpk0  (op)
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status: Offline
Reply With Quote
Jul 12, 2002, 10:20 PM
 
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?
     
cpk0  (op)
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status: Offline
Reply With Quote
Jul 12, 2002, 10:53 PM
 
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
Reply With Quote
Jul 13, 2002, 01:58 AM
 
</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
Reply With Quote
Jul 13, 2002, 10:03 AM
 
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
Reply With Quote
Jul 13, 2002, 10:21 AM
 
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
     
cpk0  (op)
Fresh-Faced Recruit
Join Date: Jul 2002
Location: North Attleboro, MA
Status: Offline
Reply With Quote
Jul 13, 2002, 12:12 PM
 
Thanks all, the script works flawlessly now
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:01 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2