VBS | Case - Selection issue - vbscript

I realize this is silly but I've spent too much time trying to figure this out. I just need this block to loop until either "Red", "red", "Blue", or "blue" is typed; (I changed the script to colors to simplify readability).
If there's a better way to do this please elaborate.
mbb=MsgBox ("Hit No" ,4, "Start script")
If mbb=7 Then mbt=MsgBox ("Do you like Red or Blue?" ,32, "Choose color")
If mbt=1 Then SOM
Sub SOM
Do
ibb=InputBox ("Please type: Red or Blue")
Select Case ibb
Case "Red"
mbt=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "red"
mbu=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "Blue"
mbv=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case "blue"
mbw=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case Else
MsgBox "Please Type: Red or Blue"
End Select
Loop Until mbt OR mbu OR mbv OR mbw=1
End Sub
If mbt Or mbu=1 Then mbx=MsgBox ("Rouge" ,4, "Rouge")
If mbv Or mbw=1 Then mby=MsgBox ("Bleu" ,4, "Bleu")

You can list multiple spellings in your Case statements:
Dim sColor
sColor = "red"
Select Case sColor
Case "Red", "red", "RED"
MsgBox "Color is Red"
Case "Blue", "blue", "BLUE"
MsgBox "Color is Blue"
Case Else
MsgBox "Color not found"
End Select
This will not handle "REd", "ReD", "reD", "rED" and "rEd" values so a more bullet-proof approach is to convert the case using LCase or UCase functions as Geert Bellekens suggests:
Select Case UCase(sColor)
Case "RED"
MsgBox "Color is Red"
Case "BLUE"
MsgBox "Color is Blue"
Case Else
MsgBox "Color not found"
End Select
The StrComp function could be another option to make the comparison case-insensitive but it is not as easy to read as a Select Case block:
If StrComp(sColor, "RED", vbTextCompare) = 0 Then MsgBox "Color is Red"
If StrComp(sColor, "BLUE", vbTextCompare) = 0 Then MsgBox "Color is Blue"

I've managed to find an answer; I realized it would be easier to avoid using the case function for this part of my code. The new format is as follows:
'simplified
Option Explicit
Dim a, x, y, w, z
Do
a=InputBox ("Please select hot or cold" ,0, "Type: ""Hot"" or ""Cold"" ")
If a="hot" Then x=MsgBox ("Hot selected" ,64, "Test")
If a="cold" Then y=MsgBox ("Cold Selected" ,64, "Test")
If a="Hot" Then z=MsgBox ("Hot selected" ,64, "Test")
If a="Cold" Then w=MsgBox ("Cold selected" ,64, "Test")
Loop Until x Or y Or z Or w=1

Related

how to handle condtion in VBScript Loop

I've two Input Fields and Two Submit Button on a page.
InputField1 "Code"
InputField2 "Amount"
Button1 "Save"
Button2 "Save And Add Another"
Having trouble writing the logic. i should only hit save when i've only one item item and move on. if mutltiple then i need to hit button2 and follow the process and at the end i need to hit save. Any help will be highly apreciated. I'm just not sure how do i handle the save button.
sCode="100:500"
sAmount="500:500"
arrCCode=Split(sCode,":")
arrAmount=Split(sAmount,":")
For i = 0 To UBound(arrCCode)
If i > UBound(arrCCode) Then
Exit For
Else
MsgBox arrCCod(i)
MsgBox arrAmount(i)
End If
Next
I think i got it,,,
sCode="100:500"
sAmount="500:500"
arrCCode=Split(sCode,":")
arrAmount=Split(sAmount,":")
For i = 0 To UBound(arrCCode)
'If i > UBound(arrCCode) Then
'Exit For
'Else
MsgBox arrCCod(i)
MsgBox arrAmount(i)
If i = UBound(arrCCod) Then
msgbox "Save"
Exit For
Else
Msgbox "Save and add"
End
'End If
Next

Choose from list?

Can someone tell me why this won't work? I can't for the life of me figure out why it doesn't. It compiles fine, looks fine, and this method has worked everywhere else for me... Really need some help, anyone know what's wrong?
set areatype to (choose from list {"Triagles", "Trapeziums"} with prompt "What would you like to calculate the area of?" with title "Area Calculator")
if areatype contains "Triangles" then
set height to text returned of (display dialog "What is the height of the triangle?" with title "Area Calculator" default answer "")
set base to text returned of (display dialog "What is the base of the triangle?" with title "Area Calculator" default answer "")
set area to (height * base) / 2
display dialog "Area = " & area & " units squared" with title "Area Calculator" buttons {"Cancel", "Go again"}
else
if areatype contains "Trapezium" then
set base1 to text returned of (display dialog "What is one base of the trapezium?" with title "Area Calculator" default answer "")
set base2 to text returned of (display dialog "What is the other base of the trapezium?" with title "Area Calculator" default answer "")
set area to ((a + b) / 2) * h
display dialog "Area = " & area & " units squared" with title "Area Calculator" buttons {"Cancel", "Go again"}
end if
end if
-Thanks
Look at your very first line in the code. You spelled Triangles incorrectly and you needed the "s" in Trapeziums added to the "else" line in the code.
on goAgain()
set areatype to (choose from list {"Triangles", "Trapeziums"} with prompt "What would you like to calculate the area of?" with title "Area Calculator")
if areatype contains "Triangles" then
set height to text returned of (display dialog "What is the height of the triangle?" with title "Area Calculator" default answer "")
set base to text returned of (display dialog "What is the base of the triangle?" with title "Area Calculator" default answer "")
set area to (height * base) / 2
display dialog "Area = " & area & " units squared" with title "Area Calculator" buttons {"Cancel", "Go again"}
if the button returned of the result is "Go again" then
goAgain()
else
return
end if
else
if areatype contains "Trapeziums" then
set base1 to text returned of (display dialog "What is one base of the trapezium?" with title "Area Calculator" default answer "")
set base2 to text returned of (display dialog "What is the other base of the trapezium?" with title "Area Calculator" default answer "")
set height2 to text returned of (display dialog "What is the height of the trapezium?" with title "Area Calculator" default answer "")
set area to ((base1 + base2) / 2) * height2
display dialog "Area = " & area & " units squared" with title "Area Calculator" buttons {"Cancel", "Go again"}
if the button returned of the result is "Go again" then
goAgain()
else
return
end if
end if
end if
end goAgain
goAgain()

Applescript: Choose color, color return

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

AppleScript: Change Font of Every Selected Character Randomly

For a project for children I would need a script, which allows me to select a text (let's say in word) of which every character is changed to a random font out of a pool of fonts.
Would appreciated help very much - to be honest I do not have a clue where to start.
Thank you!
Here's where to start. Apple includes on everyone's computer a bunch of pre-made applescripts. One of them is called "Crazy Message Text" and when run it asks you to type in a short message. When complete it opens a new email and types that message into the email in a variety of fonts/colors/sizes. So that would be a good place to start because it shows how to do much of what you need. Here's the script but you can find in on your computer in the /Library/Scripts/ folder. You should look through those scripts. There's a lot that can be learned in that folder.
property font_list : {"American Typewriter", "American Typewriter Light", "American Typewriter Bold", "American Typewriter Condensed", "American Typewriter Condensed Light", "American Typewriter Condensed Bold", "Arial", "Arial Italic", "Arial Bold", "Arial Bold Italic", "Arial Black", "Baskerville", "Baskerville Italic", "Baskerville SemiBold", "Baskerville Bold", "Baskerville SemiBold Italic", "Baskerville Bold Italic", "Big Caslon Medium", "Comic Sans MS", "Comic Sans MS Bold", "Copperplate", "Copperplate Light", "Copperplate Bold", "Didot", "Didot Italic", "Didot Bold", "Futura Medium", "Futura Medium Italic", "Futura Condensed Medium", "Futura Condensed ExtraBold", "Geneva", "Gill Sans", "Gill Sans Italic", "Gill Sans Light", "Gill Sans Light Italic", "Gill Sans Bold", "Gill Sans Bold Italic", "Herculanum", "Lucida Grande", "Lucida Grande Bold", "Marker Felt Thin", "Marker Felt Wide", "Optima Regular", "Optima Italic", "Optima Bold", "Optima Bold Italic", "Optima ExtraBlack", "Papyrus", "Verdana", "Verdana Italic", "Verdana Bold", "Verdana Bold Italic", "Zapfino"}
property low_color : 0
property high_color : 65535
property low_fontsize : 36
property minimum_size : 9
property high_fontsize : 72
property this_text : "Happy Birthday!"
activate
repeat
display dialog "Enter the text:" & return & return & ¬
"Minimum Character Size: " & (low_fontsize as Unicode text) & return & ¬
"Maximum Character Size: " & (high_fontsize as Unicode text) default answer this_text buttons {"Cancel", "Set Prefs", "Continue"} default button 3
copy the result as list to {user_text, button_pressed}
if the button_pressed is "Set Prefs" then
repeat
try
display dialog "Enter the minimum character size to use:" & return & return & ¬
"(Must be at least " & (minimum_size as Unicode text) & ")" default answer low_fontsize
set the user_size to text returned of the result as integer
if the user_size is greater than or equal to the minimum_size then
set low_fontsize to the user_size
exit repeat
end if
on error
beep
end try
end repeat
repeat
try
display dialog "Enter the maximum character size to use:" & return & return & ¬
"(Must be greater than " & (low_fontsize as Unicode text) & ")" default answer high_fontsize
set the user_size to text returned of the result as integer
if the user_size is greater than the low_fontsize then
set high_fontsize to the user_size
exit repeat
end if
on error
beep
end try
end repeat
else
if user_text is not "" then
set this_text to the user_text
exit repeat
end if
end if
end repeat
tell application "Mail"
activate
set this_message to make new outgoing message at end of outgoing messages with properties {content:this_text, visible:true}
tell content of this_message
repeat with i from 1 to the length of this_text
repeat
try
set this_font to some item of the font_list
set font_size to random number from low_fontsize to high_fontsize
set red_value to random number from low_color to the high_color
set green_value to random number from low_color to the high_color
set blue_value to random number from low_color to the high_color
tell character i
set font to this_font
set size to font_size
end tell
set the RGB_value to {red_value, green_value, blue_value}
set the color of character i to the RGB_value
exit repeat
end try
end repeat
end repeat
end tell
end tell
If you want to use "Microsoft Word", try this script
tell application "Microsoft Word"
set tc to count characters of selection
set fontNames to font names
set cF to count fontNames
repeat with i from 1 to tc
set thisName to item (random number from 1 to cF) of fontNames
try
tell font object of character i of selection to set name to thisName
end try
end repeat
end tell

GIMP Scriptfu how to make a script that takes two images as input?

I want to make a script-fu script that takes two images as input. How do I do this? Is there a file selector type?
From the tutorial I know that I can do something like
(script-fu-register
"script-fu-text-box" ;func name
"Text Box" ;menu label
"Creates a simple text box, sized to fit\
around the user's choice of text,\
font, font size, and color." ;description
"Michael Terry" ;author
"copyright 1997, Michael Terry;\
2009, the GIMP Documentation Team" ;copyright notice
"October 27, 1997" ;date created
"" ;image type that the script works on
SF-STRING "Text" "Text Box" ;a string variable
SF-FONT "Font" "Charter" ;a font variable
SF-ADJUSTMENT "Font size" '(50 1 1000 1 10 0 1)
;a spin-button
SF-COLOR "Color" '(0 0 0) ;color variable
SF-ADJUSTMENT "Buffer amount" '(35 0 100 1 10 1 0)
;a slider
)
(script-fu-menu-register "script-fu-text-box" "<Image>/File/Create/Text")
(define (script-fu-text-box inText font fontSize textColor bufferAmount)
And this mentions that if you want it to operate on an open image this should be the first parameter, but what if I want it to operate on two images and join them into one (as layers, but then with some transforms applied)?
Just use the SF-IMAGE parameter type. Once for each image you want as input.
For the script-fu to operate on the active image, the first two parameters should be
SF-IMAGE followed by SF-DRAWABLE, and the script has to be registered to the "" menu.
Add a second SF-IMAGE parameter as third parameter, bellow the SF-IMAGE and chec what you get back.

Resources