Guide to using Cocoa in ASOC / Xcode

Hey all.

I’m starting to sprinkle a lot more Cocoa in my ASOC projects, partly because they are easier to diagnose issues with, partly because I want to move away from do shell scripts and partly because there is much more stuff that you can do within Cocoa

The handling of the output from do shell scripts is doubling annoying as you either have to slam the command within a try statement or ensure that you always return something useful, like ;$1 at the end (not good practise).

You may ask why I don’t use Cocoa to begin with… I just don’t get C. I struggled in Uni with it, and have struggled ever since.


What I’m looking for is a guide to help me use the OS X 10.8 doc set more within ASOC.

Shane has helped with my issues with Wi-Fi scanning, which I’ve now explored more with getting the currently attached SSID, as simple as

set currentInterface to CWInterface's interface()
        set networks to currentInterface's scanForNetworksWithName_error_(missing value, missing value)
        log "Current network conn is " & currentInterface's ssid

However, when I look at the doc set and see

- (BOOL)associateToNetwork:(CWNetwork *)network password:(NSString *)password error:(NSError **)error ;

I have honestly no idea where to start.

my logic would suggest the following:

set networks to currentInterface's associateToNetwork(SSIDVar, PasswordVar, missing value)

A guide to this would be useful…


I recommend to buy Shane’s book. It’s worth it.
AppleScriptObjC Explored is a comprehensive guide to AppleScriptObjC and covers also all topics to use ObjC code in an ASOC environment and vice versa.

Quick tutorial to translate ObjC methods to ASOC:
Replace each colon with a underscore and concatenate the parameter labels

The equivalent of

- (BOOL)associateToNetwork:(CWNetwork *)network password:(NSString *)password error:(NSError **)error ;


associateToNetwork_password_error_(network, password, reference)

the last parameter is a pointer to a pointer which is a special case. The handling is described in the book

Thanks for the reply stefan. I Have shane’s book infront of me, which has helped

I was close with my initial guess, but I’m still struggling with the implementation:

set currentInterface to CWInterface's interface()
log currentInterface's associateToNetwork_password_error_(WiFiNetwork, WiFiPassVar, missing value)

sending “Woggle’s iPhone5” as the WiFiNetwork (as text) and “flibble99” as WiFiPassVar

I get…

-[__NSCFString scanRecord]: unrecognized selector sent to instance 0x101d62080

the only thing I can think of is that I’m passing text instead of CWNetwork for the SSID, as per

  • (BOOL)associateToNetwork:(CWNetwork *)network password:(NSString *)password error:(NSError **)error ;

right, the parameter network must be a CWNetwork instance.
The scanForNetworksWithName. method returns a set of CWNetwork objects.
If you specify an name, there is only one object in the set and you can get the network object with the anyObject() method

set currentInterface to CWInterface's interface()
set networks to currentInterface's scanForNetworksWithName_error_("Woggle's iPhone5", missing value)
if networks's |count|() > 0 then
	set myNetwork to networks's anyObject()
	currentInterface's associateToNetwork_password_error_(myNetwork, WiFiPassVar, missing value)
end if

Thanks Stefan.

That makes a lot more sense. I didn’t think to grab the params from the existing network. I was trying to work out how to convert the string to an CWNetwork instead.