Attaching and Detaching a panel

Ok I have the panel opening and closing nicely.


property speedSheet : missing value

on showSpeedSheet_(sender)
		tell class "NSApplication" of current application
			its sharedApplication's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(speedSheet, mainWindow, me, "ednsheets", missing value)
		end tell
end showSpeedSheet_

on endSpeedSheet_(sender)
		speedSheet's orderOut_(me)
		tell class "NSApplication" of current application
			its sharedApplication's endSheet_(speedSheet)
		end tell

--closing code can go here just like the old "on panel ended"!)

end endSpeedSheet_

  1. Drag from your “open” button to the app delegate instance in IB and select showSpeedSheet
  2. Drag from the “OK” button in your panel window to app delegate instance in IB and select endSpeedSheet
  3. Drag from app delegate instance in IB to Panel window and select the “speedSheet” (property in script)

Voila!

Rob

FWIW, this is what I use for sheets:

	on showSheet_(sender) -- triggered by button in window
		set theApp to current application's class "NSApplication"'s sharedApplication()
		theApp's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(alertWindow, mainWindow, me, "didEndSheet:", missing value)
		-- should be able to use "didEndSheet:returnCode:contextInfo:" as selector, but crashes whenever I use more than just "didEndSheet:". Not sure why; it works in Obj-C
	end showSheet_
	
	on closeSheet_(sender) -- triggered by button in sheet
		-- if you need info from sheet, collect it here [eg, sender's title()]
		set theApp to current application's class "NSApplication"'s sharedApplication()
		theApp's endSheet_(alertWindow) -- no point sending returnCode
	end closeSheet_
	
	on didEndSheet_(theSheet) -- must match selector name in beginSheet_ call
		theSheet's orderOut_(me)
	end didEndSheet_

I think that custom sheet panels are ideal in many situations, but what about when you just want to show a simple alert? This looks like a lot of coding to replace the old “display alert” command in ASS… What about this:

  • (IBAction)message2:(id)sender
    {
    // Use a standard alert to display the message
    NSRunAlertPanel(@“Message1”, @“Text”, @“OK”, NULL, NULL);
    }

But right now, I am a bit clueless on how to convert this to ASOC. This is what I can think of so far:

since it is part of the AppKit framework, I guess I have to declare a property to call it?? then at the top:

property appKit : "AppKit" of current application

and then:

appKit's NSRunAlertPanel_("Message1", "Text", "OK", missing value, missing value)

Anyone can confirm is this is correct?

What about plain old

display dialog "Some dialog" buttons {"OK", "Cool!"} with icon 0 -- the red alert triangle

Rob

Yeah, I know this would work. But I was more thinking of a sheet-like alert window attached to the main window… much like a "display alert … attached to window “mainWindow” " was used in ASS. Tried it already, but doesn’t work, obviously…

You have to make the alert and then display it. In theory, this should work:

	on showAlert_(sender)
		set theAlert to current application's class "NSAlert"'s alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_("An alert", "OK", "Cancel", missing value, "Further explanation")
		theAlert's beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(mainWindow, me, "alertDidEnd:returnCode:contextInfo:", missing value)
	end showAlert_

	on alertDidEnd_returnCode_contextInfo_(theAlert, returnCode, contextInfo)
		-- get button pushed
		log (returnCode as integer)
	end alertDidEnd_returnCode_contextInfo_

but in practice it has the same problem with returnCode. So you can use:

	on showAlert2_(sender)
		set theAlert to current application's class "NSAlert"'s alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_("An alert", "OK", "Cancel", missing value, "Further explanation")
		theAlert's beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(mainWindow, me, "alertDidEnd:", missing value)
	end showAlert2_
	
	on alertDidEnd_(theAlert)
		-- get button pushed
		log theAlert
		--log (returnCode as integer)
	end alertDidEnd_

But it’s useless for more than one button, because you can’t tell which button was pressed.

Using the same analogy, can we attach the

"choose folder with prompt"

window to “mainWindow” ?

No, you have to use the Cocoa equivalent, NSOpenPanel.

I looked up NSOpenPanel in XCode documentation and this is what I have so far


property aWindow : missing value

on buttonClick_(sender)
set thePrompt to current application's NSOpenPanel's openPanel()
tell thePrompt
setCanChooseDirectories_(true)
set myParameter to "true_or_false"
beginSheetForDirectory_file_types_modalForWindow_modalDelegate_didEndSelector_contextInfo_(path to downloads folder,missing value, missing value, aWindow, me, "openPromptDidEnd:returnCode:contextInfo:myParameter", missing value)
end tell
end buttonClick_

on openPromptDidEnd_returnCode_contextInfo_(thePrompt, returnCode, contextInfo,myParameter)
 
      --- process myParameter
       log (returnCode as integer)
end openPromptDidEnd_returnCode_contextInfo_


I have a few questions regarding this code:
1: The first parameter according to XCode docs is absoluteDirectoryPath. Is ‘path to downloads folder’ the correct way of specifying a default folder?
2:How do I pass my own parameter ‘myParameter’ to the method ‘openPromptDidEnd_returnCode_contextInfo_’
So that I can process it?

This is one of those methods that can’t be used in ASOC because the return type of contextInfo is a void pointer – something that ASOC can’t handle. It’s a depreciated method anyway, so you shouldn’t use it. Do you have a copy of Shane’s Myriad Helpers? That code has various categories that make it easier, and in some cases possible, to use open, save and choose type panels.

In objective C you would use the context info to pass whatever info you want to your openPromptDidEnd_returnCode_contextInfo_ method.

Ric

XCode docs define the contextInfo parameter as :

So in this call

beginSheetForDirectory_file_types_modalForWindow_modalDelegate_didEndSelector_contextInfo_(path to downloads folder,missing value, missing value, aWindow, me, "openPromptDidEnd:returnCode:contextInfo:", missing value)

say I want to pass a custom value (100 for eg), how would I use the contextInfo parameter to pass it to
openPromptDidEnd_returnCode_contextInfo_ method and retrieve it in that method ?

Any idea?

Did you read my last reply?

  1. You can’t use methods with the contextInfo (void *)contextInfo signature in ASOC, they don’t work!

  2. This is a depreciated method, so you shouldn’t use it (I’m not sure if you can’t use it).

You’ll have to find another way to accomplish what you want.

Ric

Sorry, I misunderstood your reply. I guess I will get shane’s myriad helpers