Display dialog after choose from list - applescript

I have been playing with applescript for a while and i am making a text version of pokemon. I have a choose from list and a if statement to start a display dialog. when you press Run it should say "You cannot run from a trainer battle" but the display dialog never opens. I can't see what is wrong. I have been searching google but can't find a solution
here is the code
set userStarter to "undefined"
set starterHP to "undefined"
set starterLV to 5
set starters to {"Charmander", "Bulbasor", "Squirtle", "Pikachu"}
set userName to "undefined"
on battle(traner)
display dialog traner & " wants to battle!" buttons {"Next"}
set battleMenu to {"Attack", "Bag", "Run"}
set temp to {choose from list battleMenu}
if temp is "Run" then
display dialog "You cannot run from a trainer battle" buttons {"Ok"}
end if
end battle
display dialog "Welcome to text based Pokemon!" buttons {"Play", "Quit"}
if the button returned of the result is "Play" then
set temp to display dialog "What is your name?" default answer "Ash" buttons {"Submit"}
set userName to text returned of temp
set userStarter to {choose from list starters}
display dialog "Oak: Okay " & userName & " here is your " & userStarter buttons {"Next"}
display dialog "Gary: Hey, lets battle!" buttons {"Battle"}
battle("Gary")
end if
here is the log
tell application "AppleScript Editor"
display dialog "Welcome to text based Pokemon!" buttons {"Play", "Quit"}
--> {button returned:"Play"}
display dialog "What is your name?" default answer "Ash" buttons {"Submit"}
--> {text returned:"Ash", button returned:"Submit"}
choose from list {"Charmander", "Bulbasor", "Squirtle", "Pikachu"}
--> {"Pikachu"}
display dialog "Oak: Okay Ash here is your Pikachu" buttons {"Next"}
--> {button returned:"Next"}
display dialog "Gary: Hey, lets battle!" buttons {"Battle"}
--> {button returned:"Battle"}
display dialog "Gary wants to battle!" buttons {"Next"}
--> {button returned:"Next"}
choose from list {"Attack", "Bag", "Run"}
--> {"Run"}
end tell

Okay, I found the problem.
This line is wrong, remove the { }: (otherwise you are pushing the return of the choose from list into a list, the return is already a list)
set temp to {choose from list battleMenu}
Then choose from lists returns a list so you have to check so:
if temp is {"Run"} then
display dialog "You cannot run from a trainer battle" buttons {"Ok"}
end if
(But if you want to show up the dialog if you press bag you should replace "Run" with "Bag")
The complete code is then:
set battleMenu to {"Attack", "Bag", "Run"}
set temp to choose from list battleMenu
if temp is {"Run"} then
display dialog "You cannot run from a trainer battle" buttons {"Ok"}
end if

Related

Is there a way to take away certain words from returned text?

noob coder here,
I am trying to create a program that would remove certain words from a user returned sentence.
Heres what I got so far:
set returnedSentence to display dialog "Welcome to SimpleScript Early Release (Beta)! To continue please enter your desired code/text." default answer "" buttons {"Go!", "Cancel"} default button 1
display dialog returnedSentance - "if" - "and" - "then" - "a" buttons {"ok", "Cancel"} default button 2
For example: If the user would enter the sentence: If I am hungry, then i would go to a restaurant.
Then the code should return I am hungry, i would go to restaurant.
Thanks in advance.
Plain AppleScript doesn't have regular expressions, but you can use text item delimiters to replace the desired text items with nothing/blanks. For example:
set returnedSentence to text returned of (display dialog "Welcome to SimpleScript Early Release (Beta)! To continue please enter your desired code/text." default answer "" buttons {"Go!", "Cancel"} default button 1)
set trimList to {"if ", "and ", "then ", "a "} -- the text items to replace (note the trailing space to denote words)
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, trimList} -- stash original delimiters (normally blank) and set new ones
set trimmedItems to text items of returnedSentence -- break the string apart
set AppleScript's text item delimiters to tempTID -- restore delimiters (normally blank)
set trimmedSentence to trimmedItems as text -- put the string back together
display dialog trimmedSentence buttons {"ok", "Cancel"} default button 2
Use the regex library of your language to extract the part that does not contain de forbidden word.

Applescript user interaction

I am looking for a piece of applescript where the user is asked to select a specific frame in InDesign and then copy its contents into another document.
I use this to create a new structured document within a script.
I have made a script where i ask the user to select a frame with
display dialog "Selecteer kop" buttons {"OK", "Nee", "Cancel"} default button "OK"
if button returned of result is "OK" then
if class of item 1 of selection is in {rectangle, oval, polygon} then
Problem is when i run this part i can't select in InDesign a frame and then go on with my script.
In the chain of actions, don't ask user to select a page item after having displayed the dialog but have the user select the page item and then check the context prior to display the dialog.

My VBScript is running in system account I need a Confirmation Message Box With Yes/no option to popup and be visible to user

My VBScript is running in system account I need a Confirmation Message Box With Yes/no option to popup and be visible to user. I am able to display the message box using this command
ObjShell.run "cmd.exe /C "" MSG * /SERVER:" &IP& " " &Str_Message &""&""""
but I need yes and no confirmation box and fetch user Choice there.
Here is the documentation on the MsgBox function.
Here's an example of that code. These constants are built into vbscript, so you don't need to declare them. The vbSystemModal value will cause the message box to be "modal" - as in, it will appear on top of all other windows until it is dismissed.
userInput = MsgBox("Prompt",vbExclamation+vbYesNo+vbSystemModal,"Title")
Select Case userInput
Case vbYes
MsgBox "User pressed yes."
Case vbNo
MsgBox "User pressed no."
End Select
Note that you might want to be careful with an unexpected popup to end users, especially as it relates to which button is default and if the dialog is modal. If a user is typing and they hit spacebar when the box pops up, they might select the default button without reading the message first. You might consider the vbDefaultButton1 or vbDefaultButton2 values to assign a default button.

When I run the program, It says the variable result is not defined. This is in Applescript

set var to "Which Application would you like to open?"
display dialog var buttons {"Cancel", "Safari", "Calculator"}
if button returned of result is "Calculator" then
display dialog "Are you sure?" buttons {"No", "Yes"} default button 2
if button returned of result is "Yes" then
tell application "Mail" to activate
end if
else
display dialog "Are you sure?" buttons {"No", "Yes"} default button 2
if button returned of result is "Yes" then
tell application "Safari" to activate
end if
if button returned of result is "Cancel" then
return
end if
end if
result only refers to the line above it. You need to store it as another variable if you wanna use it later.
set TheResult to button returned of result
You should probably try and format your code in this forum a little better to assist people trying to help you. You don't need the last "Cancel" option and you also are asking the user if they want to launch "Calculator" but then you launch "Mail". I've made some modifications to your code and reposted it.
set var to "Which Application would you like to open?"
display dialog var buttons {"Cancel", "Safari", "Calculator"}
if button returned of result is "Calculator" then
display dialog "Are you sure?" buttons {"No", "Yes"} default button 2
if button returned of result is "Yes" then
tell application "Calculator" to activate
end if
else
display dialog "Are you sure?" buttons {"No", "Yes"} default button 2
if button returned of result is "Yes" then
tell application "Safari" to activate
end if
end if

Prevent Email From Being Sent via Applescript

I've got the following AppleScript (below). I'm attempting to confirm the sending of an email. This AppleScript is already successfully hooked up to an "outbox rule" (using Mail Act-On) in Mail.app, and I've verified that it runs when it is supposed to (at the time of sending).
The ultimate goal is to pop a dialog to the user, asking if they "really" want to send the email. If not, stop the email from being sent.
The current script attempts to delete the message, but that doesn't work. Any ideas?
using terms from application "Mail"
on perform mail action with messages messageList for rule theRule
repeat with thisMessage in messageList
set theResult to display dialog "Send?" buttons {"OK", "Cancel"} default button 2
if button returned of theResult is not equal to "OK" then
delete thisMessage
end if
end repeat
end perform mail action with messages
end using terms from
I think that a "Cancel" button in a display dialog will immediately end execution of your script which means that the delete thisMessage line never is run.
You might try changing that to something like:
set theResult to display dialog "Send?" buttons {"OK", "No, Delete Message"} default button 2
if button returned of theResult is not equal to "OK" then
...

Resources