Applescript Progress Bar Freezes at end and stays active while next routine is called - macos

I am making an Applescript that automates my local development workflow. Whats happening is I'm using an Applescript progress bar to run while I check for script dependencies (i.e. MAMP). The progress bar appears, animates perfectly, and is returning my routines that check for dependencies as expected. The problem is, even when the progress bar reaches its total steps count, the progress bar freezes, even when the script calls a dialog box in the following routine, and the script freezes.
Here is the routine that starts the progress bar:
on dependency_check()
set progress description to "Access Granted: "
set progress additional description to "Preparing..."
delay 0.5
set progress total steps to 100
repeat with i from 0 to 100
try
if i = 0 then
set progress additional description to "Checking Dependencies..."
delay 0.5
else if (i > 0 and i < 10) then
set progress additional description to "Checking Node.js..."
else if i = 10 then
set progress additional description to node_exists()
else if (i > 10 and i ≤ 20) then
set progress additional description to "Node.js is installed..."
else if (i > 20 and i < 30) then
set progress additional description to "Checking mamp-cli..."
else if i = 30 then
set progress additional description to mampcli_exists()
else if (i > 30 and i ≤ 40) then
set progress additional description to "mamp-cli is installed..."
else if (i > 40 and i < 50) then
set progress additional description to "Checking MAMP..."
else if i = 50 then
set progress additional description to mamp_exists()
else if (i > 50 and i ≤ 60) then
set progress additional description to "MAMP is installed..."
else if (i > 60 and i < 70) then
set progress additional description to "Checking Google Chrome..."
else if i = 70 then
set progress additional description to chrome_exists()
else if (i > 70 and i ≤ 80) then
set progress additional description to "Google Chrome is installed..."
else if (i > 80 and i < 100) then
set progress additional description to "All Dependencies Found..."
else if i = 100 then
set progress additional description to "Complete..."
set progress completed steps to i
end if
set progress completed steps to i
delay 0.025
on error errTxt number errNum -- errTxt and errNum are returned from system
display dialog errTxt & return & errNum
exit repeat
end try
end repeat
end dependency_check
It should be noted that it is calling other routines at i = 10, 30, 50, 70.
They all return text if the dependency is found.
Here is an example of one of those routines:
on node_exists()
set nodeExists to do shell script "test -e /usr/local/bin/node"
if nodeExists is not "" then -- Verifies Node.js is installed by 'test' command
beep
tell application "Finder" to display dialog ¬
"Node.js is not installed, and is needed to run this program. Download Node.js and try again." with title ¬
"Required Library Missing: Node.js!" with icon caution ¬
buttons {"Download Node.js", "Quit Program"} ¬
default button 2 cancel button 2
if button returned of result is "Download Node.js" then
open location "https://nodejs.org/en/"
script_exit()
end if
else
return "Node.js is installed..."
end if
end node_exists
Thanks for any help in advance. I'm completely stuck!

Related

AppleScript - How do I detect if a specific button is clicked on GUI and terminate the script?

I have a script that manipulates a GUI. At some point in the process, a progress indicator appears in the GUI for a few mins. If the user clicks the GUI button button "Stop" of sheet 1 (so not a dialog button), I'd like the script to display dialog followed by error number -128. How do I do this? Here's what I tried...
repeat while progress indicator 1 of sheet 1 exists
try
set button_returned to button returned of button "Stop" of sheet 1
if button_returned is "Stop" then
display dialog "Operation cancelled"
error number -128
end if
end try
end repeat
Note: I use repeat while progress indicator 1 of sheet 1 exists to pause the script whilst the progress indicator is up.
Save this example as usual app to see the correct workaround to solve your problem:
global itemCount
set processName to name of current application
set theList to {"Marlow", "Maddie", "Sammy", "Stuey", "Jessie", "Tolstoy", "Marlow", "Maddie", "Sammy", "Stuey"}
set itemCount to count of theList
set progress total steps to count of theList
repeat with i from 1 to itemCount
set thisItem to item i of theList
set progress description to "Item " & i & " of " & itemCount
set progress additional description to thisItem
-- The delay is simply to simulate processing time
-- so you can see the progress bar in action.
-- Exclude this from your code and put your real do stuff.
delay 1
set progress completed steps to i
end repeat
on quit
if progress completed steps < itemCount then
display dialog "Operation cancelled"
end if
end quit

Pressing cancel in choose from list will cause a error

When I execute this script, everything works fine, but then when I press the cancel button, the script just give me a error, "Can’t get item 1 of false"
set mailList to {"Hide Applications", "Quit Applications", "Full Volume"}
set mailType to choose from list mailList
if item 1 of mailType is "Hide Applications" then
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
set the collapsed of windows to true
end tell
else if item 1 of mailType is "Full Volume" then
set volume output volume 100
else if item 1 of mailType is "Quit Applications" then
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
tell application this_process
quit
end tell
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
end if
You add a check if mailType is a boolean which would happen if you press cancel. The check would be inserted between line 2 and 3 and would look something like this:
...
if class of mailType = boolean then return
...
choose from list returns boolean false when the Cancel button is pressed.
Just abort the script immediately in this case
set mailType to choose from list mailList
if mailType is false then return

How to check internet connection applescript

noob here trying to teach myself applescript and generally not very smart so apologies :|
i was looking at this question and the answers
Check for active internet connection with Applescript/Automator
and i wanted to make a applescript that runs constantly in the background and that puts a red dot in the menubar when i don't have an internet connection and a green one when i don't (using the AnyBar application) but i can't get it to work properly.
can someone please tell me what i am doing wrong. thank you so much!
repeat
repeat with i from 1 to 2
try
do shell script "ping -o -t 2 www.google.com"
exit repeat
tell application "AnyBar" to set image name to "green"
on error
tell application "AnyBar" to set image name to "orange"
delay 2
if i = 2 then tell application "AnyBar" to set image name to "red"
end try
end repeat
delay 60
end repeat
I'd recommend to use an applet (script saved as application) with an idle handler rather than an infinite repeat loop
property imageName : "red"
property delayValue : 60
property googleURL : "http://www.google.com"
on run
set imageName to "red"
end run
on idle
if (count (get ((googleURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0 then
set imageName to "green"
else
if imageName is "green" then
set imageName to "orange"
set delayValue to 2
else if imageName is "orange" then
set imageName to "red"
set delayValue to 60
end if
end if
tell application "AnyBar" to set image name to imageName
return delayValue
end idle

The variable result is not defined AppleScript

I am working on the same app that I mentioned in my first question. I have gotten much farther, but when I try to play "2048" the first time, AppleScript give me the error:
"The variable result is not defined"
I will skip the main bulky body of the app and get to the area with the problem:
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
else
display dialog "Invalid response" with title "Invalid response" buttons {"Go back", "Quit"} default button 1
end if
if the button returned of the result is "Go back" then
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
else
return
end if
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
end if
Just set the result of the display dialog to a variable and use the variable. It's tricky to use "result" as you have in many if statements. One of them is bound to cause a problem because "result" will change at some point.
So do something like this right after your display dialog statement...
set {buttonReturned, textReturned} to {button returned of result, text returned of result}
Then in your if statements use buttonReturned or textReturned.

AppleScript less than number or greater than number

I am having an error in AppleScript when I use less than or greater than operators consecutively. I probably didn't explain that very well, so I will post the code.
**set good to false**
**repeat until good = true**
set oneTen to the text returned of (display dialog "Pick a number from 1 through 10" default answer "" buttons {"OK"} default button 1) as number
if oneTen is less than 0 then
display dialog "below" buttons {""} default button 1
else if oneTen is greater than 10 then
display dialog "above" buttons {""} default button 1
else
set good to true
end if
**end repeat**
I am trying to take the input from the prompt, and keep the user from entering anything below 0 or above 10. Could you post some code to do this well?
I want something similar to this.
**set oneTen to the text returned of (display dialog "Pick a number from 1 through 10" default answer "" buttons {"OK"} default button 1) as number**
**if oneTen is less than 0 or greater than 10 then**
**-- make them do the prompt again**
**end if**
Try:
repeat
set oneTen to the text returned of (display dialog "Pick a number from 1 through 10" default answer "" buttons {"OK"} default button 1) as number
if oneTen is less than 0 then
display dialog "below" buttons {""} default button 1
else if oneTen is greater than 10 then
display dialog "above" buttons {""} default button 1
else
exit repeat
end if
end repeat

Resources