First time on JXA, stumped on variables and results

Hi everyone,

a system is producing a broken CSV file that has one delimiter too much at the end of each line.

In order to remove this superfluous semicolon, I decided to run it through JXA (first time trying it with JXA...) and the script works in some way (meaning it produces no errors), but I can't get the output out of it.

I run a ForEach loop for each line in the CSV file variable "QSSemikolonfixCSV", each line is a "QSSemikolonfixCSVZeile", and the output... well, all I got so far was "true".

Where is my mistake? Here is the code:

function removeFinalSemicolon(csvLine) {
    // Remove the final semicolon if it exists
    if (csvLine.endsWith(";")) {
        return csvLine.slice(0, -1);
    return csvLine;

// Example usage
var app = Application.currentApplication()
app.includeStandardAdditions = true
var kme = Application("Keyboard Maestro Engine")

var csvLine = kme.getvariable("QSSemikolonfixCSVZeile");

var result = removeFinalSemicolon(csvLine);

kme.setvariable("QSSemikolonfixCSVZeile", { to: result});

I know nothing about JXA, but you could do the entire thing in one swipe with a relatively easy regular expression:


That should remove any semicolon in the file that's the last character on any line. I tested with a multi-line CSV variable, and it worked fine.



No need for .setvariable or .getvariable in KM 11 (let alone for the opaque complexities of file paths and regular expressions :slight_smile: ), you just use a return

Without trailing semicolon.kmmacros (3.4 KB)

Expand disclosure triangle to view JS source
return kmvar.local_Source.split("\n")
    line => line.endsWith(";")
        ? line.slice(0, -1)
        : line


Note that in Keyboard Maestro 11(+), the Execute JavaScript for Automation action allows you, (with the 'Modern Syntax' option which requires a return expression)
to specify which (if any) Keyboard Maestro variables are made available to the JS code.

See the menu behind the small left-hand chevron:

action:Execute a JavaScript For Automation [Keyboard Maestro Wiki]

1 Like