Write To File Error?

I’m wrapping this in a tell current application as indicated here http://macscripter.net/viewtopic.php?id=38090 and here www.macosxautomation.com/applescript/apps/gotchas.html but it’s resulting in an error.

tell current application
	set refFileToWRite to (open for access pathToSave with write permission)
	-- Result = An error of type -1409 has occurred. (error -1409)
end

I tried adapting the AS Obj-C version shown on the first link like this but it’s not working either:

set theMessage to current application's NSString's stringWithFormat_("%@
		", textToSend)
set theMessageData to theMessage's dataUsingEncoding_(current application's NSUTF8StringEncoding)
set logPath to POSIX path of pathToSave
set fileManager to current application's NSFileManager's defaultManager()
set theResult to fileManager's createFileAtPath_contents_attributes_(logPath, theMessageData, missing value)
set logFileHandle to current application's NSFileHandle's fileHandleForWritingAtPath_(logPath)
logFileHandle's seekToEndOfFile()
logFileHandle's writeData_(theMessageData)
logFileHandle's closeFile()

-- Result = unrecognized function seekToEndOfFile. (error -10000)

Does write to file work inside a tell current application or not? If so, I’d prefer that over the AS Obj-C version which I totally don’t understand. Any tips on what I’m doing wrong here?

I should mention that I have a write to file working fine in another app… This one is writing to a server volume. I definitely have write permission, but is there some problem with coin that?

Typo above… it’s actually “file” pathToSave. Still generates the error.

tell current application
	set refFileToWRite to (open for access file pathToSave with write permission)
end

The read/write commands are unlike to be your problem. This works fine:

      set pathToSave to "/Users/shane/Desktop/AAA.txt"
      tell current application
         set refFileToWRite to (open for access pathToSave with write permission)
         write "blah" to refFileToWRite
         close access refFileToWRite
      end
      set pathToSave to "Macintosh HD:Users:shane:Desktop:BBB.txt"
      tell current application
         set refFileToWRite to (open for access pathToSave with write permission)
         write "blah" to refFileToWRite
         close access refFileToWRite
      end
      set pathToSave to "Macintosh HD:Users:shane:Desktop:CCC.txt"
      tell current application
         set refFileToWRite to (open for access file pathToSave with write permission)
         write "blah" to refFileToWRite
         close access refFileToWRite
      end
      set logPath to "/Users/shane/Desktop/DDD.txt"
      set theMessage to current application's NSString's stringWithFormat_("%@
      ", "blah blah")
      set theMessageData to theMessage's dataUsingEncoding_(current application's NSUTF8StringEncoding)
      set fileManager to current application's NSFileManager's defaultManager()
      set theResult to fileManager's createFileAtPath_contents_attributes_(logPath, theMessageData, missing value)
      set logFileHandle to current application's NSFileHandle's fileHandleForWritingAtPath_(logPath)
      logFileHandle's seekToEndOfFile()
      logFileHandle's writeData_(theMessageData)
      logFileHandle's closeFile()

Never do that! A file, locally or remote, can only be opened once with write permissions and thousands of files beeing opened with read permissions. So if user A opens the file and user B wants to open the file as well, an error is thrown from the file system. When user A messes up something the file state can remain opened and nobody is able to open the file again until you restart the server.

Today’s software write to a temporary file and eventually rename the temporary file to the destination file. So when something happens during writing, the original file stays intact and you only lose your changes but not the entire file.

Don’t panic! :slight_smile:

In this case, the script is creating the file with the user’s name, date and time, writing and closing it. That’s it. Instructions are being pushed to an automated server for further processing. However, your warning is certainly prudent…

Sure enough, you are correct!!

I am embarrassed to admit that I wasn’t putting the file name onto the path, so the script was trying to write to a folder rather than a file. DUH!

So, now it IS writing to the file successfully. HOwever, after doing that, Xcode logs:

malloc: *** auto malloc[16171]: error: GC operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.

Anyone know what that nonsense is all about?

It’s a garbage-collection bug in one of the frameworks. You get them occasionally. Fortunately the message is telling you the problem has been fixed.