I was wrong! The _javascript_ code DOES work with JSA!
var obj = JSON.parse('{\"timeblocksDefs\":
{ \"normal\": { \"morning\": \"06-09\", \"day\": \"09-18\", \"primetime\": \"18-22\", \"nighttime\": \"22-06\" }, \"lateprime\": { \"morning\": \"06-09\", \"day\": \"09-19\", \"primetime\": \"19-23\", \"nighttime\": \"23-06\" } }}');
JSON.stringify(obj.timeblocksDefs.lateprime);
—> "{\"morning\":\"06-09\",\"day\":\"09-19\",\"primetime\":\"19-23\",\"nighttime\":\"23-06\"}"
Then I just have to call JSA from Applescript. I Guess it is as easy as calling another applescript. (Thanks for the lead, Simon!)
Any thoughts about that, anyone?
Thanks.
I’ve had _javascript_ in mind, but then in the form of Extendscript, since this script is going to run with Indesign server. But the JSON-methods you used here in your Safari example doesn’t work with Extendscript. (And calling Safari is of course
not good solution for this use.)
I also tried your _javascript_ code with JSA, but JSON.stringify() doesn’t seem to work there either. Otherwise calling JSA would have been a nice solution.
J
Cheating but…
Fire up Safari. In the “Develop” menu “Allow _javascript_ from Apple Events” then:
tell
application "Safari"
set
theString
to do _javascript_ "var obj = JSON.parse('{\"timeblocksDefs\":
{ \"normal\": { \"morning\": \"06-09\", \"day\": \"09-18\", \"primetime\": \"18-22\", \"nighttime\": \"22-06\" }, \"lateprime\": { \"morning\": \"06-09\", \"day\": \"09-19\", \"primetime\": \"19-23\", \"nighttime\": \"23-06\" } }}');
JSON.stringify(obj.timeblocksDefs.lateprime);"
in
document 1
end
tell
theString
—> "{\"morning\":\"06-09\",\"day\":\"09-19\",\"primetime\":\"19-23\",\"nighttime\":\"23-06\”}"
Dunno if that helps point you towards a solution to your problem.
All the best
Simon
On 15 Nov 2016, at 15:20, Jörgen Stahle < email@hidden> wrote:
Hi!
I’m an applescripter with no professional knowledge of other programming. And with a bad knowledge of the right terms to use here. Anyway, her is a try to describe my problem
In a current project, I need a script to search a huge json for properties with certain names while ignoring most of the others. The properties are named by their key value, while the script only know this name as a string passed to it as a parameter.
To convert the json into an applescript record, I can use a script library that I once got from Shane Stanley in this forum that (thanks again, Shane!!) But now I want to be able to refer to a certain properties without the need to loop all the
json data – since the json is so huge and I have to do that kind of query many times in a time critical situation.
So – is there a solution out there that I can use to search dynamically for json properties from applescript?
Simplified example:
I want to be able to ask for the property ”lateprime” among the two properties under ”timeblocksDefs” in the small json below:
{"timeblocksDefs":
{
"normal":
{
"morning":
"06-09",
"day":
"09-18",
"primetime":
"18-22",
"nighttime":
"22-06"
},
"lateprime":
{
"morning":
"06-09",
"day":
"09-19",
"primetime":
"19-23",
"nighttime":
"23-06"
}
}}
In my real case, the json is of course far more complicated… Any suggestions welcome!
|