Since JavaScript can parse JSON strings natively, we are able to borrow this potential utilizing a snippet of JXA. I’ve additionally elected to name the API utilizing an Goal-C technique, 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 under accommodates one foremost handler, APIResponse
, which takes a labelled for
parameter that ought to be handed both an IP deal with 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 person’s personal IP deal with.
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 accommodates one other handler embedded inside the API
script object, known 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 atmosphere, it might return an Object
populated with key-value entries. This straight “bridges” to an AppleScript file
, containing properties and values.
The general return worth of the script is at present this AppleScript file
that represents the response to an API request. Nevertheless, the traces instantly previous this extract all the person response fields and assigns their values to top-level variables with corresponding names.
Thus, you possibly can 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 deal with : ({} & ip_addr & "")'s string 1
property baseURL : "https://freeipapi.com"
property endpoint : "/api/json/" & IPv4 deal with
property requestURL : baseURL & endpoint
on response:(JSON as string)
run script "JSON.parse(" & quoted kind ¬
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
#・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・#