Capture Result of Input - Display Dialog - applescript

How to store the result of input in a variable ?
For exemplo:
display dialog "Insert Number" default answer "1" buttons{"Save"} default button 1
-- Result: {button returned:"OK", text returned:"1"}
How to capture the result and declare as the value of a variable?
set number to text returned as string
^ It doesn't work.
Help ? =P

set userResponse to text returned of (display dialog "Insert Number" default answer "1" buttons {"Save"} default button 1)

Related

Syntax error - Expected variable name or property but found class name

I get the syntax error below with this script. It fails in the line beginning repeat with cell in rows. How can I get the script to compile?
Expected variable name or property but found class name.
-- Declare variables
property albumDescription : ""
-- Display input box to get album description
display dialog "Enter the album description:" default answer ""
set albumDescription to text returned of result
-- Open Excel
tell application "Microsoft Excel"
activate
end tell
-- Set variables for the active sheet and range of cells
tell application "Microsoft Excel"
set sh to active sheet
set rng to range of sh
end tell
-- Loop through each cell in the range, starting from row 2
repeat with cell in rows 2 thru (count of rows of rng) of rng
-- Check if the cell value is not empty
if value of cell is not "" then
-- Update the album description for the row
set value of cell to albumDescription
end if
end repeat
Actually there are three issues:
All terminology which belongs to a specific application (in this case rows, the content of rng and value) must be inside a tell application block.
You cannot use the reserved word cell as a variable name. Well, you can, but you have to wrap the word in pipes (|cell|). However I recommend to use a different name like aCell
range in set rng to... must be used range
-- Declare variables
property albumDescription : ""
-- Display input box to get album description
display dialog "Enter the album description:" default answer ""
set albumDescription to text returned of result
-- Open Excel
activate application "Microsoft Excel"
-- Set variables for the active sheet and range of cells
tell application "Microsoft Excel"
set sh to active sheet
set rng to used range of sh
-- Loop through each cell in the range, starting from row 2
repeat with aCell in rows 2 thru (count of rows of rng) of rng
-- Check if the cell value is not empty
if value of aCell is not "" then
-- Update the album description for the row
set value of aCell to albumDescription
end if
end repeat
end tell

How do I fix an undefined variable in my program?

I am a beginner when it comes to AppleScript. I'm trying to develop a program that can generate an actually random number. When I set the values of the numbers that will be used to generate the random number, the editor gives me an error:
The variable numberOne is not defined.
I know what this means, but I don't know why it isn't defined. Can someone help with this?
I've tried seeing if only strings would work instead of text, but that doesn't seem to do it. There could be a really easy fix to this, but as I said, I'm a beginner.
on variableValue()
set numberOne to text returned of (display dialog "Variable One:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
set numberTwo to text returned of (display dialog "Variable Two:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
set numberThree to text returned of (display dialog "Variable Three:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
set numberFour to text returned of (display dialog "Variable Four:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
set numberFive to text returned of (display dialog "Variable Five:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
set useSameValues to button returned of (display alert "Use same values?" message "If you re-generate a new number, do you want to use the same values?" as critical buttons {"Cancel", "No", "Yes"} default button 3)
if useSameValues = "No" then
set useSameValuesTwo to "false"
else if useSameValues = "Yes" then
set useSameValuesTwo to "true"
end if
end variableValue
variableValue()
on randomNumber()
-- The line of code just beneath this text is where the error shows up (This is my first program that utilizes handlers, so something could be wrong there).
set numberSix to (numberOne + numberTwo + numberThree + numberFour + numberFive)
set numberSeven to (numberSix * (random number from 1 to (random number from 2 to 100)))
set possibleValueOne to (random number from 1 to 5)
if possibleValueOne = 1 then
set numberEight to (numberSeven - numberOne)
else if possibleValueOne = 2 then
set numberEight to (numberSeven - numberTwo)
else if possibleValueOne = 3 then
set numberEight to (numberSeven - numberThree)
else if possibleValueOne = 4 then
set numberEight to (numberSeven - numberFour)
else if possibleValueOne = 5 then
set numberEight to (numberSeven - numberFive)
end if
set numberNine to (numberEight - 100000)
end randomNumber
randomNumber()
In AppleScript (like most languages), variables have a scope, which is where a declared identifier is recognized within any given script object. Variables declared inside a handler are local to that handler and don't exist outside of it, so you will need to provide some means to make them available elsewhere. There are a few ways to do this, such as declaring global variables or properties, or having a handler return values to the caller.
In your example, the variables declared in your variableValue() handler are not available outside the handler, so it would probably be the easiest to just declare those variables as global, for example by adding the following declaration at the beginning of your script:
global numberOne, numberTwo, numberThree, numberFour, numberFIve

Accept a name in Text Box, Print it when someone click on Print Button

I have a TextBox in VB and a Command Button. I want to print the string upon clicking on command button.
I am using the following code, please tell what I am doing wrong:-
Dim name As String
name = Val(Text1.Text)
MsgBox ("Welcome" & Str(name))
When I input a string in Textbox and click on command button, result is:
Welcome 0
Leave out the val() around your Text1.Text, val() returns the numbers up to the first symbol it can't recognize as a number used in a String. See the documentation. I guess you used a 0 in your string in the TextField or no number at all, both would return 0.
Additionally, it is unnecessary to cast your String name to a String since it is already a String so you can also leave out the Str().
The val function returns the numeric representation of its argument, otherwise it returns "0". It's a bit hard these days to find the official VB6 documentation, but you may want to check: https://en.wikibooks.org/wiki/Visual_Basic/VB6_Command_Reference#Val
So, in your example, if you enter any number in the Text1 textbox control, you will see it in your message box. If you enter any text, you will get "Welcome 0", as you do now. Therefore, you have to remove the val function from your code, like:
Dim name As String
name = Text1.Text
MsgBox ("Welcome " & name)
maybe even simplifying it to:
MsgBox("Welcome " & Text1.Text)
So you declared a string varaible namewhich you want to fill with the text from the Text1box. So you need to spare the val(...)part.
Second, as namealready represents a string, leave out the strin the message box:
name = Text1.Text
MsgBox ("Welcome " & name)

How to change variables in applescript with if command

I have tried this forever, what am I doing wrong?
you get my variable repeatvar by
set repeatvar to the text returned of (display dialog "set repeatvar")
if repeatvar is greater than "1000" then
set repeatvar to "1"
end if
when i enter any number it always just sets that number to 1, regardless of the number. What am I doing wrong?
If you want to perform numeric comparison with strings – which behave different as integers – you have to tell AppleScript explicitly to consider numeric strings
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
considering numeric strings
if repeatvar is greater than "1000" then
set repeatvar to "1"
end if
end considering
The syntax of your first line is not correct. with this syntax, you will never get "text returned" valid value, but only valid "button returned".
to have text returned, you must tell the script that you expect user to input a value. this is done by adding words "default answer"":
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
The second issue with your text is that you want to compare the text entered with the string "1000". Comparing strings and numbers does not always give correct result. For instance strings are using alphabetical order and "9" is greater than "1000", because "9" > "1".
Script bellow will work :
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
try
set myNumber to repeatvar as integer
on error
set myNumber to 0
end try
if myNumber is greater than 1000 then
set repeatvar to "1"
end if

Getting first few characters of a string with applescript

I wish to keep only the first 13 caracters of a string in an applescript. I think I didn't understand how to use the "characters" function.
Can someone help me ?
choose from list {"0041325677667-pharmacie 1", "0041325677557-pharmacie 2",
"0041325677447-pharmacie 3", "0041325677337-pharmacie 4"} with prompt
"Thanks to select" without multiple selections allowed and empty selection allowed
return the result as string
return characters 1 thru 13 of result
Thanks in advance for your help
characters x thru y returns a list of characters, in your example item 1 returns
{"0", "0", "4" ,"1" ,"3" ,"2" ,"5" ,"6" ,"7" ,"7" ,"6" ,"6", "7"}
text x thru y returns a string as requested.
without multiple selections allowed and empty selection allowed is the default so it can be omitted. But you should consider the case when the user presses "Cancel", then the expression return boolean false.
set chosen to choose from list {"0041325677667-pharmacie 1", "0041325677557-pharmacie 2", "0041325677447-pharmacie 3", "0041325677337-pharmacie 4"} with prompt "Thanks to select"
if chosen is false then return "" -- in case of 'Cancel' return empty string
return text 1 thru 13 of (item 1 of chosen) -- as chosen returns a list by default it must be flattened

Resources