Why doesn't "file" work as per Applescript Language Guide?

Why first line of the following fail?


set toFile to file "Macintosh HD:Users:sean:Library:Things To Do and Handle:D4EDA7DC-E2AB-45F3-9643-6D3030845696:data.txt"

open for access toFile with write permission
set eof of toFile to 0
write "sss" to toFile starting at eof as text
close access toFile

The file exists, but applescript returns error "Can’t get file “Macintosh HD:…”

Also, why does it work if I change “file” above to:

  1. “file specification?” or
  2. POSIX file “Users/sean/Library/…”?

AppleScript: 2.0.1
Browser: Safari 525.20
Operating System: Mac OS X (10.5)

You don’t need the word “file” in your set toFile statement. You want to set toFile to the path to the file, not make it class file.

Hi,

using your syntax, you must create a reference to the file


set toFile to a reference to file "Macintosh HD:Users:sean:Library:Things To Do and Handle:D4EDA7DC-E2AB-45F3-9643-6D3030845696:data.txt"

But it’s easier to use this syntax

set toFile to "Macintosh HD:Users:sean:Library:Things To Do and Handle:D4EDA7DC-E2AB-45F3-9643-6D3030845696:data.txt"

open for access file toFile with write permission
set eof of toFile to 0
write "sss" to toFile starting at eof as text
close access toFile

Thanks for the replies! The thing that still boggles my mind is why it works with POSIX file?? :o

the result of POSIX file is «class furl» (file URL), which behaves a bit different

I’m not sure there is any good reason for it. The ‘file’ specifier form never has compiled separately in the whole history of AppleScript, as far as I know, except in commands that make immediate use of it. It can, however, exist independently in a variable, as returned by your two workrounds or by the ‘choose file name’ command.

The ‘file specification’ coercion’s been around for some time, but I don’t know what it’s for. It’s not part of the official AppleScript command set and the kind of ‘file’ it produces is apparently not quite the same as that produced by ‘POSIX file’ and ‘choose file name’:

set HFSpath to (path to desktop as Unicode text) & "Fred.txt"
set POSIXPath to POSIX path of HFSpath

-- In the following dialog, navigate to the desktop and type "Fred.txt" into the name field.
set CFNfile to (choose file name)

POSIXPath as POSIX file = CFNfile
display dialog result

HFSpath as file specification = CFNfile
display dialog result

If you want to use a variable rather than the literal file form with your ‘open for access’ command, then Stefan’s suggestion is the way to do it. However, I’d urge you to use the returned access reference thereafter rather than the ‘file’ and to use a ‘try’ block to ensure that the access gets closed in the event of an error:

set toFile to "Macintosh HD:Users:sean:Library:Things To Do and Handle:D4EDA7DC-E2AB-45F3-9643-6D3030845696:data.txt"

set fRef to (open for access file toFile with write permission)
try
	set eof fRef to 0
	write "sss" as text to fRef
end try
close access fRef