The normal behavor for NSFileManager class. To save string of data to a path that is a folder.
The reason why the below code return false is becouse I have folder with name EditFolder inside the Desktop folder. In other words I couldn’t write data to a folder.
I also tested if it was possible to write to a protected folder, it was not. (return false)
use framework "Foundation"
use scripting additions
set manager to current application's NSFileManager's defaultManager()
set desktopFolder to manager's URLsForDirectory:(current application's NSDesktopDirectory) inDomains:(current application's NSUserDomainMask)
set desktopFolder to (desktopFolder's valueForKey:"path") as string
set desktopFolder to desktopFolder & "/EditFolder"
set theString to current application's NSString's stringWithString:"hello"
set theData to theString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
theData's writeToFile:desktopFolder atomically:true
My point here is to show the NSFileManager behavor is corrent and AppleScript is not.
You cannot write data to a folder with FileManager
because it’s required to specify always the full destination path including the file name plus extension.
From the Objective-C perspective your code contains two questionable practices.
-
URLsForDirectory:inDomains:
returns an array. Better than flatten the array with KVC – what is supposed to happen if the array contains multiple items? – get the item with firstObject
-
It’s highly recommended to compose paths with the URL related API because it handles the slash separators reliably on your behalf.
use AppleScript version "2.5"
use framework "Foundation"
set manager to current application's NSFileManager's defaultManager()
set desktopFolders to manager's URLsForDirectory:(current application's NSDesktopDirectory) inDomains:(current application's NSUserDomainMask)
set desktopFolder to desktopFolders's firstObject()
set editFolder to desktopFolder's URLByAppendingPathComponent:"EditFolder"
set theString to current application's NSString's stringWithString:"hello"
set theData to theString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
theData's writeToURL:editFolder atomically:true
Edit:
The code run in Swift (which provides an error handling) throws this error:
Error Domain=NSCocoaErrorDomain Code=512 “The file “EditFolder” couldn’t be saved in the folder “Desktop”.” NSUnderlyingError=0x600000243840 {Error Domain=NSPOSIXErrorDomain Code=21 “Is a directory”}}
1 Like
Thanks StefanK, the script was only to show my ‘point of view’ of software behavor in computer system. There documents are files and not folders. Everywhere I have test that behavor it return the same result. But that is not what happen in AppleScript.
But I stating to wonder if the problem was the alias class. I used (path to desktop)
I’m speaking about this topic: Big warning to all AppleScript users - #15 by Fredrik71
The save command could return something like this
use AppleScript version "2.5"
use framework "Foundation"
set manager to current application's NSFileManager's defaultManager()
set desktopFolders to manager's URLsForDirectory:(current application's NSDesktopDirectory) inDomains:(current application's NSUserDomainMask)
set desktopFolder to desktopFolders's firstObject()
set editFolder to desktopFolder's URLByAppendingPathComponent:"EditFolder"
set theString to current application's NSString's stringWithString:"hello"
set theData to theString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {writeData, theError} to theData's writeToURL:editFolder options:0 |error|:(reference)
if writeData is false then error (theError's localizedDescription() as string)
error “The file “EditFolder” couldn’t be saved in the folder “Desktop”.” number -2700