"open with write permission" changed from OS9

Just moving over to OSX and a script that used to work in OS9 no longer works in OSX and unfortunately I don’t have time to troubleshoot it due to workload demands. My script used to create an HTML file, open it with write permissions, write to it, and then close access to that file. Now, in OSX, the same script won’t open the file with write permission. The specific code that doesn’t work anymore is:

tell application “Finder”
set file_ref to open for access file filename with write permissions
end tell

When the script runs, I get a very cryptic (to me at least) error which states: “Can’t Make file (<>“index.html”) of application “Finder” into a file specification.”

This is probably a no-brainer for someone out there so your assistance is appreciated.

Thanks in advance

OS9’s Finder was extremely forgiving so could cope with all sorts of strange things being flung at it. OSX’s is not, hence the error.

First, ‘open for access’ et al are scripting addition commands, so there’s no reason for calling them inside a ‘tell application’ block (and it’s generally considered good practice not to). A common misconception amongst many scripters, for whatever reason.

Second, the ‘file’ keyword is used by both AppleScript and the Finder to mean different things. Inside a ‘tell app Finder’ block it indicates a Finder object. Outside, it indicates a file specification value, which is what ‘open for access’ uses (along with alias values, with are similar). Two very different things. OS9’s Finder took care of such details but OSX’s doesn’t, so you’ll have to do it yourself. Assuming the file doesn’t already exist, this means using something like:

set fileName to "index.html" -- whatever
tell application "Finder" to set folderAlias to desktop as alias -- wherever

set fileSpec to ((folderAlias as Unicode text) & fileName) as file specification
open for access fileSpec with write permission returing fileRef
...

A bit ugly, and having to mess with raw strings to build file paths isn’t ideal (it can cause problems if you’ve more than one volume by the same name). But it should get the job done.

Thanks, that helps. But still having problems. Based on your script suggestions, my question is what do I write to now?

Is it write “data” to file_ref

or

write “some text” to fileSpec

I’ve tried both and I either get an error stating that the file is already open, or is closed.

Doesn’t really matter, to be honest. But I prefer using the reference from ‘open for access’, since that’s what it’s there for.

on writeFile(fileSpec, txt)
   open for access fileSpec with write permission returing fileRef
   set eof of fileRef to 0
   write txt to fileRef
   close access fileRef
end writeFile

Sounds like you previously opened a file using ‘open for access’ and didn’t close it again when done with ‘close access’. You can’t open a file for access if it’s already open; you’ll have to close it first and then reopen it. It’s a pity AppleScript doesn’t bother to make stuff like this foolproof, but that’s just how it is.