Buttons setState()

I have checkboxes that I want to be checked if a person is connected to certain servers, and not checked when they are not. I have code working to connect and disconnect but I want the program to check if connected when it starts up and cannot seem to get this working.

It works if I keep everything on the main window, but I have all of these checkboxes on a secondary window.
Code for button:


	on connectKEY07_(sender)
		if key07Button's |state|() as boolean is true then
			tell application "Finder"
				if not (exists disk "inkey07") then
					open location "smb://sfreeman@inkey07.myserver.local/inkey07"
				end if
			end tell
		end if
		if key07Button's |state|() as boolean is false then
			tell application "Finder"
				eject disk "inkey07"
			end tell
		end if
	end connectKEY07_

here is what I have for the start up…


on applicationWillFinishLaunching_(sender)
		-- Insert code here to initialize your application before any files are opened 
		
		tell application "Finder"
			if (exists disk "inkey07") then
				key07Button's setState_(1)
			end if
		end tell
	end applicationWillFinishLaunching_

I am sure there is a better way of doing this than what I have, but alas I am just beginning with applescriptObjC

thanks for the help…

Try moving your “key07Button’s setState_(1)” line outside the Finder tell block.

the code with it inside works when I test it just by itself and on the main window. Just not if I have it on a second window.

I have tried to take it out of the finder tell block and it still seems to not work.

Any Ideas…

What isn’t working? Do you get any error messages? I tried the code below with the checkbox in a second window, and it worked fine:

script CheckBoxTestAppDelegate
	property parent : class "NSObject"
	property key07Button : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		tell application "Finder"
			if (exists disk "MacHD") then
				tell me to log "found it"
				key07Button's setState_(1)
			end if
		end tell
	end applicationWillFinishLaunching_
end script

Ric

I added the log to it and it is not even logging. I do not know if it matters that it is on a “textured” window or not.

I just do not know why it is not finding it in this one app that I created. It will work every time I try this elsewhere. No error messages are popping up.

I tried it on a textured window, and that still works. Try adding the line “log key07Button’s |state|()” as the first line of the applicationWillFinishLaunching method and see what happens.

Ric

I still do not get it to add the log. the only error I get is

here is the way that I have the code:

	on applicationWillFinishLaunching_(sender)
		-- Insert code here to initialize your application before any files are opened 
		
tell application "Finder"
    log key07Button's |state|()
    if (exists disk "inkey07") then 
        tell me to log "found it"
        key07Button's setState_(1)
        end if
    end tell
        		
	end applicationWillFinishLaunching_

Well there could be of course dozens of situations causing this problem but they all point to one kind of error, missing binding of the nib file.

The most common problem is working in the wrong class. Let’s say you connect the button to the appDelegate and try to call the button in another class. Then does the second window has it’s own window controller? do you connect the button with a window controller or a subclass of it or an instance of it (file owner of blue cube). SO it woudl be helpful how your parent chain works and if your in the same object as in the main window.

Try the log statement as the very first line – outside the tell block

Ric

still did not log

Did it not log anything? Try – log “got here” and see if that logs. It seems like your applicationWillFinishLaunching method is not being called.

Ric

is it an issue to have all the code for the button’ set state on a second applescript class?
I have two applescript pages one has the main code and the other has all the stuff for the buttons.

probably should have said this at first, sorry…

Yes, that’s a problem – your second class probably isn’t the application delegate, so applicationWillFinishLaunching, which is a delegate method, won’t be called.

Ric

tried the log “got here” and nothing

so, then I should move all of that to the other applescript page and delete that class?

It’s hard to say without seeing the whole structure of your app. You could move it all to your main script – that should work. If you want to have it in a separate class, then you need a blue cube in your IB file that’s set to that class, and you should put the code inside an “on awakeFromNib()” method which will be called.

Ric

Only if you want to instantiate a class. But to make it not too difficult I’ll go with the ‘blue cube’.

But like I said in my post, you’re having the most common mistakes for new ASOC writers. Copying and pasting code from one class to another will not work because the nib bindings can’t find the object at run time. You’re having a broken binding so delete the broken binding an re-bind the button with the property right class.