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
Related
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
What I'm trying to make is this:
set retry to true
repeat while retry is true
display dialog "In the next window you'll have to choose a color!" buttons {"Cancel", "COLORR!"} cancel button 1 default button 2
set Chosencolor1 to (choose color)
display dialog "The chosen color is: " & Chosencolor1 & " Is this right?" buttons {"NO", "YES"}
if the button returned of the result is "no" then
set retry to true
display dialog "Retry is now: " & retry
else
set retry to false
display dialog "Retry is now: " & retry
end if
end repeat
end
But when I run this code it will return a color code in numbers. But what I want is that it will return a color name like: light blue, blue, green etc.
The only way I know is to do something like:
if the chosencolor1 is "000" then
set the chosencolor1 to "Black"
end if
end
is there any other, more simple way to do this? Or is this just not possible?
Thanks for reading,
Jort
From the AppleScript Language Guide for choose color:
Result The selected color, represented as a list of three integers
from 0 to 65535 corresponding to the red, green, and blue components
of a color; for example, {0, 65535, 0} represents green.
As you have surmised, the only way to get a textual response is to program it yourself using the values of the list that your user chooses.
Here is a very simple example that adds all the values of the chosen RGB integers, and determines if it closer to black or white:
set myColor to choose color
display dialog ("You have chosen: " & my fetchColorName(myColor))
to fetchColorName(clst)
set totalColorValue to ((item 1 of clst) + (item 2 of clst) + (item 3 of clst))
set blackWhiteMidpoint to (196605 / 2)
if totalColorValue < blackWhiteMidpoint then
set colorName to "A color closer to black than white."
else
set colorName to "A color closer to white than black."
end if
return colorName
end fetchColorName
I'm running an applescript program where I am asking for the user to type in their name. If the user clicks "OK", then the name gets stored to the variable I want. However, if the user clicks "Cancel", then the program just quits. How do I set this up to either hide the "Cancel" button, so it's not an option to click, or to set up a loop so that if cancel is clicked, it just continues to ask the user for his/her name until it's entered?
Thanks in advance.
display dialog "Please Enter Your Name." default answer " " with title "Enter Name"
simple solution
display dialog "Please Enter Your Name." default answer " " with title "Enter Name" buttons {"OK"} default button 1
Here's what I got:
display dialog "Please Enter Your Name." default answer "" buttons {"Submit"} with title "Enter Name" default button 1
if the button returned of the result is "Submit" then
set yourVariable to text returned of the result
end if
The way that you hide the cancel button is like this:
display dialog "Please Enter Your Name." default answer " " buttons {"ok"} with title "Enter Name"
set a to the text returned of the result
But if you wanted the repeat then use this (I have made it so they have to enter something too instead of just pressing ok to end it:
repeat
display dialog "Please Enter Your Name." default answer "" buttons {"ok", "cancel"} default button 1 with title "Enter Name"
set a to the text returned of the result
if the button returned of a is "ok" then
if the text returned of a ≠ "" then
exit repeat
end if
end if
end repeat
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.
No matter what my password is, it always says my password isn't right! Help please. Here is the script
set my_password to display dialog ¬
"Please enter your password:" with title ¬
"Password" with icon stop ¬
default answer ¬
"" buttons {"Cancel", "OK"} default button 2 ¬
with hidden answer
if (text returned of my_password) is no then
display dialog "Running the application!" buttons ["OK"] default button 1
else if (text returned of my_password) is not no then
display dialog "Did not enter correct password!" buttons ["OK"] default button 1
end if
You must put the password (no) in quotes.
I also shortened the script. Try it out:
display dialog "Please enter your password:" with title "Password" with icon stop default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
if (text returned of result) is "no" then
display dialog "Running the application!" buttons ["OK"] default button 1
else
display dialog "Did not enter correct password!" buttons ["OK"] default button 1
end if