Here is a better AppleScript to write text to file:
Tested running Keyboard Maestro 9.2 on macOS 10.14.6 (Mojave).
AppleScript to Write Text to File
property ptyScriptName : "Create & Write String to File UTF8 ASObjC"
property ptyScriptVer : "1.1"
property ptyScriptDate : "2021-02-21"
property ptyScriptAuthor : "JMichaelTX"
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
try
### --- Set Your File HFS Path Here ---
set fileHFS to "Macintosh HD:Users:home:Desktop:Lists:NameofList.txt"
set filePath to POSIX path of fileHFS
set fileContents to "
This file was created by AppleScript, using ASObjC.
"
set results to my writeFile(filePath, fileContents)
return filePath
on error errMsg number errNum
if errNum = -128 then ## User Canceled
set errMsg to "[USER_CANCELED]"
end if
set scriptResults to "[ERROR] [MACRO CANCELLED] " & errMsg
set currentApp to path to frontmost application as text
tell application currentApp
display notification errMsg ¬
with title ("SCRIPT: " & ptyScriptName & " " & ptyScriptVer) ¬
subtitle "[ERROR] MACRO CANCELLED " sound name "glass"
end tell
--» THROW ERROR ~~~~~~~
error scriptResults
end try
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on writeFile(pPosixPath, pStringToWrite)
(* VER: 1.1 2020-01-07
---------------------------------------------------------------------------------
PURPOSE: Write String to File. Will be created if necessary.
PARAMETERS:
• pPosixPath ┃ text ┃ Posix Path of File to write to
• pStringToWrite ┃ text ┃ String to be written
RETURNS: ┃boolean┃ true if successful; else error is thrown
AUTHOR: JMichaelTX
REF:
1.
—————————————————————————————————————————————————————————————————————————————————
*)
local successfulBool
set nsStringToWrite to current application's NSString's stringWithString:pStringToWrite
set nsPosixPath to (current application's NSString's stringWithString:pPosixPath)'s stringByExpandingTildeInPath
set successfulBool to nsStringToWrite's writeToFile:nsPosixPath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
if (successfulBool is false) then
error "Unable to write to file: " & pPosixPath
end if
return successfulBool
end writeFile
--~~~~~~~~~~~~~~~ END OF handler writeFile ~~~~~~~~~~~~~~~~~~~~~~~~~