Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can and how do you get remote data (e.g. JSON) into AppleScript?

Tags:

applescript

I've got a Web-based API to which I would like to send POST/GET requests via AppleScript. I'd like to retrieve and parse the response such that I can feed it into another app.

Is this possible? If so, how?

The JSON data would, for example, look like this:

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

like image 996
buley Avatar asked Jan 05 '11 05:01

buley


3 Answers

I needed to parse JSON in AppleScript, and made a very simple scriptable background app to do this. It really just ties two frameworks (JSON, Appscript) together.

It's now available free on the Mac AppStore. You can check out more examples on our website.

Usage 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
like image 63
davidb Avatar answered Oct 16 '22 06:10

davidb


To answer the specific question (after a quick reread), the only web support Applescript has is through the URL Access Scripting library, which is just a wrapper for the terminal's curl command. It's a bit buggy and doesn't report back everything as it should.

Beyond that, there is no native JSON support in Applescript either, and doing so is going to be a bit painful. In order to parse the JSON, you'll need to use the Applescript's text item delimiters.

set mJson to "\"result\":\"success\",\"image\":\"foo\", \"name\":\"bar\"" -- get your data into a string somehow, like a function
set AppleScript's text item delimiters to {","}
set keyValueList to (every text item in mJson) as list
set AppleScript's 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's text item delimiters to {":"}
    set theKeyValueBufferList to (every text item in theKeyValuePair) as list
    set AppleScript's text item delimiters to ""
    set theKey to item 1 of theKeyValueBufferList
    (*"result"*)
    set theValue to item 2 of theKeyValueBufferList
    (*"success"*)
end repeat

This is all done when everything goes right. You'll have to take into consideration badly-formed JSON, as in your example which contains an extra comma where it doesn't belong, and variances like extra spaces and the like. If you can manipulate the data elsewhere to get what you need, I would suggest doing so. Applescript isn't very good for things like this.

like image 36
Philip Regan Avatar answered Oct 16 '22 07:10

Philip Regan


I needed a version which didn't require any new dependencies (like installing an app). So I made a applescript only json encoder/decoder.

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

like image 44
user23127 Avatar answered Oct 16 '22 08:10

user23127