tell current application
open for access "/tmp/myfile.txt" with write permission
--> 96
end tell
What does 96 mean?
The code looks like this:
try
set fileDescriptor to open for access myFile with write permission
set eof of fileDescriptor to 0
write myString to fileDescriptor starting at eof
close access fileDescriptor
on error errMsg
More from the log following this
tell application "Safari"
set eof 96 to 0
--> error number -10004
end tell
tell current application
set eof 96 to 0
end tell
tell application "Safari"
write "
[content of the string]
" to 96 starting at eof
--> error number -10004
end tell
tell current application
write "
[content of the string]
" to 96 starting at eof
end tell
tell application "Safari"
close access 96
--> error number -10004
end tell
tell current application
close access 96
end tell
As you can see 96 appears multiple times. What is that?
It’s value of your ‘fileDescriptor’ variable — the reference number your script’s been given on this occasion by the open for access. You might get a different number on other occasions, even for the same file. It’s the system’s reference for the particular access channel it’s created to the file for you. This channel includes a pointer to whereabouts in the file you’ve got to while reading or writing to it. It’s quite important that if you use open for access, the script run should survive long enough to close the channel using that number in the event of an error. In other words, a close access command should also be included in the on error section of the try statement.
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/03/04 09:38
# dMod: 2019/03/04 09:38
# Appl: Miscellaneous
# Task: Write Handlers
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Write, @Handlers
--------------------------------------------------------
set theText to "•• Now is the time for all good mén to come to the aid of their country ••" & linefeed
writeFile(theText, "~/Downloads/Test 01.txt", eof, «class utf8»)
writeUTF8(theText, "~/Downloads/Test 02.txt")
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on writeFile(srcData, targetFilePath, startPosition, dataType)
try
if targetFilePath starts with "~/" then
set targetFilePath to POSIX path of (path to home folder as text) & text 3 thru -1 of targetFilePath
end if
set fileRef to open for access targetFilePath with write permission
if startPosition = 0 then
set eof of fileRef to 0
end if
write srcData to fileRef as dataType starting at startPosition
close access fileRef
on error errMsg number errNum
try
close access fileRef
end try
error "Error in writeFile() handler of library: gen.lib" & return & return & errMsg
end try
end writeFile
--------------------------------------------------------
on writeUTF8(_text, targetFilePath)
try
if targetFilePath starts with "~/" then
set targetFilePath to POSIX path of (path to home folder as text) & text 3 thru -1 of targetFilePath
end if
set fRef to open for access targetFilePath with write permission
set eof of fRef to 0
write _text to fRef as «class utf8»
close access fRef
if targetFilePath contains ":" then
return alias targetFilePath
else if targetFilePath starts with "/" then
return alias POSIX file targetFilePath
end if
on error e number n
try
close access fRef
on error e number n
error "Error in writeUTF8() handler of library: gen.lib" & return & return & e
end try
end try
end writeUTF8
--------------------------------------------------------
Although these days I’m more inclined to use AppleScriptObjC most of the time:
--------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/06/13 13:59
# dMod: 2018/07/26 15:34
# Appl: AppleScriptObjC
# Task: Write UTF8 Text to a file – creating the directory path as required.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Write, @UTF8, @Text, @File, @Create, @Make, @Directory, @Folder
--------------------------------------------------------
use AppleScript version "2.4" -- Yosemite & later
use framework "Foundation"
use scripting additions
--------------------------------------------------------
set fileContent to "This–String—Has–Some–ÜTF8-Characters…"
set fileName to "test file 01.txt"
set newDirPath to POSIX path of ((path to desktop folder as text) & "test01:test02:")
set newFilePath to newDirPath & fileName
its createDirectoryAtPath:newDirPath
its writeString:fileContent toPath:newFilePath
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on createDirectoryAtPath:thePath
set {theResult, theError} to current application's NSFileManager's defaultManager()'s createDirectoryAtPath:thePath withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
if not (theResult as boolean) then
set errorMsg to theError's localizedDescription() as text
error errorMsg
end if
end createDirectoryAtPath:
--------------------------------------------------------
on writeString:aString toPath:posixPath
set anNSString to current application's NSString's stringWithString:aString
anNSString's writeToFile:posixPath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeString:toPath:
--------------------------------------------------------
# Other encodings: NSMacOSRomanStringEncoding
--------------------------------------------------------