Here's some example code, but please note: This code doesn't actually do anything of relevance, other than demonstrate the error I'm trying to solve. In fact, the code in question isn't even invoked in the JXA script - its mere presence causes problems.
#!/bin/bash
osascript -l JavaScript <<JXA_END 2>/dev/null
(function() {
'use strict';
// NOTE: This never gets called
function readTextFile(strPath) {
var error;
var str = ObjC.unwrap(
$.NSString.stringWithContentsOfFileEncodingError(
$(strPath).stringByStandardizingPath,
$.NSUTF8StringEncoding,
error
)
);
if (error)
throw Error('Could not read file "' + strPath + '"');
return str;
}
return "Hello World";
})()
JXA_END
This code, minus the osascript wrapper of course, runs just fine in Script Editor or from Atom. But execute this script in a Terminal window, or in a KM Plugin, and I get the following error (under El Cap 10.11.6):
line 2: strPath: command not found
I'm pretty sure the error message is misleading. I think the error has to do with the ObjC code. I've tried various Objc.import( )
statements, but haven't found anything to help.
Anyone got any ideas? @ComplexPoint - I found a post of yours from way back when that kind-of alluded to a similar issue, and I wonder if you ever figured out what the issue was/is?
Thanks.
PS: I know there are other ways to read a file. This is just one example of ObjC code that doesn't work; some of the other ObjC things I might want to do can't be as easily replaced with other methods.