Attaching and Detaching a panel

How does one use ApplescriptObjC from the script to open a panel on a window, containing some items such as status bar and then release the panel later? What are the bindings for the panel?

Do you mean a drawer attached to a window or a separate window?

That would be an NSPanel dropping from the titlebar of the main window

Create a new “Window” not a “Panel” in MainMenu.xib.

Create two properties


property mainWindow : missing value
property panelWindow : missing value

Connect the two properties from your app’s instance in IB to the windows.
Create a button on the panel window and control drag from it to the instance
of the window and select “orderOut:”

Attach the “showPanelWindow_” to a button on your main window.


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

I added a “didEndSelector_” selector to the code but I cannot find a way to get the method called once the panel has closed.

Not much use without that.

I think I would write it in Objective-C.

The purpose of the panel is to display several progress bars and text fields. The application opens the panel and closes it when it has finished the section. I can adapt your code to attach the panel but how does the script dismiss the panel?


myWindow's orderOut_(me)

Hi All,

I was about to ask about a drawer as well as a panel but quickly found that you simply control drag from your “openDrawer” button to the instance of the Drawer (the blue cube called Drawer) and select toggle.

No code required!

Rob D

Hello again

I did find that the panel must be declared invisible in its properties in order to be attached properly to the titlebar of the main window.

The app aborts under debug with the console message:

-[NSApplication beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:]: unable to set argument 5 because the AppleScript value <NSAppleEventDescriptor: ‘msng’> could not be coerced to type :.

I had used “missing value” as the fourth and fifth paramaters as described above.

I have tried the same thing as Whiterock, and i get the same error in the log. If I remove the fifth variable, there is no error in the log, but no window appears. And I know that everything else works, I have checked.

If I leave the fifth variable in place, although I get the error, the window appears, but just once. There is no way to bring the window back, unless I quit the application.

I have looked in the Xcode documentation, and there is a mention of “releasing” the window. Doesn’t Applescript release objects automatically? Or has things changed with ASOC?

I also found in the doc that this method exists:
alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:

Wouldn’t that be easier than to create a window and adding custom elements. I would like to reproduce the “display alert … attached to window” command in applescript. I have tried this:


tell class "NSApplication" of current application
its sharedApplication's alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_("OK", missing value, missing value, "Please quit Illustrator.")
end tell

But no result. No window, no error in the log.

Anyone has successfully used this method?

Model: MacBookPro2,2
Browser: Safari 531.9
Operating System: Mac OS X (10.6)

Try coercing it with “as text” or “as string”

Hi!

Do you mean coercing the missing value to text/string? Something like this?


tell class "NSApplication" of current application
its sharedApplication's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(alertWindow, mainWindow, me, missing value as string, missing value as string)
end tell

If i try this code, it returns this in the log:

*** -[NSApplication beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:]: value passed to argument of type ^v must be either missing value' or reference’; assuming `missing value’.

And there is no change to the opening only once window problem…

Thanks for replying…

Try this.

		tell class "NSApplication" of current application
			its sharedApplication's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(alertWindow, mainWindow, me, "", missing value)
		end tell

Thanks! That worked. It solved the error in the log problem…

But still, the window will only show up once, unless I quit the application. Do I need to release it somehow?

I have looked at

alertWindow’s setReleasedWhenClosed_(true)

and

alertWindow’s close_(me)

but none of them work, in fact they cause errors in the log…

Any ideas?

Model: MacBookPro2,2
Browser: Safari 531.9
Operating System: Mac OS X (10.6)

With the window selected in IB, go to the Attributes Inspector and turn off One Shot.

Well, was already off. Tried the deffered button too, as well as the buffered/retained/nonretained popup, every permutation of the three, no change.

And the Release when closed is checked under behavior, visible at launch is off… quite strange…

Thanks for the tip! I got much to learn about IB it seems…

How are you closing the sheet? Are you using endSheet:?

Nope, using:

myWindow’s orderOut_(me)

as suggested by Craig in this post…

I have found in the Xcode docs the following about “Using Custom Sheets”:

  • (IBAction)closeMyCustomSheet: (id)sender
    {
    [NSApp endSheet:myCustomSheet];
    }

but also this about orderOut:

  • (void)didEndSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
    {
    [sheet orderOut:self];
    }

So maybe I need to put the orderOut in a “on didEndSheet” routine in ASOC? I have put it inside a IBOutlet that is called when the user clicks on “OK” in the panel…

Model: MacBookPro2,2
Browser: Safari 531.9
Operating System: Mac OS X (10.6)

If you do, you’ll need to put its name as the didEndSelector in beginSheet_…

Did anyone ever get the panel to close? I have tried numerous things with no luck.

Sorry! I meant “get the panel to open after closing it once.”

Rob