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 & theURLQueryStringthe 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