i386net Posted November 15, 2020 Share Posted November 15, 2020 Hi! I want to test requests to remote API using JS and then get the result to clipboard. run = (input) => { const hex = input[0]; fetch(`http://thecolorapi.com/id?hex=${hex}`) .then(res => { if(res.ok) { return res.json(); } return new Error('error') }) .then(data => JSON.stringify(data.rgb.value)) .catch(err => console.log(err)); } But when I use the standard fetch() method, I immediately get the the following error: execution error: Error: ReferenceError: Can't find variable: fetch (-2700). What am I doing wrong? Link to comment
deanishe Posted November 15, 2020 Share Posted November 15, 2020 1 hour ago, i386net said: But when I use the standard fetch() method A lot of common JS APIs are part of the browser, not JavaScript itself. fetch is one of them. The JS interpreter used by JXA doesn’t really have any IO APIs of its own. If you want to read/write files or retrieve URLs, you need to import Objective-C libraries (or use doShellScript()). Link to comment
i386net Posted November 15, 2020 Author Share Posted November 15, 2020 1 hour ago, deanishe said: The JS interpreter used by JXA doesn’t really have any IO APIs of its own. If you want to read/write files or retrieve URLs, you need to import Objective-C libraries (or use doShellScript()). Thank you for the answer. Unfortunately, I don't know Objective-C or AppleScript. But, Is it possible to use an external script and run it through NodeJS in this case? For example: /usr/local/bin/node main.js args And is it possible to use external packages in the main.js file, e.g. axios for data acquisition? If so, how can I get the data back to Alfred? Link to comment
deanishe Posted November 15, 2020 Share Posted November 15, 2020 6 minutes ago, i386net said: I don't know Objective-C You use its libraries, not the language itself. 7 minutes ago, i386net said: But, Is it possible to use an external script and run it through NodeJS in this case? You can run anything you want. Alfred doesn't care. Just put the appropriate command in the Script box or give your script an appropriate shebang. 9 minutes ago, i386net said: And is it possible to use external packages in the main.js file You can import JXA libraries, but not regular JS ones. You'd need to use a bundler like browserify for that. 11 minutes ago, i386net said: axios for data acquisition? Won’t work. Like I said, there are no IO APIs in JXA, and Axios needs XMLHttpRequest. Link to comment
i386net Posted November 15, 2020 Author Share Posted November 15, 2020 1 minute ago, deanishe said: Won’t work. Like I said, there are no IO APIs in JXA, and Axios needs XMLHttpRequest. Thanks !! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now