Can and how to get deleted data (e.g. JSON) in AppleScript?

I have a web interface to which I would like to send POST / GET requests via AppleScript. I would like to receive and analyze the answer so that I can submit it to another application.

Is it possible? If so, how?

JSON data, for example, looks like this:

{"result":"success","image":,"foo", "name":"bar"}

+6
applescript
source share
5 answers

To answer a specific question (after a quick re-reading), the only Applescript web support is through the URL Access Scripting library, which is just a shell for the curl terminal command. It is a little buggy and does not report everything that is needed.

In addition, there is no native JSON support in Applescript, and it will be a bit painful. To parse JSON, you need to use Applescript text item delimiters .

 set mJson to "\"result\":\"success\",\"image\":\"foo\", \"name\":\"bar\"" -- get your data into a string somehow, like a function set AppleScript text item delimiters to {","} set keyValueList to (every text item in mJson) as list set AppleScript text item delimiters to "" (*"result":"success", "image":"foo", "name":"bar"*) repeat with thiskeyValuePair from 1 to (count keyValueList) set theKeyValuePair to item thiskeyValuePair of keyValueList set AppleScript text item delimiters to {":"} set theKeyValueBufferList to (every text item in theKeyValuePair) as list set AppleScript text item delimiters to "" set theKey to item 1 of theKeyValueBufferList (*"result"*) set theValue to item 2 of theKeyValueBufferList (*"success"*) end repeat 

All this is done when everything goes right. You will need to take into account ill-formed JSON, as in your example, which contains an extra comma where it does not belong, and deviations, such as extra spaces and the like. If you can manipulate the data elsewhere to get what you need, I would suggest doing it. Applescript is not very good for such things.

+3
source share

I needed to parse JSON in AppleScript and make a very simple scripting application for this. It really just binds the two structures (JSON, Appscript) together.

Now it is available for free on the Mac AppStore. You can see other examples on our website .

Using is very simple:

 tell application "JSONHelper" -- return JSON from an AppleScript list set jsonString to make JSON from {"A", "B", "C"} log jsonString set asList to read JSON from jsonString -- return JSON from an AppleScript record set jsonString to make JSON from {a_string:"string", a_list:{"abc", 123, false, true}} log jsonString -- return an AppleScript record from JSON set asRecord to read JSON from jsonString log asRecord end tell 
+8
source share
The question was years, but by now, there seems to be a solution available:

JSON Helper is an agent (or scripting application) that allows you to do useful things using JSON (JavaScript Object Notation) directly from AppleScript.

http://www.mousedown.net/mouseware/JSONHelper.html App Store

+2
source share

I needed a version that did not require new dependencies (for example, installing the application). Thus, I made the box only json encoder / decoder.

https://github.com/KAYLukas/applescript-json

+1
source share

I parse XML / HTML / JSON etc. using regular expressions. AppleScript does not have built-in regex support, but you can download a script add-on called Satimage , which allows you to use them in your Applescripts.

Download and install the script add-on, and then read the Satimage User Guide for instructions and sample code.

If you are not familiar with regular expressions (or even if there are), an application called RegExhibit will help you find the right syntax for your scripts.

0
source share

All Articles