12 Replies Latest reply: Oct 16, 2013 5:03 PM by red_menace
Apple_For_The_Win Level 1 Level 1

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.

Solved by red_menace on Oct 16, 2013 5:03 PM Solved

The alert sheet example in my first reply is modal.  There is another method in the category that doesn't have a timeout - it would just be:

 

showOver_calling_(mainWindow, {"sheetDismissed:", me})


Reply by red_menace on Oct 16, 2013 2:39 PM Helpful

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

Reply by red_menace on Oct 16, 2013 3:52 PM Helpful

Oops, sorry about that.  Cocoa is a shortcut I use for current application.  You can define a property like:

 

  property Cocoa : current application

 

or just replace "Cocoa" with "current application".

 

Edit:

I'm pulling most of these examples from my Windows and Panels example project - it can be downloaded here.

All replies

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

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

  • Apple_For_The_Win Level 1 Level 1

    Is there a way to do it without using Myriad Helpers?

    ...And without sheets?

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    As far as I know there is still a problem with AppleScriptObjC using sheets, so some kind of Objective-C protocol or category will be needed.  Using Myriad Helpers is pretty easy, and you can just just use what you need - for the alert sheet, just add the NSAlert+MyriadHelpers .h and .m files to your project and you are good to go.

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    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_
    


  • Apple_For_The_Win Level 1 Level 1

    It says the "Cocoa" isn't defined. How should I declare it in my Applescript.delegate and what do I link it to?

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    Oops, sorry about that.  Cocoa is a shortcut I use for current application.  You can define a property like:

     

      property Cocoa : current application

     

    or just replace "Cocoa" with "current application".

     

    Edit:

    I'm pulling most of these examples from my Windows and Panels example project - it can be downloaded here.

  • Apple_For_The_Win Level 1 Level 1

    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:

    Screen Shot 2013-10-16 at 6.52.31 PM.png

     

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

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    Well, that is a sheet, so you are wanting to fake a sheet without using a sheet?  For a sheet with anything other than a simple alert you are looking at a custom panel or window (which AppleScript's display dialog is, after all).

  • Apple_For_The_Win Level 1 Level 1

    Actually if I could use a popup like the one in TextEdit, that says "Hello" and an "OK" button or something, that would be the ideal.

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    Well, the easiest way would be to use an alert as a sheet - is there a particular reason you don't want to use the Myriad Helper category?

  • Apple_For_The_Win Level 1 Level 1

    It has to be connected to the window to block the user from interacting from anything else in the window until they acknowledge the notification. I don't want to use the helper because it's exactly the same as display dialog. If I were to just use Display dialog users could ignore the window or it might not pop up correctly.

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    The alert sheet example in my first reply is modal.  There is another method in the category that doesn't have a timeout - it would just be:

     

    showOver_calling_(mainWindow, {"sheetDismissed:", me})