Accessing items in a list in AppleScript?

I’m trying to access items in a list via AppleScript. Try as I might, I can’t seem to access them. I’ve pasted my code below

tell application “JSON Helper”
set result to fetch JSON from “https://newsapi.org/v2/top-headlines?sources=bbc-news&pageSize=1&apiKey=X

set news to title of articles of result

end tell

set result_string to news & “”

note, I’ve removed my api key.

The api format is :

{“status”:”ok”,”totalResults”:10,”articles”:[{“source”:{“id”:”bbc-news”,”name”:”BBC News”},”author”:”BBC News”,”title”:”Trump urges Israeli ‘care’ on settlements”,”description”:”The US president also casts doubt on whether the Palestinians or Israel are ready to talk peace.”,”url”:”http://www.bbc.co.uk/news/world-middle-east-43025705“,”urlToImage”:”https://ichef.bbci.co.uk/news/1024/branded_news/165CD/production/_99979519_044058382.jpg“,”publishedAt”:”2018-02-11T16:47:27Z”}]}

I’m trying to access title but I keep getting “can’t get title”.

Any input would be much appreciated. Thanks!

One thought on “Accessing items in a list in AppleScript?”

  1. Using the sample JSON data you gave, I used this command:
    tell application "JSON Helper" to read JSON from ¬
    "{
    \"status\": \"ok\",
    \"totalResults\": 10,
    \"articles\": [
    {
    \"source\": {
    \"id\": \"bbc-news\",
    \"name\": \"BBC News\"
    },
    \"author\": \"BBC News\",
    \"title\": \"Trump urges Israeli 'care' on settlements\",
    \"description\": \"The US president also casts doubt on whether the Palestinians or Israel are ready to talk peace.\",
    \"url\": \"http://www.bbc.co.uk/news/world-middle-east-43025705\",
    \"urlToImage\": \"https://ichef.bbci.co.uk/news/1024/branded_news/165CD/production/_99979519_044058382.jpg\",
    \"publishedAt\": \"2018-02-11T16:47:27Z\"
    }
    ]
    }"

    to retrieve an AppleScript record. Pretty-printing it as I have above, however, you might already be able to see that articles is a list (containing one item). Therefore, whilst this:
    set news to title of articles of result
    produces an error, this:
    set news to title of item 1 of articles of result
    retrieves the correct datum.

Leave a Reply

Your email address will not be published.