3

I'm querying the ipstack API for data with a list of IPs using a repeat loop and JSON Helper.

I'm looping through a list of IPs, but I can't get the Script Editor to return all the results; I only get the one cityName of the last IP in theListOfips in the results pane, "Tallahassee".

How can I get the Script Editor to show a list of all cities in the result pane, with a carriage return after each?

 set theListOfips to {"104.137.108.23", "107.140.9.50", "146.201.166.248"}

    tell application "JSON Helper"

        repeat with theCurrentValue in theListOfips

            delay 2

            set json to fetch JSON from
   "http://api.ipstack.com/" & theCurrentValue & "?access_key=theAPIkey&fields=city"

            set cityName to city of json

        end repeat

    end tell
1
  • AppleScript only returns the result of the last statement executed, so you would need to build a list of the cities, adding to it each time through the loop, then return the list.
    – red_menace
    Commented Nov 10, 2019 at 19:55

2 Answers 2

3

The immediate solution would be to use the Replies pane rather than the Results pane in Script Editor. It will print a live result from each AppleScript command executed during the run of a script, whereas the Results pane only returns the overall result of the script's execution (which equates to the result of only the final command).

The final command in your script will be set cityName to city of json, and so the result of your script will be the value of cityName after it has been set to the value from the JSON record that pertains specifically to the last IP address in your list, i.e. "146.201.166.248".

So for a more comprehensive solution, you can edit your script slightly so that the last command will be one that returns what you actually want. You can do this by first declaring a variable as an empty list, into which you would add an item of data in each iteration of your repeat loop. After the repeat loop is complete, your final command would simply be a reference to that list so as to have AppleScript evaluate its contents and return the result:

set APIKey to "<your secret key>"
set URLRelativePath to "http://api.ipstack.com/"
set URLQueryString to "?access_key=" & the APIKey & "&fields=city"

set IPaddresses to {"104.137.108.23", "107.140.9.50", "146.201.166.248"}

set cityNames to {}

tell application "JSON Helper" to repeat with IPaddress in the IPaddresses
        fetch JSON from the URLRelativePath & the IPaddress & the URLQueryString
        set the end of cityNames to the contents of the result's city
        --OR: set the end of cityNames to the contents of {the IPaddress, the result's city}
end repeat

return the cityNames

This returns a list of all the city names (or a list of lists of IP address-city name pairs if you elect to use the line I commented out as an alternative). To make this list into a single piece of text with one city per line, use the list that contains city names only; set the text item delimiters to linefeed; then coerce the list to text. So, instead of return the cityNames, you would have:

set my text item delimiters to linefeed
return the cityNames as text
2
  • 1
    Thanks! That works great. It's also cleaner that what I cam up with. Commented Nov 10, 2019 at 23:41
  • @BlueDogRanch You're most welcome. Thanks for the tick.
    – CJK
    Commented Nov 11, 2019 at 0:33
1

Try using the log() function, it will log the results to the messages pane.

Example:

set i to 1

repeat 10 times

    log (i)

    set i to i + 1
end repeat

Make sure that you open up the messages pane: messages pane

Edit: If it's important that it's only the returned value, than you could simply keep track of all the strings and add them together and return the composite. The above example modified:

set i to 1
set a to ""
repeat 10 times

    set a to a & i & return

    set i to i + 1
end repeat
return a

Result:
"1
2
3
4
5
6
7
8
9
10
"
1
  • Thanks, this is a good general demo of the principal. Commented Nov 11, 2019 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .