Input Dialog Box Reappears when it is "on idle" in Applescript - applescript

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.

Related

How do I get a dialog input with 2 buttons to auto execute after timeout if no input action?

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.

Adding a progress bar to a dialog for a "do shell script" in AppleScript

I have an app I'm working on written in AppleScript that moves the St. Bernard redirect LaunchDaemon to the current user's directory so that the user can access any website at home, but lets them put it back before they go back to school. The reason for this is that at school, iPrism blocks whatever website the school doesn't want you going to, but the St. Bernard LaunchDaemon blocks the same websites when the students are at home, and I think that it's unnecessary to do that.
Anyway, I want to add a progress bar to a dialog that shows the input of the shell script that removes the app. Anyone know how to do that?
(I also wanted to not have any buttons on the display dialog "Removing..." buttons "", but instead just have the progress bar. Is this possible?)
Here is the code:
display alert "You may have to restart your computer after using this tool!" buttons {"Ok"} default button 1
set question to display dialog "RMR (Remove My Redirect)
Are you unable to go to a website at home because of that annoying St. Bernard Redirect?
If the answer is Yes, then RMR is your solution! Simply Choose Remove to remove it, and Replace to put it back." buttons {"Remove", "Replace", "Erase Evidence"} default button 3
set answer to button returned of question
if answer is equal to "Remove" then do shell script "mv /Library/LaunchDaemons/com.stbernard.rfcd.plist ~/"
if answer is equal to "Replace" then do shell script "mv ~/com.stbernard.rfcd.plist /Library/LaunchDaemons/"
if answer is equal to "Erase Evidence" then set question to display dialog "Are you sure? RMR will be deleted forever." buttons {"Yes", "No"} default button 2
set answer to button returned of question
if answer is equal to "No" then do shell script "echo"
-- Progress bar goes on the below dialog
if answer is equal to "Yes" then ¬
tell application "Terminal"
display dialog "Removing..." buttons ""
do shell script "srm -rf ~/Downloads/RMR.app; history -c; killall Terminal"
delay 20
quit
end tell
Thanks in advance for anyone who can help!
Applescript in MacOS X 10.10 or later has built-in terminology for a progress bar. For applets, it runs a progress indicator in a dialog box. In script editor, it shows in the status bar. Adapt this terminology:
set n to 10
set progress total steps to n
set progress description to "Script Progress"
set progress additional description to "This should be helpful"
repeat with i from 1 to n
delay 1
set progress completed steps to i
end repeat

AppleScript ASObjC Runner Not Displaying Progress Of Shell Script

Ok, so I have the progress bar made, using ASObjC Runner, but it doesn't show the progress of the shell script, and also doesn't hide the progress bar after the shell script completes. Anyone know why?
Here is the code now:
display alert "You may have to restart your computer after using this tool!" buttons {"Ok"} default button 1
set question to display dialog "RMR (Remove My Redirect)
Are you unable to go to a website at home because of that annoying St. Bernard Redirect?
If the answer is Yes, then RMR is your solution! Simply Choose Remove to remove it, and Replace to put it back." buttons {"Remove", "Replace", "Erase Evidence"} default button 3
set answer to button returned of question
if answer is equal to "Remove" then do shell script "mv /Library/LaunchDaemons/com.stbernard.rfcd.plist ~/"
if answer is equal to "Replace" then do shell script "mv ~/com.stbernard.rfcd.plist /Library/LaunchDaemons/"
if answer is equal to "Erase Evidence" then set question to display dialog "Are you sure? RMR will be deleted forever." buttons {"Yes", "No"} default button 2
set answer to button returned of question
if answer is equal to "No" then do shell script "echo"
if answer is equal to "Yes" then ¬
tell application "ASObjC Runner"
reset progress
set properties of progress window to {button title:"Cancel", button visible:true, message:"Removing...", detail:"Please be patient", indeterminate:false, max value:100, current value:0}
activate
show progress
end tell
repeat with i from 1 to 100
do shell script "srm -rf ~/Downloads/RMR.app; history -c; killall Terminal"
tell application "ASObjC Runner"
activate
set properties of progress window to {detail:"Progress: " & i, current value:i}
if button was pressed of progress window then
exit repeat
end if
end tell
end repeat
tell application "ASObjC Runner" to hide progress
Thanks in advance!
Also, here is a link to the app in case you need it: RMR
Two thoughts: 1. There for sure are issues with do shell script being synchronous/asynchronous. Since you hand out the work load, not sure what the pb does with that. 2. Start off by stripping out the guts of your script, leaving just the flow, the dialogs and the if clauses. Replace all the stuff (do shell scripts, etc) with delay statements, in order to first test your pb.
However, you're looking for |answer| being set to "Yes", but it will never be set to "Yes" in your script. That's not an option in the dialog box.??? You need to reduce and test parts of your script bit by bit.
I would also avoid using that helper app, which may be part of your problem. You can do a progress bar without it, as you can see in the answer to your previous question: stackoverflow.com/questions/27517042/…
– jweaks

Force quit on Applescript

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

How to tell an Applescript to stop executing

Applescript's "choose from list" user interaction has a "cancel" button — I want this "cancel" to tell the script to immediately stop executing. In other words:
set wmColor to choose from list {"Black", "Black for all", "White",
"White for all"} with prompt "What color should the watermark be?"
default items "White for all" without multiple selections allowed and
empty selection allowed
if wmColor is false
*tell script to stop executing*
end if
Can't seem to find how to do this — does anyone know how?
Error number -128 is "User Cancelled", and will stop the script - for example:
if wmColor is false then
error number -128
end if
"return" tells a script to stop executing:
display dialog "Do you want to exit this script?" with icon note buttons {"Exit", "Continue"}
if the button returned of the result is "Exit" then
return
end if
For the specific case of an AppleScript invoked via osascript, it can be terminated, returning a status of 0 to the shell, by doing:
tell me to "exit"
Terminate, emitting a message and returning a status of 1, by doing:
tell me to error "{your message}"
The above writes a line like this to standard error:
{scriptname}: execution error: {your message} (-2700)
Here is an example that replaces the cryptic "-2700" and likely does what jefflovejapan is looking for:
tell me to error "Terminated by user" number 0
which writes this to standard error:
{scriptname}: execution error: Terminated by user (0)
and returns a status of 1.
I learned this by experiment, after reading:
AppleScript Language Guide - error Statements
I found this works well for scripts running within an application (for example, InDesign CS5.5):
repeat 100 times
tell application ("System Events") to keystroke "." using command down
delay (random number from 0.5 to 5)
end repeat
This was modified from this answer.
You can use the word "quit" to quit the current application. You can use this if you save your script as an application.
if wmColor is false
quit
end if

Resources