Since JavaScript can parse JSON strings natively, we are able to borrow this means utilizing a snippet of JXA. I’ve additionally elected to name the API utilizing an Goal-C methodology, which, on the very least, leads to cleaner-looking code (though I see AskDifferent’s syntax highlighting nonetheless would not cope effectively with apostrophes).
The code beneath incorporates one essential handler, APIResponse
, which takes a labelled for
parameter that must be handed both an IP tackle to carry out a look-up. If handed an empty string or any non-string worth (e.g. lacking worth
, null
, me
) then it sends an API request to the identical endpoint however with no payload, which their documentation signifies that the look-up will then be carried out in opposition to the consumer’s personal IP tackle.
Subsequently, I made a decision to carry out a look-up on theirs, by calling the handler like so:
set response to the APIResponse for "104.21.94.136"
This handler itself incorporates one other handler embedded inside the API
script object, referred to as response:
. It takes a single argument, which would be the JSON string that will get returned by an API request. This JSON string is then inserted right into a JavaScript name to JSON.parse(...)
because the argument. In a JavaScript setting, it might return an Object
populated with key-value entries. This instantly “bridges” to an AppleScript document
, containing properties and values.
The general return worth of the script is presently this AppleScript document
that represents the response to an API request. Nevertheless, the strains instantly previous this extract all the person response fields and assigns their values to top-level variables with corresponding names.
Thus, you may exchange the ultimate line:
return the response
with one the returns the particular fields you are after, e.g.:
return [countryCode, isProxy]
#!/usr/bin/env osascript -s s
#・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・#
use framework "Basis"
use scripting additions
#・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・#
property textual content merchandise delimiters : {}
property nil : a reference to lacking worth
#・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・#
set response to APIResponse for "104.21.94.136"
set [ipVersion, ipAddress, isProxy, latitude, longitude, timeZone, ¬
countryName, countryCode, zipCode, cityName, regionName, ¬
continent, continentCode] to response's [ipVersion, ipAddress, ¬
isProxy, latitude, longitude, timeZone, countryName, countryCode, ¬
zipCode, cityName, regionName, continent, continentCode]
return the response
#・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・#
## HANDLERS:
on APIResponse for ip_addr
script API
property IPv4 tackle : ({} & ip_addr & "")'s string 1
property baseURL : "https://freeipapi.com"
property endpoint : "/api/json/" & IPv4 tackle
property requestURL : baseURL & endpoint
on response:(JSON as string)
run script "JSON.parse(" & quoted type ¬
of JSON & ");" in "JavaScript"
finish response:
finish script
API's response:(my (NSString's stringWithContentsOfURL:(the ¬
URLWithString_(the requestURL of the API) of my NSURL) ¬
encoding:(my NSUTF8StringEncoding) |error|:nil))
finish APIResponse
#・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・#