Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How could I display a dialog attached to a window?

If there is a way to do this, how could I, in Xcode 5, cocoa applescript? Something like:


Display dialog "Hello Red_Menace" attached to MyWindow


I know sheets can do this, but this seems a lot more simple, because I wouldn't have to design a window and link it.

Posted on Oct 16, 2013 1:38 PM

Reply
Question marked as Best reply

Posted on Oct 16, 2013 2:39 PM

I saw what you did there...


You don't necessarily need to create another window for simple alerts, you can use the NSAlert class. I think there is still some weirdness with sheets, so the easiest way to do a sheet would be to use Myriad Helpers, which has a category that adds some methods to the NSAlert class, for example:


tell Cocoa's NSAlert's makeAlert_buttons_text_("Alert", {"OK"}, "Hiya,  Apple_For_The_Win" & return & "This sheet will give up after 10 seconds")   showOver_calling_wait_(mainWindow, {"sheetDismissed:", me}, 10) end tell -- on sheetDismissed_(theButton) -- the sheet has been dismissed   # whatever end sheetDismissed_


The completion handler (sheetDismissed in this case) is called when the sheet is dismissed, and is passed the name of the button (or 'Gave Up').

12 replies
Question marked as Best reply

Oct 16, 2013 2:39 PM in response to Apple_For_The_Win

I saw what you did there...


You don't necessarily need to create another window for simple alerts, you can use the NSAlert class. I think there is still some weirdness with sheets, so the easiest way to do a sheet would be to use Myriad Helpers, which has a category that adds some methods to the NSAlert class, for example:


tell Cocoa's NSAlert's makeAlert_buttons_text_("Alert", {"OK"}, "Hiya,  Apple_For_The_Win" & return & "This sheet will give up after 10 seconds")   showOver_calling_wait_(mainWindow, {"sheetDismissed:", me}, 10) end tell -- on sheetDismissed_(theButton) -- the sheet has been dismissed   # whatever end sheetDismissed_


The completion handler (sheetDismissed in this case) is called when the sheet is dismissed, and is passed the name of the button (or 'Gave Up').

Oct 16, 2013 3:32 PM in response to Apple_For_The_Win

Looks like you snuck an edit in there...


For just a regular alert you can do something like:


tell Cocoa's NSAlert's alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_("This is an alert", "OK", "Cancel", missing value, "Hello Apple_For_The_Win") to set theChoice to its runModal()   if theChoice is Cocoa's NSAlertDefaultReturn then -- OK button     # whatever on OK   else if theChoice is Cocoa's NSAlertAlternateReturn then -- Cancel     # whatever on Cancel   end if


Edit:


I've also used a handler for a simple alert with just an OK button:


on showAlert_withMessage_(alertTitle, alertMessage) -- show a simple alert   tell Cocoa's NSAlert's alloc's init()     setMessageText_(alertTitle)     setInformativeText_(alertMessage)     return runModal()   end tell end showAlert_withMessage_

Oct 16, 2013 3:54 PM in response to red_menace

Oh, alright. It just worked for me and I just realized this is not what I meant. I need something that comes out of the window, not a displayed dialog. An example would be when you quit TextEdit without saving a file:

User uploaded file


How could I have something like that come down from the top of the window and have variable text displayed, without using a sheet?

How could I display a dialog attached to a window?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.