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
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
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.
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.
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.
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.
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.
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.