Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
2
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
 
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.

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).
 
Doesn't really matter

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
 
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

If you dont want to help why do you even bother posting in this help thread?
 
If you dont want to help why do you even bother posting in this help thread?

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

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.

----------

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.

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".
 
Last edited:
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.
 
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
Code:
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.

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?
 
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 "display alert "Warning" message "Are you sure?". But i dont know how to connect so that the OK button runs the 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".

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.)
 
Last edited:
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?


Im working in AppleScript, i have tried that code in Xcode and it works, whats wrong with it?
 
Im working in AppleScript, i have tried that code in Xcode and it works, whats wrong with it?

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.
 
We gave you the manual. Now what you're asking is to someone read it for you.

Typical macrumors, is there any forum where people just pick on you?, i didn't see how to do it?

----------

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.


Ahh, yeah they actually aren't in the code. I typed it as sentence.
 
Typical macrumors, is there any forum where people just pick on you?, i didn't see how to do it?

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


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.
 
Last edited:
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_
 
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_

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.
 
Last edited:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.