I am experimenting with an interesting site which curates a large set of APIs with sample code to retrieve data from the APIs - this is one exampe of a weather API:
The sample code gives examples in many different languages. Often the CURL example will work in KM as a shell script. But I would like to use Node JS or Javascript so I can manipulate/format the data after it is retrived
I am trying to figure out the right Shebang which works with Node JS specifically -and more generically for the many other languages the site supports.
I have tried:
#!/usr/bin node
#!/usr/bin/node
to no avail - though that is where Node is located when I search in Terminal.
If you want to test this example in KM, you can sign up for a RapidAPI account and do 1 million API calls in the free tier.
Can anyone help with a working example of a KM macro using Javascript or Node with this API or other examples on this site?
If the reason to use JavaScript is mainly to manipulate/format the data after it is retrieved, then you don't need JavaScript at all. Keyboard Maestro is capable of it. If it is too explicated for Keyboard Maestro to process the returned data, you may pass it to another script to process it. For instance, you may use Shell Script to do the curl to retrieve the data, and then pass the returned data to JavaScript to process it if you are more comfortable with JavaScript (I'm saying this because you said the curl runs properly in ShellScript but not in Node and you'd like to process the data with JavaScript).
#!/usr/local/bin/node
// get variable from KM whose name is "VarName" (without quotes)
// change VarName to whatever variable name you want.
var kmVar = process.env.KMVAR_VarName;
console.log(kmVar);
I'm sure you know that you may pass the Node result to KM variable or SystemClipboard etc.:
Come back to your problem. I installed Node16 and did some tests. I can't see your error msg. You may open the Keyboard Maestro Engine log file to see the full message. I had a similar one when I tried to import the 'applescript' module:
2021-08-09 12:50:54 Execute a Shell Script failed with script error: node:internal/modules/cjs/loader:936
throw err;
^
/privatetext-script
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object. (/privatetext-script:3:21)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/privatetext-script'
]
}
This was because when I installed the applescript module, npm installed it to my Home directory. I have to specify the full module path in my code: