Copy link to clipboard
Copied
I know that JSON isn't built into ExtendScript but even with the downloaded lib I do not understand how you are supposed to use this?
If I try and use #include in my jsx-file the entire script comes to a halt (nothing gets loaded)
main.jsx
#include "../js/libs/json2.js"
alert("this does not trigger! - script halted on line above");
So what's the approach here? Send the file path from JS to JSX, create a file object and read every line in JSX, returing the file string back to JS and then parse it?
Summary
-The JSON -library json2.js can be "included" in the main.js file by running evalFile(pathToFile). I assume this also works in JSX. Thanks goes to Davide Barranca for showing us the way!
-You cannot use #include in the JSX file for Photoshop as this will completely break the execution
-When reading the file, you have to do it all in one go - not line-by-line (see below).
I read the JSON-file incorrectly, which broke JSON.parse()
For some reason I had setup the function that did the reading, t
...Copy link to clipboard
Copied
I understand that you're referring to an HTML Panel context, am I correct?
If this is the case, yes, include will not work. You have to send the extension's path down to the JSX from the JS, then evaluate json2.js with $.evalFile().
From that point onwards, the JSON object should be available to your extendscript context.
Davide Barranca
Copy link to clipboard
Copied
I moved the parsing to the JS file instead but it appears that you can't even use JSON there! JSON is undefined -_-
Why did Adobe make things so bloody complicated?
EDIT
I put the JSON.parse() inside a try-catch and I'm getting syntax errors (which is weird as I've used several online validators on my JSON code)
Exception:SyntaxError: Unexpected token :
JSON
{
"lolk": [
{
"icon": "/img/picA.png",
"path": "/scripts/file.a",
"text": "somethingA"
},
{
"icon": "/img/picB.png",
"path": "/scripts/file.b",
"text": "somethingB"
},
{
"icon": "/img/picC.png",
"path": "/scripts/file.c",
"text": "somethingC"
}
]
}
EDIT2
JSON.parse() seems to be completely broken?
try{
var lol = JSON.parse("{'lolk':'stuff'}"); // Exception:SyntaxError: Unexpected token '
alert(lol);
} catch(e) {
alert("Exception:" + e + "\nCould not run JSON.parse()");
}
Copy link to clipboard
Copied
If you want to parse JSON in extendscript, you can use a polyfill. Here's an easy example you can copy/paste: generator-gizmo/rootscript.jsx at master · codearoni/generator-gizmo · GitHub
Just paste that at the top of you extendscript file and you should be good to go.
Copy link to clipboard
Copied
Summary
-The JSON -library json2.js can be "included" in the main.js file by running evalFile(pathToFile). I assume this also works in JSX. Thanks goes to Davide Barranca for showing us the way!
-You cannot use #include in the JSX file for Photoshop as this will completely break the execution
-When reading the file, you have to do it all in one go - not line-by-line (see below).
I read the JSON-file incorrectly, which broke JSON.parse()
For some reason I had setup the function that did the reading, to read the file row-by-row, like this:
JSX
file = new File(filePath);
file.open("r", "TEXT");
var fileString = "";
while (!file.eof){
var line = file.readln();
if (fileString.indexOf(line) == -1){
fileString += line;
}
}
return fileString;
Which seems to add characters that the parser does not like.
I changed it so that it just reads the entire file
JSX
var scriptFile = File(filePath);
scriptFile.open('r');
var content = scriptFile.read();
scriptFile.close();