0

So I'm trying to write a script that writes any text I want it to to a new file on my Desktop.

set the theText to "hello world"
set fileName to "file.txt"
set thePath to "~/Desktop/"
try
    tell application "Finder" to make file at thePath with properties {name:fileName}
end try
set myFile to open for access (thePath & fileName) with write permission
write theText to myFile
close access myFile

This code is based on the second most liked answer on this post. When I try to execute the version I have above though, it gives me the error: "File not open with write permission." number -61 from "~/Desktop/file.txt" to «class fsrf». I tried running the code my code is based on and it worked flawlessly. I also tried running it with choose file and that also worked. Why is Applescript having problems when working with predefined paths?

gurkensaas
  • 793
  • 1
  • 5
  • 29
  • 1
    The Finder does not know about POSIX paths. – red_menace May 27 '21 at 20:19
  • @red_menace I've seen similar comments before about the Finder and posix paths but I don't understand it. My understanding is that posix paths are simply file specifiers and are built into applescript — the same as aliases and files. I'm not sure what the Finder has to do with it? You can certainly tell the Finder to work with posix-style references. Note, I'm not questioning the answer, as I agree that the Finder isn't necessary to create a file. Just wondering if you could elaborate on the comment above. – Mockman May 28 '21 at 20:44
  • @Mockman : The OP tried to use the Finder to create a file using a POSIX path - some terminology can use either format, but the Finder uses colon separated HFS paths. When using the Finder with POSIX paths, the `POSIX file` specifier needs to be used to specify a file using a POSIX path (`System Events` can also be used). See [Aliases and Files](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW28) section in the AppleScript Language Guide for more information. – red_menace May 28 '21 at 23:30
  • But isn't that just a syntax issue? The OP's code could have been made to create the file by simply including 'posix file' (putting aside the tilde issue, which required fixing regardless). Again, I agree with the answer but if for example, the intent was to use a shell script to write text and do other stuff, then a posix file approach would have been fine. Framing it this way is kind of ominous-sounding, as though this approach is fraught with peril and doomed to failure. It does work even if there are better approaches. – Mockman May 29 '21 at 02:20
  • @Mockman AppleScript is a relict of pre-OS X times. In System 7–9 there was no UNIX subbase so there were no POSIX paths. In OS X Apple gradually enabled support for POSIX paths in many APIs like System Events and Scripting Additions but the Finder always stayed in the HFS bubble. – vadian May 29 '21 at 04:01

1 Answers1

2

As already mentioned in the comments The Finder does not know about POSIX paths

But even if it did open for access does not know how to expand the tilde, you have to specify the full path.

The Finder is not needed at all to create a file for writing text into. Basically this is sufficient

set the theText to "hello world"
set fileName to "file.txt"
set thePath to "/Users/myself/Desktop/"
set myFile to open for access (thePath & fileName) with write permission
write theText to myFile
close access myFile

However this is bad practice because the write command could fail and then the file remains in an undefined open state.

The most reliable and user independent syntax is

set the theText to "hello world"
set fileName to "file.txt"
set thePath to POSIX path of (path to desktop)
set myFile to thePath & fileName
try
    set fileDescriptor to open for access myFile with write permission
    set eof of fileDescriptor to 0
    write theText to fileDescriptor starting at eof
    close access fileDescriptor
on error
    try
        close access myFile
    end try
end try
vadian
  • 274,689
  • 30
  • 353
  • 361
  • It works great, except it doesn't remove the old text that was in the file before and just overwrites it. So if I for example have `"hello world"` and then just `"Dog"`, it won't be `Dog`, it will be `Doglo world`, but I just want it to be `Dog`. – gurkensaas May 27 '21 at 21:33