So I’m late to the game and reading @ShaneStanley’s book and @alldritt’s posts, amongst other posts here and I am trying to understand why NSURL is quoted and explicitly called by current application's class
, when passing a URL to NSJSONSerialization, e.g.,…
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set TheURLStr to "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo"
set JSONData to getUrlSource(TheURLStr)
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property NSData : a reference to current application's NSData
property NSArray : a reference to current application's NSArray
set theJSONDictionary to NSJSONSerialization's JSONObjectWithData:JSONData options:0 |error|:(missing value)
set timeSeries to theJSONDictionary's objectForKey:"Time Series (Daily)"
set prices to {} -- list of records for the prices, _not_ sorted
set theDates to timeSeries's allKeys() as list -- list of all dates in timeSeries
repeat with d in theDates
set t to (timeSeries's objectForKey:d)
set p to (t's objectForKey:"4. close")
set end of prices to {date:d as string, price:p as string}
end repeat
-- prices is now a list of records containing the daily closing prices
on getUrlSource(urlStr)
set theURL to current application's class "NSURL"'s URLWithString:urlStr
set theData to current application's NSData's dataWithContentsOfURL:theURL
return theData
end getUrlSource
IIUC, NSData dataWithContentsOfURL
requires an NSURL so I get why the previous line has to create an NSURL. I am trying understand why it’s written as current application’s class “NSURL”'s in this quoted form.