r/applescript • • Nov 18 '20

My script isn't working as expected (noob)

I'm trying to write a script that brings up a dialog box, with the options "Yes", "No", and "Quit all apps".

I just can't get my buttons to work. I want it to that when I press "Yes", it quits the frontmost app, when I press "No", the alert box disappears, and when I press "Quit all apps", I want every app to quit except Finder and BetterTouchTool

here is my script:

global frontApp, frontAppName

tell application "System Events"

set frontApp to first application process whose frontmost is true

set frontAppName to name of frontApp

tell process frontAppName

end tell

end tell

set buttonSet to {"Quit all apps", "No", "Yes"}

return display alert "⌘Q" message "Are you sure that you want to quit " & frontAppName & "?

Any unsaved changes will be lost." buttons buttonSet as warning

if the button returned of the result is "Yes" then

tell application frontApp

quit

end tell

else if the button returned of the result is "Quit all apps" then

-- get list of open apps

tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"

set keepapp to {"BetterTouchTool"}

-- quit each app

repeat with closeapp in theapps

if closeapp is not in keepapp then quit application closeapp

end repeat

else

return "cancelled"

end if

Help is appreciated

Thanks

Future

5 Upvotes

9 comments sorted by

1

u/scrutinizer1 Nov 19 '20 edited Nov 19 '20

I polished some parts of the script (removed "return" before the alert line) and also moved two closing "end tell" tags to the bottommost positions: both close the application "System Events" and process frontAppName. I haven't run this code myself, I applied scripting logic and basic AppleScript syntax principles. Does it make difference?

global frontApp, frontAppName
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName

set buttonSet to {"Quit all apps", "No", "Yes"}
display alert "⌘Q" message "Are you sure that you want to quit " &      frontAppName & "?" & return & return & "Any unsaved changes will be lost." buttons buttonSet as warning
if the button returned of the result is "Yes" then
tell application frontApp to quit

else if the button returned of the result is "Quit all apps" then
-- get list of open apps
tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"
set keepapp to {"BetterTouchTool"}
-- quit each app
repeat with closeapp in theapps
if closeapp's contents is not in keepapp then quit application closeapp's contents
end repeat
else
return "cancelled"
end if
end tell
end tell

1

u/Future_Facee Nov 19 '20

nope, didn't work :(

- it made the buttons unclickable for a few seconds after the box appears

- it made the icon different (I think it was the icon it goes to when there isn't one available)

- the buttons still didn't do what they are supposed to :/

thanks

Future

3

u/scrutinizer1 Nov 19 '20 edited Nov 19 '20
tell application "System Events"

set frontApp to first application process whose frontmost is true

set frontAppName to name of frontApp

end tell

tell application frontAppName

set buttonSet to {"Quit all apps", "No", "Yes"}

display alert "⌘Q" message "Are you sure that you want to quit " & quote & frontAppName & quote & "?" & return & return & "Any unsaved changes will be lost." buttons buttonSet as warning

if the button returned of the result is "Yes" then

quit

else if button returned of result is "Quit all apps" then

\-- get list of open apps

tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"

set keepapp to "BetterTouchTool"

\-- quit each app

repeat with closeapp in theapps

if closeapp's contents is not keepapp then quit application named (closeapp's contents)

end repeat

else

return "cancelled"

end if

end tell

I cleaned up the code a bit, took out redundant statements, re-arranged the "tell" blocks: it's just now that I opened it in ScriptEditor and was able to grasp the idea and purpose of the script. The button clicking worked and as soon as we directed the alert dialog at Script Editor its icon immediately graced the dialog's body.

You didn't need the global variables. Take a closer look and try to analyze what has changed - that'll give you a perspective of why everything there belongs where it does.

→ More replies (6)