How to tell an Applescript to stop executing - applescript

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

Related

Applescript: Photoshop: How to find out whether the image is really open

We have an automation tool for Photoshop, using a control app, which calls Applescripts controlling Photoshop.
One situation is that we must open a RAW image in the CameraRAW plug-in, and then open it in Photoshop. This part is handled, via an applet using System Events. When that applet terminates, we run the processing script for Photoshop.
As some pictures take quite a bit of time to open, we have to make sure that the picture is really open before the script can run. … and that's where I am stuck.
At the moment, I am using the following code which is intended to wait until the image is open (the criterion for "open" is correct (and tested manually), so that's not the issue here).
tell application "Adobe Photoshop CC 2015"
activate
tell application "System Events"
tell application process "Photoshop CC"
--
display alert "Waiting for Window"
--
repeat
try
set wn to name of window 1 as text
try
if (wn contains "(RGB/16") then
set wn to "image is open: " & wn
end if
end try
if (wn contains "(RGB/16") then
display alert "We are there, quitting now… " & wn
exit repeat
end if
end try
delay 1
end repeat
end tell
end tell
--
display alert "Ready for process"
--
-- and here comes the processing code
end tell
I also tried to set a variable which is tested as argument for repeat, and changed when the exit condition is fulfilled.
Trying to create even alerts within the repeat loop, does not lead to any effect; the script ends up in an infinite loop.
It is well possible that I miss the obvious… So, I am grateful for any helpful hint.
Thanks in advance.
I think you have a few small issues with your script that are causing your problem.
You are using name of window 1 when I believe you need name of document 1. With your first try block structured as it was you weren't realizing it was actually giving an error on name of window 1
The name that is returned doesn't contain the color space and bit count, so I've changed the result test to an empty string
Notice the modifications to the try block around getting the document name
I don't believe it's necessary or see a reason to use "System Events" in this case, so I've modified the version below without it.
Example Script
tell application "Adobe Photoshop CC 2015"
display alert "Waiting for Window"
repeat
try
set wn to name of document 1 as text
on error
set wn to ""
end try
try
if wn is not equal to "" then
set wn to "image is open: " & wn
end if
end try
if wn is not equal to "" then
display alert "We are there, quitting now… " & wn
exit repeat
end if
delay 1
end repeat
display alert "Ready for process"
end tell

Applescript to check App Store

Thanks for taking the time to read my question.
It's pretty simple, but i am a complete noobie to this, so am having some trouble.
Is it possible to have an applescript that will check the mac app store for updates, and if there are, output the number of updates to someplace?
A good example of this is (if you are aware of it) the geeklets that check for unread mail, and then outputs it to the desktop.
EDIT:
I downloaded a geeklet for the unread mail (as referenced above), and using that as a starting point, I tried to write my own script.
set run_bool to 1
tell application "System Events"
set run_bool to count (every process whose name is "App Store")
end tell
if run_bool is 1 then
tell application "App Store"
set update_count to 0
set output_string to ""
repeat with upd in Apps in Updates
if upd's download is not true then
set update_count to update_count + 1
end if
end repeat
if update_count is 0 then
set output_string to "zero"
else if update_count is 1 then
set output_string to "one"
else
set output_string to "two"
end if
end tell
else
set output_string to "not running"
end if
return output_string
now this is not my final code, but simply to check to see if it will work and what the output would be.
On compilation I get an error saying
error "The variable Updates is not defined." number -2753 from "Updates"
as well as
Syntax Error
Expected end of line but found unknown token
Also, when I stopped compilation, this appeared below the last line in my code
tell application "GeekTool Helper"
activate
«event ascrgsdf»
Any help is appreciated.
#foo is pretty right on with his idea. This code only requires one line. In the second line, I used display notification, but you substitute it with you preferred method to pass on the value.
tell application "System Events" to tell (first application process whose ¬
frontmost is true) to set returnValue to title of ((first menu item whose title ¬
begins with "App Store") of menu "Apple" of menu bar 1)
display notification returnValue
Result:
"App Store…, 1 update"
menu bar items are accessible everywhere (e.g. windowed/numeral desktop mode, fullscreen mode, dock hidden/disabled).
Make sure accessibility is enabled for Script Editor, or whichever app you use to invoke the script, to gain access to the UI.
There is just one weird thing: if I had used begins with "App Store..." instead of begins with "App Store", the code would be a dud. I don't know why - it might has to do with escaped characters and the .... Anyone who knows please enlighten me with a comment.
As for your code, I can tell from AppleScript Dictionary that Updates is not a property of App Store.app. Nor is any other categories in the UI. To get to the Dictionary, open Script Editor and press CMD+SHIFT+O
In addition, if you want to use return statement, you need an explicit handler. In other words, you need to wrap the code between on run and end run.

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

"end if"/"else if" in AppleScript not working?

I am trying to code an AppleScript with a dialog box that has multiple buttons, which each execute different commands. My problem is that AppleScript Editor detects either "end if" or "else if" as a syntax error, as per the title.
For example:
set dialog to display dialog "Test" buttons {"1","2"}
set pressed to button returned of dialog
if pressed is equal to "1" then activate "Safari"
else if pressed is equal to "2" then beep
end if
AppleScript Editor displays the error "Syntax Error: Expected end of line, etc. but found “else if”."
Is there something I'm doing wrong?
AppleScript's conditionals have two formats, a simple single-line format:
if condition then statement
and a multiline format:
if condition then
statement
end if
If you want to use multiple statements or have multiple conditions (else if), you must use the multiline format:
set dialog to display dialog "Test" buttons {"1", "2"}
set pressed to button returned of dialog
if pressed is equal to "1" then
activate "Safari"
else if pressed is equal to "2" then
beep
end if

Input Dialog Box Reappears when it is "on idle" in 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.

Resources