Multithreading?

Friends, I’m not even sure “multithreading” is the right word, but I was looking at p34 in Shane’s book and wondering if I could adapt that procedure with the progress bar to other outlets.

Specifically, this is what I’m trying to do.

I have some code, part of which looks like this:

set myTemp to do shell script "mktemp -t txt"
            do shell script "curl -s http://checkip.dyndns.org &> " & myTemp & " &2> /dev/null"
            delay 7
            set extIP to do shell script "sed 's/[a-zA-Z/<> :]//g' " & myTemp

            if extIP = "" then
                set my theNetwork to "Still trying."
else if extIP contains "=" then
                set my theNetwork to "Can't get IP"
            else
                 set my theNetwork to extIP
                            end if


# note: if you delete the two 'my' words in the last 4 lines, this should compile and run in native AS Editor

What its meant to do is let CURL go get the external IP in the background while returning control to the user. But that is defeated by my use of delay in Line 3.

The property 'theNetwork is connected to a UI Text Field. The idea is it should be displaying the ‘Still trying.’ message until the app checks the myTemp file and finds the IP address sitting prettily waiting to be fetched. So what I want to do is find a way to tell the app to go check back on that myTemp file to see if CURL has returned the result yet, but in the meantime let the user have control.

Any thoughts on how I can achieve this?

Hi,

this is the Cocoa way to load URL data asynchronously

property theData : missing value
property theNetwork : ""

on applicationWillFinishLaunching_(aNotification)
	set my theNetwork to "still trying."
	set theData to current application's NSMutableData's |data|()
	set dyndnsURL to current application's |NSURL|'s URLWithString_("http://checkip.dyndns.org")
	set theRequest to current application's NSURLRequest's requestWithURL_(dyndnsURL)
	current application's NSURLConnection's connectionWithRequest_delegate_(theRequest, me)
end applicationWillFinishLaunching_

on connection_didReceiveResponse_(connection, response)
	theData's setLength_(0)
end connection_didReceiveResponse_

on connection_didReceiveData_(connection, receivedData)
	theData's appendData_(receivedData)
end connection_didReceiveData_

on connection_didFailWithError_(connection, anError)
	set my theNetwork to anError's localizedErrorDescription()
end connection_didFailWithError_

on connectionDidFinishLoading_(connection)
	set htmlDocument to current application's NSXMLDocument's alloc()'s initWithData_options_error_(theData, current application's NSXMLDocumentTidyHTML, missing value)
	set ipString to htmlDocument's rootElement()'s stringValue()
	set components to ipString's componentsSeparatedByString_("IP Address: ")
	if components's |count|() = 2 then
		set my theNetwork to components's objectAtIndex_(1)
	else
		set my theNetwork to "Can't get IP"
	end if
end connectionDidFinishLoading_

Thanks, Stefan. Your help is much appreciated!

:slight_smile: