Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created March 22, 2023 23:32
Show Gist options
  • Save SKaplanOfficial/3f09e9cbe4274716965030683386276c to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/3f09e9cbe4274716965030683386276c to your computer and use it in GitHub Desktop.
AppleScriptObjC script to get the HTML from a URL using NSURLSession
use framework "Foundation"
set theResult to ""
on getURLHTML(theURL)
global theResult
set theURL to current application's NSURL's URLWithString:theURL
set theSessionConfiguration to current application's NSURLSessionConfiguration's defaultSessionConfiguration()
set theSession to current application's NSURLSession's sessionWithConfiguration:(theSessionConfiguration) delegate:(me) delegateQueue:(missing value)
set theRequest to current application's NSURLRequest's requestWithURL:theURL
set theTask to theSession's dataTaskWithRequest:theRequest
theTask's resume()
set completedState to current application's NSURLSessionTaskStateCompleted
set canceledState to current application's NSURLSessionTaskStateCanceling
repeat while theTask's state() is not completedState and theTask's state() is not canceledState
delay 0.1
end repeat
return theResult
end getURLHTML
on URLSession:tmpSession dataTask:tmpTask didReceiveData:tmpData
global theResult
set theText to (current application's NSString's alloc()'s initWithData:tmpData encoding:(current application's NSASCIIStringEncoding)) as string
set theResult to theResult & theText
end URLSession:dataTask:didReceiveData:
return getURLHTML("https://google.com")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment