Loading local text file

I am long-time user of omnigraffle who is new to automation and JavaScript. I would like to load a text file and parse the contents, which are needed to lay out a diagram. I think that what I want is to create a URL that points to the text file, use the fetch function on it, and give it a callback function to parse the data, so something like

fileURL = URL.choose([‘public.text’])
fileURL.fetch( parseTextFile )
function parseTextFile( data ) {…}

but I can’t figure how to parse the object ‘data’ inside the function parseTextFile. Since fileURL is just pointing to a text file, I would have expected data to just be a string with the file contents. All the examples of using fetch on the omni-automation webpage involve accessing images…

macOS or iOS ? Or does it need to be both ?

Just on macOS.

One approach is to read the file in JXA, and pass it on from there to omniJS.

(The JXA method .evaluateJavascript(strCode) lets you pass a function and data from JXA to omniJS for evaluation in OG’s own JSContext)

(see the link below for more details)

In JXA I would use a function like:

// readFile :: FilePath -> IO String
const readFile = strPath => {
    const
        error = $(),
        str = ObjC.unwrap(
            $.NSString.stringWithContentsOfFileEncodingError(
                $(strPath)
                .stringByStandardizingPath,
                $.NSUTF8StringEncoding,
                error
            )
        );
    return Boolean(error.code) ? (
        ObjC.unwrap(error.localizedDescription)
    ) : str;
};

e.g.

const strText = readFile('~/sample.txt')