display dialog "Your Computer Has Been Taken Over By Ninja Cows With Flamethrowers!" buttons {"Ok......"} default button 1
display dialog "How Would You Like To Proceed?" buttons {"Milk Them", "Burn Them", "Do Nothing"} default button 1
if the button returned of the result is "Milk Them" then
beep 1
delay 1
display dialog "You milk them day and night but unlucky you spilt the milk on to the computer, the computer crashes and the batery shoots out, you failed" buttons {"Wait, What?"} default button 1
tell application "Finder"
shut down
end tell
else
if the button returned of the result is "Do Nothing" then
display dialog "The cows look at you, they are just staring eager to destroy somthing, after 23 hours and 59 minutes they burn the place to the ground, rupturing the lithium ion battery.
your computer explodes, you explode and everything explodes." buttons {"Wait, What?"} default button 1
tell application "Finder"
shut down
end tell
end if
if the button returned of the result is "Burn Them" then
display dialog "You grab a even bigger flamethrower from your bottemless bag and burn them to the ground YOU DID IT!, unfortunataly apples new chipset in not designed to withstad such heat, the computer explodes, you explode, everything explodes. Hey, atleast you got the cows this time.." buttons {"Wait, What?"} default button 1
tell application "Finder"
shut down
end tell
end if
end if
end
The reason is that the internal variable result contains always the result of the preceding line.
Use this:
set buttonReturned to button returned of (display dialog "How Would You Like To Proceed?" buttons {"Milk Them", "Burn Them", "Do Nothing"} default button 1)
if buttonReturned is "Milk Them" then
-- proceeed button 1
else if buttonReturned is "Do Nothing" then
-- proceeed button 2
else
-- proceeed button 3
end if
Related
I load an automator.app on my MacBook on startup that connects my MacBook to my local server. I created it with a dialog box that allows me to 'connect' or 'cancel' this script. This has been working, but at times I wish it would automatically input "connect" after 60 seconds.
I'm not really versed in AppleScript and have build most of my automator (app) routines I use from information I find on the internet and modify until it works. The following code works, sort of. It redraws the dialog box ever 5 seconds and I just want the numerics (wait_time variable) updated without a complete redraw of the dialog box. If I can do that I can have it update every 1 second. Next and more important, when I select the default button "Connect Now" nothing happens. The "Don't Connect" button seems to work fine. After this part of the script runs, I connect to specific folder on my local server and all of that works fine.
set theDialogText1 to "You will be connected to the Local Server in "
set wait_time to "60"
set theDialogText2 to " seconds. If your are not on the Local network; select [Don't Connect]."
repeat wait_time times
display dialog theDialogText1 & wait_time & theDialogText2 buttons {"Don't Connect", "Connect Now"} default button "Connect Now" cancel button "Don't Connect" giving up after 5
set wait_time to wait_time - 5
end repeat
I would like this to function like the 'shut down' dialog works in macOS.
Display what is happening, offer a button to run the action sooner, offer a button to cancel the function, and run the function automatically in 60 seconds if no input is received.
Applescript only offers simple dialog, then you can't display a count down of remaining seconds with simple Applescript command. There is a progression bar display, but that display has no button.
The easiest is to use a standard dialog box with instruction "giving up after x" which closes the dialog after x seconds if the user does not react before. You just have to check if the dialog has been closed with the gave up boolean = true or check which button has been used to close it. The script bellow handles the 3 cases (waiting time 3 seconds for test)
set wait_time to 3
set feedback to display dialog "test time out" buttons {"Don't connect", "Connect"} giving up after wait_time
if gave up of feedback then
display alert "no user action after " & wait_time & " seconds."
else if button returned of feedback is "Connect" then
display alert "User clicks on Connect"
else
display alert "User clicks on don't connect"
end if
You can also put this in an handler (= sub routine) and set wait_time to 3 seconds and call it several times. The script becomes a bit more complex, this is why I added few comments :
set Refresh_time to 3 -- the delay between 2 refresh of the dialog
set wait_time to 21 --total delay for user to decide
set Decision to 0
repeat until (Decision > 0) or (wait_time = 0)
set Decision to Get_User_Feedback(wait_time, Refresh_time)
set wait_time to wait_time - Refresh_time
end repeat
display alert "Decision is " & Decision
-- 0 means no answer
-- 1 means Connect
-- 2 means don't connect
-- your program here
on Get_User_Feedback(Remain_time, Step_delay)
set feedback to display dialog "Do you want to connect. Time left is " & Remain_time & " Sec." buttons {"Don't connect", "Connect"} giving up after Step_delay
if gave up of feedback then
return 0
else if button returned of feedback is "Connect" then
return 1
else
return 2
end if
end Get_User_Feedback
the Get_User_Feedback is called several times in the repeat loop. It displays the dialog for 3 seconds, then close and recall again showing the new remain time. After total wait_time, or if user uses a button before, the repeat loop stops.
display dialog "Play Nillys Realm?" buttons {"Yes please", "No"} default button 1
if result = {button returned: "Yes please"} then
display dialog "Long time loading, u think?" buttons {"Yes", "No"}
end if
if result = {button returned:"No"} then
tell application "Google Chrome"
repeat 5 times
open location "http://test.nillysrealm.com"
activate
end repeat
if result = {button returned:"Yes"} then
tell application "Google Chrome"
repeat 10 times
open location "http://test.nillysrealm.com"
activate
end repeat
thats my code and it is a MESS. Can anyone help me fix? It will never load
It is not easy to help in your script, because reading it, I do not understand what you want to do. However you are not using the 'Result' variable correctly. This variable always contains the result of the last dialog, so when you have 2 dialog box, any test of that variable will always give you the result of the second dialog, never the first one.
The solution is to specifically assign the result to a separate variable, like in example bellow :
set Dialog1 to display dialog "do you agree ?" buttons {"Yes please", "No"} default button 1
if button returned of Dialog1 is "Yes please" then
-- do something here when user click "yes please"
else
-- do somthing here when user click "No"
end if
I'm making a text game but can't get it to end the script half way through when the user pushes quit. I have tried quit, return and error -128 but none of them work.
The code isn't inside a tell/end tell because I'm not wanting to use an application, but I've tried putting it inside one with finder and that didn't work either..
Any help is appreciated :)
set temp to display dialog "Welcome" buttons {"Play", "Quit"}
if temp = "Quit" then
error number -128
end if
set temp to display dialog "What is your name?" default answer "Joe" buttons {"Submit"}
set userName to text returned of temp
etc. etc.
Another solution: define the cancel button.
display dialog "Welcome" buttons {"Play", "Quit"} cancel button "Quit"
-- no need to check the button returned, the script quit automatically when user cancelled
set temp to display dialog "What is your name?" default answer "Joe" buttons {"Submit"}
set userName to text returned of temp
etc. etc.
You should get button returned:
if button returned of temp = "Quit" then
error number -128
end if
I am using a script that I wrote to automatically write my dynamic IP to a .txt file but my problem is that I cannot get the dialog to close when the quit button is clicked.
set yn to (display dialog "Your ip has been written to the server, the application will re-run in 10 minutes if you DO NOT close this window." buttons {"Quit", "Run again"} giving up after 600)
if yn is equal to "Quit" then
quit
end if
What I ended up doing was
display alert "This is an alert" buttons {"No", "Yes"}
if button returned of result = "No" then
display alert "No was clicked"
else
if button returned of result = "Yes" then
display alert "Yes was clicked"
end if
end if
You can replace the lines "display alert "No/Yes was clicked"" with whatever code you want to run
The simplest way to figure out how to make use of yn's button pressed is to look at yn:
set yn to (display dialog "Your ip has been written to the server, the application will re-run in 10 minutes if you DO NOT close this window." buttons {"Quit", "Run again"} giving up after 600)
return yn
You'll see that yn returns {button returned:"Quit", gave up:false}. This indicates that yn has a property button returned that you can use in your if statement.
Another way to figure this out is to look through the AppleScript dictionary (File > Open Dictionary...) that documents display dialog, which is the StandardAdditions dictionary.
To add as an answer as the original question had a giving up after and I needed to do something in my script if the dialog did exceed past the timeout period. Here is an additional option that considers the timeout:
set dialogTitle to "Star Wars Question"
set theDialog to display alert "Do you think Darh Maul should have his own movie?" buttons {"YES", "NO"} default button "YES" giving up after 10
if button returned of theDialog = "" then
display notification "No decision was made, cancelled dialog" with title dialogTitle
else if button returned of theDialog = "YES" then
display notification "I concur" with title dialogTitle
else if button returned of theDialog = "NO" then
display notification "I find your lack of faith disturbing" with title dialogTitle
end if
First, some notes:
Even used without assigning a variable, dialogs return a result "record" object containing two key-value pairs: button returned and gave up.
To get the selected button's text (other than Cancel), use button returned of result.
When using giving up after n, use either button returned of result (with text value of "" (empty string)) or gave up of result (with boolean value of true or false).
Clicking the "Cancel" button causes an error (number -128 or "User canceled."). To handle the Cancel button, use a try block.
In my first example, I'm providing a Cancel button in multiple places. This demonstrates that the on error code will work regardless of which dialog in the try block returns the Cancel button. If you don't need a Cancel, then just provide a single button such as buttons "OK".
I'm using parens around some text in my examples, but AppleScript does not require these. AppleScript does require quotes around text.
Tested in both Monterey and High Sierra, and using Mar10Josh's answer, with fixes, as the basis for my examples to make it simple and clear...
try
display dialog ("An example dialog.") buttons {"Cancel", "OK"} giving up after 5
if button returned of result = "OK" then
display dialog ("You clicked OK.") buttons {"OK", "Cancel"}
else if gave up of result then
display dialog ("You let the dialog expire.") buttons {"OK", "Cancel"}
end if
-- handle the Cancel button here
on error "User canceled." -- or you can say: on error number -128
display dialog ("You clicked Cancel.") buttons "OK"
end try
Same thing, stated slightly differently to show some other stuff (text without parens, giving up after option, generic error handling):
try
-- uncomment following line to see non-Cancel-related error result ;)
-- error "foobar!"
display dialog "An example dialog." buttons {"OK", "Cancel"} giving up after 5
if button returned of result = "OK" then
display dialog "You clicked OK." buttons "OK"
else if gave up of result then
display dialog "You let the dialog expire." buttons "OK"
end if
-- handle any error that might occur here;
-- errText and errNum are variable names I chose for these values
on error errText number errNum
if errText is "User canceled." then -- or you can say: if errNum is -128
display dialog "You clicked Cancel." buttons "OK"
else
display dialog "Error: " & errText & return & return & "Number: " & errNum buttons "OK"
end if
end try
display dialog ("An example dialog.") buttons {"Cancel", "OK"}
if button returned of result = "Button" then
display dialog ("You clicked Button.")
end
I have this functionality in my Applescript wherein a input dialog box is shown to the user to enter some text. And this dialog box code is inside "on idle" "end idle" which repeats after every 3 seconds.
The issue is when this dialog box is shown and the user doesn't enter any details and leave the dialog box open, than after a minute or so this dialog box still remains but another dialog box appears (the same one repeats). How should I handle this issue inside "on idle" anyone?
Breakup of the code is shown below for reference.
on idle
try
tell application "iTunes"
repeat
set loginbutton to display dialog "Enter your facebook log in name to start using XXX." default answer loginusername with title "XXX Log In" buttons {"Quit", "OK"} default button 2
display dialog "loginbutton = " . loginbutton
end repeat
end tell
end try
return 3
end idle
In regular AppleScript, when you put up a dialog the script will wait until the dialog is dismissed before continuing. I am not able to get the symptoms you are describing, although your example snippet is incomplete and a bit buggy - you are in a repeat (forever) loop with no means of escape, since you are also trapping all errors.
The idle handler isn't really the place for things like this - this handler is called when your application is, well, idle, so whatever code is in it will run every time the script isn't doing anything.
if you are just wanting a dialog that repeats until a correct answer is returned, you can use something like the following in your main run handler
repeat -- forever
display dialog "this is a test, so enter something with \"test\"" default answer "test"
set theAnswer to text returned of the result
if theAnswer contains "test" then exit repeat -- success
end repeat
log theAnswer
Note that although a dialog's cancel button generates a "user cancelled" error, in a stay open script the script won't quit on the error, so you will need to do your own error handling.