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

前端 未结 4 1155
猫巷女王i
猫巷女王i 2021-02-04 19:44

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.

相关标签:
4条回答
  • 2021-02-04 19:51

    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.

    0 讨论(0)
  • 2021-02-04 19:59

    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

    0 讨论(0)
  • 2021-02-04 20:01

    I parse XML/HTML/JSON etc. using regular expressions. AppleScript doesn't have native support for regular expressions but you can download a scripting addition called Satimage which will allow you to use them in your Applescripts.

    Download and install the scripting addition and then check out the Satimage user guide for instructions and sample code.

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

    0 讨论(0)
  • 2021-02-04 20:06

    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
    
    0 讨论(0)
提交回复
热议问题