I am trying to get a button in xcode to connect to a server if it is checked and the unmount a volume when it is unchecked. here is my sample code that I have been working with
what am i missing here? and thanks for the help
script Textbox_testAppDelegate
property parent : class "NSObject"
property key07Button : missing value
property textField : missing value
on clickedtheObject_(sender)
set textFieldValue to textField's |stringValue|() as string
display dialog textFieldValue
end clickedtheObject_
on cleartheObject_(sender)
set this_folder to (path to desktop folder)
set textFieldValue to (textField's |stringValue|()) as text
tell application "Finder"
if not (exists folder textFieldValue of this_folder) then
make new folder at this_folder with properties {name:textFieldValue}
end if
end tell
end cleartheObject_
on checkTheButton_(sender)
if inkey07Button 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
end checkTheButton_
I’m not sure what problem you’re having since you didn’t mention one – it helps to state what you are seeing, and any error messages you get.
I also can’t tell when you want the action to take place. Do you want to check the state of the button only when you change the state, or when the program opens (I’m assuming that checkTheButton is the action for your check button)?
Another problem I see, is that you need to check the state of the button, not the button itself, and you need to coerce the result to a boolean or an integer like so:
if inkey07Button’s |state|() as integer is 1 then …
or
if inkey07Button’s |state|() as boolean is true then …
Also, in your properties, you have key07Button and in the method, inkey07Button. Are these 2 different things?
Ric
Sorry I guess i was a little vague…
I have a button with the name of a server and when you check that box I want it to connect to that server. It would be nice if the app could see if you are connected or not and have the state of the checkbox reflect that. I am planning on adding code after I figure this part out that when you uncheck the box it will eject that server.
The problem I am having is that it will not connect to the server when I check the box. It does nothing.
checkTheButton is the action for the checkbox. In IB I gave the button the name key07Button also
I hope that this clears it up a bit.
script Textbox_testAppDelegate
property parent : class "NSObject"
property key07Button : missing value
on checkTheButton_(sender) -- action to take when checkbox is clicked
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
end checkTheButton_
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Are you sure you have both the property and the action method hooked up properly in IB? I cut and pasted your code into my app, and when I checked the box it tried to connect to the server.
You should always try putting in log statements into your method if it’s not working to see where it doesn’t work. This is what I did with your code:
on checkTheButton_(sender) -- action to take when checkbox is clicked
log "got here"
if key07Button's |state|() as boolean is true then
log "got to if"
tell application "Finder"
if not (exists disk "inkey07") then
log "got past if not"
open location "smb://sfreeman@inkey07.myserver.local/inkey07"
end if
end tell
end if
end checkTheButton_
Try adding the log statements and see what you get.
Ric
I guess I did not have it hooked up right in IB. I have it working now. Thanks for the help.
How could I have the button be true if you are already connected to the server when the app starts up?
Sorry for all the questions I am just beginning to learn more.
Thanks,
Scott
I’m not sure what is the best way to tell when you are connected to a particular server. One way might be to use NSFileManager’s method, mountedVolumeURLsIncludingResourceValuesForKeys_options_. It could be used like this to log what volumes are mounted:
set manager to current application's NSFileManager's alloc()'s init()
log manager's mountedVolumeURLsIncludingResourceValuesForKeys_options_(missing value, current application's NSVolumeEnumerationSkipHiddenVolumes)
You would probably have to use this log to see exactly what the URL of your server is and then put that name into an if statement something like this:
set manager to current application's NSFileManager's alloc()'s init()
set volumeArray to manager's mountedVolumeURLsIncludingResourceValuesForKeys_options_(missing value, current application's NSVolumeEnumerationSkipHiddenVolumes)
if volumeArray's containsObject_(current application's NSURL's URLWithString_("<the url of your server>")) as boolean is true then
key07Button's setState_(1)
else
log "The server is not mounted"
end if
Presumably, you could also use the Finder method that you have in your code like the following. I tend not to use the Finder and use all cocoa methods, but this way is certainly shorter.
tell application "Finder"
if (exists disk "inkey07") then
key07Button's setState_(1)
end if
end tell
Ric