File Handling and Forming Lists

Ok, it returns a reference to the file to use later in your script, when you want to write to the file, and is needed to close the file access, but, if you take a look, it creates the file too, with the name you provided into your variable.

set fileRef to (open for access file theLogFileName with write permission)
set eof fileRef to 0

now make what you want with your new file. You can set the type and creator or whathever, but don’t forget to close the access to the file, or you can do nothing more with it until the file access is not closed

Any ideas on how to hard code a list in the following format without using the command new file: {file “HD:temp:test.gif”, file “HD:temp:result.gif”}
cya
Roberto

The reason I’m trying to form a list in the format shown above is because the following code saves data with the above reference format, but it does not work if the reference is a number. And opening a file for access returns a number as a reference. Any ideas?


tell application "xxx"
try
save gust in (item 1 of fileRef)
save wind in (item 2 of fileRef)
save barometer in (item 3 of fileRef)
end try
end tell

Looks like you have a few misconceptions about how Applescript handles files. It doesn’t quite work like that. When you open a file, you get a file reference number. You use this number to read from the file or write to it. Take a look at the dictionary for the file read/write commands. They’re in the standard additions addition in most modern Mac OS versions. You use the file commands kinda like this:


--writes myData to myFile
set fileRef to open for access myFile with write permission
write myData to fileRef
close access fileRef --Always close access after using open for access
--reads contents of myFile into myData
set fileRef to open for access myFile with write permission
set myData to read fileRef to (get eof)
close access fileRef

Hope this helps you.

You’re probably correct, but being a newbie, I’m trying to understand the format of different types of file references.
The new file command returns a “file reference:”
{file "HD:temp:gust.gif}
Where the following code returns a number as the file reference e.g. {536} …


set fileRef to open for access myFile with writepermission
write myData to fileRef
close access fileRef

I would like to format the file reference in the way new file does, without having to use the command new file.

Ok, it seems to me that you already have the information you require, it’s just understanding it…
new file returns a complete path to the file. ie. {file "HD:temp:gust.gif}
open for access takes the path & opens the file, returning the index of the open file.
what you need is to add the paths you want to the list
ie.

-- set end of theFileList to alias <some path>
set end of theFileList to file "HD:temp:gust.gif"
set end of theFileList to file "HD:temp:gust2.gif"
-- result: {file "HD:temp:gust.gif", file "HD:temp:gust2.gif"}

if you were to use “alias” instead of “file” then moving the physical files wont matter (the OS handles it for you).
Cameron :slight_smile: