How do i make a warning that you have to click to run a button?

Discussion in 'Mac Programming' started by Intelligent, May 11, 2014.

  1. Intelligent macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #1
    How do i do so that when i click a button, an alert message comes up "Are you sure you want to x"

    Yes No
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
  3. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #3

    Not really a helpful answer, i knew that already.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Did you also look at the sample code associated with NSAlert?

    Here's the NSAlert online class reference page:
    https://developer.apple.com/library...lasses/NSAlert_Class/Reference/Reference.html

    Several samples are linked under "Related sample code".


    If you've looked at the NSAlert class, and studied its sample code, then please restate a more specific question.
     
  5. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #5
    Yes i have, the specific question is.

    Let's say i have a button, and when i click it i get an alert box. I need to click "OK" for the script to run ( I have an applescript on the button).
     
  6. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #6
    Do you want to be pointed to the right direction or someone to hand you the code?
     
  7. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #7
    Doesn't really matter
     
  8. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #8
    If it doesn't really matter, I've pointed you to the right direction and Chown33 went even further providing you the class reference page so you don't even need to google it!

    But I won't do your work for you and hand you the code, sorry
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Did you study the sample code?
     
  10. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #10
    If you dont want to help why do you even bother posting in this help thread?
     
  11. DeltaMac macrumors 604

    DeltaMac

    Joined:
    Jul 30, 2003
    Location:
    Delaware
    #11
    As you don't seem to be accepting of the help that's offered - why did you even bother asking your question?

    Just my worthless opinion - Much of the value in any programming language is wasted - if you choose not to learn how to program.

    Rather than have someone more experienced than you throw you a bone (that may or may not be useful to you) - how about showing what code you have tried so far - and is not working for you. I'm sure that someone will offer a way to correct it... or, link you to references for you to "learn" something.
     
  12. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #12
    You have been helped by several people in this thread. You've now been nothing but rude and disrespectful to those very people that helped you.
     
  13. Intelligent, May 12, 2014
    Last edited: May 12, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #13
    Why did i ask the question? Because i wanted to know the answer? I dont see how to know what to type because it only explains what commands do, and I'm trying to learn it. I have tried this so far
    on mybuttonhandler127_(sender)
    "display alert "Warning" message "Are you sure?".
    end

    But i dont know how to connect so that the OK button runs the other script script.

    ----------

    I dont see how anyone has helped me, i already knew what NSAlert was and someone said that i should look that up, i said that i already knew that, then I'm disrespectful? Its like asking, "how do i upgrade to Mavericks?" And then someone answers "Download it from the App Store, and figure the rest out yourself".
     
  14. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #14
    The display alert command (which is a little different from an NSAlert, so it is important to explain these things) returns a record with the button returned, so if it is "OK" (or whatever), you can just run the other script like you normally would.
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    When asking a question, it's important to specify the language you're using.

    You didn't say "in AppleScript", so people reasonably assumed Objective-C.

    I'm not entirely sure you're asking for AppleScript, because the posted code isn't syntactically correct for AppleScript.

    So let me ask this:
    Exactly what language are you working in?
     
  16. DeltaMac, May 12, 2014
    Last edited: May 12, 2014

    DeltaMac macrumors 604

    DeltaMac

    Joined:
    Jul 30, 2003
    Location:
    Delaware
    #16
    You say that you know about all available resources (and, I'm guessing, any relevant examples for code), yet you want to respond to a question with another (actually, the same) question, without saying anything about what you have already tried. Is there a chance that you will post those few lines of code that don't work for you?
    (This is a programming forum, after all. Someone will likely help you get your code to work, or at least give you some ideas on how to proceed.)
     
  17. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #17

    Im working in AppleScript, i have tried that code in Xcode and it works, whats wrong with it?
     
  18. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #18
    This is what you posted:
    Code:
    on mybuttonhandler127_(sender)
     "display alert "Warning" message "Are you sure?".
    end
    
    As posted, you have an unmatched " before the display alert, and a period ('.') at the end of that line.
     
  19. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #19
    We gave you the manual. Now what you're asking is to someone read it for you.
     
  20. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #20
    Typical macrumors, is there any forum where people just pick on you?, i didn't see how to do it?

    ----------


    Ahh, yeah they actually aren't in the code. I typed it as sentence.
     
  21. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #21
    Consider yourself lucky that this isn't StackOverflow, or your topic would have been closed or down voted into oblivion already.

    No one can see your computer or read your mind to know what you are doing (well, I hope not, anyway) so you need to explain it in excruciating detail. No one knows what kind of programing experience you have either, so you also need to explain that.

    So here we are, a few days and a dozen or so replies later, and now there are enough bread crumbs to know that you are using AppleScript in Xcode (I can understand some reluctance to mention the AppleScript part, since Objective-C programmers usually run away screaming when you do that). Most regular AppleScript commands such as display alert work the same in an AppleScriptObjC project, so in your action handler you just need to get the button that was pressed in the usual way, and if it is the button you are looking for, then run your other script or handler in the usual way. Using an NSAlert works more or less the same, you will just be using various class methods to manually create and display the alert instead of having everything done for you by the display alert command.

    If you don't know how to do either of those, again, you will need to explain that, or exactly what isn't working.
     
  22. Intelligent, May 14, 2014
    Last edited: May 14, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #22

    I dont know the code for it, thats what is not working.

    I have a button programmed to perform an applescript, for example
    Code:
    
    onmybuttonhandler_(sender)
    tell application "Finder" restart
    end
    
    Thats just an example, but when i click the button the action is performed instantly and i want a way to add a warning that you have to press OK to confirm or something similar.
    And i know the display alert command here.

    Code:
    display alert "Warning" Message "Are you sure?"
    
    That gives me an alert message with "OK and "Cancel"
    But i want to to so that when i click the OK button, this runs

    Code:
    tell application "Finder" restart
    end
    
    This is as good as i could explain.

    The actual code isn't the finder restart but it was an example, i could remove onmybuttonhandler_(sender) because what it does it running an applescript on a button, but as it looks right now i can't because it wouldn't run the script.
    EDIT: Actually it just gives me an option to press ok.
     
  23. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #23
    The solution is to just group your various statements together:

    Code:
    on myButtonHandler_(sender)
    	display alert "Warning" message "Are you sure?" buttons {"Yes", "No"}
    	if button returned of the result is "Yes" then
    		tell application "Finder" to restart
    		# whatever
    	end if
    end myButtonHandler_
     
  24. Intelligent, May 14, 2014
    Last edited: May 14, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #24
    Thank you! :) Thats really helpful! I wonder what the whatever is supposed to be replaced with? Is it just if i wanted to add some code or something. It worked, but it doesn't work when i replace "tell application "Finder" to restart" with "do shell script "rm /path/to/file". Any idea why? Im using both things, so atleast one of the codes worked.
     
  25. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #25
    Yes, the "whatever" is whatever other stuff you want to put in there, although if you are just dropping in random scripts you found on some website, you should understand what they are doing. For the most part regular AppleScript statements work in an Xcode project, but there are a few gotchas, so you would need to post something more specific to tell.
     

Share This Page