I used this on a daily basis to read a htm or html file to a virable, but after updating to the newest version (9.0.5), this error occured:
MACRO ERROR on 2020-04-03 10:22:44
This info was pulled from the KM Engine Log for the
Last Macro error that occured.
Macro: Trying
Action: Read File to Variable “sourceText”
Status: ABORTED
ERROR:
––––––––––––––––––––––––––––––––––––––––––––––––––
Read File action failed to read text file with error Error Domain=NSCocoaErrorDomain Code=264 "The file “temp.html” couldn’t be opened because the text encoding of its contents can’t be determined." UserInfo={NSFilePath=/Users/xxx/Downloads/temp.html}
––––––––––––––––––––––––––––––––––––––––––––––––––
Check the file encoding of the file. The Read File action reads the file as a string, and the system determines the appropriate character encoding. Seems like the system cannot determine the encoding (maybe there are multiple different encodings in the file, some UTF8, some ISO-8859-1 Latin or something like that).
I'm getting the same error while KM tries to read a file that it has previously created through an Apple script. The file is a .txt file and it can be opened and viewed in Text Edit without a problem.
This is the part of the script that writes the file:
set _output to "Macintosh HD:Users:home:Desktop:Lists:NameofList.txt"
set _reference to open for access file _output with write permission
write _body to _reference
close access _reference
end try
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 ~~~~~~~~~~~~~~~~~~~~~~~~~