What is the output of an InputBox command that is left empty - autohotkey

What is the output of an InputBox command that is left empty
Is there a good way to check if the feild was left empty
like if OutputVar = "" { goto, restart }
i've tried the solution above and it didn't work for me any help is appreciated

This is what I use:
InputBox, text, Enter Text
if(text)
MsgBox Something was inputed
else{
MsgBox Nothing was inputed
goto restart
}
The output of an empty InputBox is not a value, so you can just check whether or not the variable you store the output as has a value or not through an if(variable) statement.
Edit responding to comment:
AHK is special in that you do not need a boolean value (i.e. True or False) within an if statement in order for it to work (in contrast to traditional programming languages). In ahk, if the value inside the parenthesis of an if statement is a non-boolean data type (i.e. a number), the program will treat it as true if the value is non-zero. This can be seen with the following code:
text:=0 ; Alternatively: text:=""
if(text)
MsgBox True was triggered!
else
MsgBox False was triggered!
Another Edit after I noticed something:
The idea behind the code you had above also works, provided that you move the curly braces/ content to the next line.
So either
if (text = "")
{
MsgBox hello
}
or
if (text = "")
MsgBox hello
is also fine.

Related

Get First Character of A_LoopFileName?

I'm trying to parse a filename and get the first character from it as a string to compare it to a previously inputted variable. My code looks like:
FileSelectFolder, WhichFolder ; Ask the user to pick a folder.
; Ask what letter you want to start the loop from
InputBox, UserInput, Start At What Letter?, Please enter a letter to start at within the folder (CAPITALIZE IT!)., , 450, 150
if ErrorLevel {
MsgBox, CANCEL was pressed.
ExitApp
} else {
inputted_letter = %UserInput%
tooltip %inputted_letter% ; Show the inputted letter
sleep, 2000
tooltip
}
Loop, %WhichFolder%\*.*
{
current_filename_full = %A_LoopFileName%
files_first_letter := SubStr(current_filename_full, 1, 1)
tooltip %files_first_letter% ; Show the file's first letter
sleep, 2000
tooltip
if files_first_letter != inputted_letter
continue
...
Right now, it clearly shows in the tooltips the user-entered capital letter, and then the first letter of each file name from within the selected folder, but for some reason when the two look alike, it doesn't recognize them as a match. I'm thinking maybe because technically A_LoopFileName is not of a string type? Or maybe the inputted letter doesn't match the type of the first filename's letter?
I want it to continue if the inputted letter and the first letter of the filename don't match, but if they do, to carry on with the rest of the script. Any ideas on how I can get these two to successfully match? Thanks!
Firstly, AHK doesn't really have types. At least not how you've experienced types in other languages.
So your assumption about "not being correct type" will pretty much always be wrong.
So the actual cause is because in a legacy if statement, the syntax is
if <name of variable> <operator> <legacy way of representing a value>
So you'd do it like this:
if files_first_letter != %inputted_letter%
You we're comparing if the variable files_first_letter is equal to the literal text inputted_letter.
However, I highly recommend you stop using legacy syntax. It's really just that old.
It'll differ horribly much from any other programming language and you run into confusing behavior like this. Expression syntax is what you want to use in AHK nowadays.
Here's your code snippet converted over to expression syntax in case you're interested:
FileSelectFolder, WhichFolder
;Forcing an expression like this with % in every parameter
;is really not needed of course, and could be considered
;excessive, but I'm doing it for demonstrational
;purposes here. Putting everything in expression syntax.
;also, not gonna lie, I always do it myself haha
InputBox, UserInput, % "Start At What Letter?", % "Please enter a letter to start at within the folder (CAPITALIZE IT!).", , 450, 150
if (ErrorLevel)
;braces indicate an expression and the non-legacy if statement
;more about this, as an expression, ErrorLevel here holds the value
;1, which gets evaluated to true, so we're doing
;if (true), which is true
{
MsgBox, % "CANCEL was pressed."
ExitApp
}
else
inputted_letter := UserInput ; = is never used, always :=
Loop, Files, % WhichFolder "\*.*"
;non-legacy file loop
;note that here forcing the expression statement
;with % is actually very much needed
{
current_filename_full := A_LoopFileName
files_first_letter := SubStr(current_filename_full, 1, 1)
if (files_first_letter != inputted_letter)
continue
}
Also you don't have to be concerned about case with !=, it'll always compare case insensitively.

How to pass a variable to WinExist?

I need to do
if WinExist(%window%) {...}
however this doesnt work
%window% is set earlier to be equal to %3% which is a command line argument that should be only of type string pointing the AHK script to the window of intrest.
I tried replacing %window% with %3% however the code inside never gets run. Why?
Since WinExist() accepts a string parameter for the window title, you don't need to enclose the variable in %.
If you set window to the value of the 3rd command parameter then the following code should work.
window = %3%
if WinExist(window)
{
Msgbox, Exists.
}
else
{
Msgbox, Does not exist.
}
Note: windowor %3% must exactly match the window title, or you must use SetTitleMatchMode, 2
(see SetTitleMatchMode).
If WinExist(window)
is an expression, any variable names in its parameter should not be enclosed in percent signs.
By contrast, literal strings should be enclosed in double quotes:
If WinExist("Untitled - Notepad")

CATIA macro (Input box not working)

Help My Balloon finding macro is not working with input box, it works only when i manually add the balloon number.. please tell me what i m missing ...Ferdo m expecting you
Language="VBSCRIPT"
Sub CATMain()
Set drawingDocument1 = CATIA.ActiveDocument
Set selection1 = drawingDocument1.Selection
result = InputBox("Ballon Number ?", "Title") 'The variable is assigned the value entered in the InputBox
selection1.Search "CATDrwSearch.DrwBalloon.BalloonPartName_CAP= result ,all"
End Sub
I don't know what you are doing but the last line looks wrong. I don't know what the docs are for your function but you are passing the string result rather than the value of the variable result because it is in quotes. Assuming your line is otherwise right ...
selection1.Search "CATDrwSearch.DrwBalloon.BalloonPartName_CAP= " & result & ",all"

Check if field contains any numbers?

Hi I'm trying to write some VBA so that it checks whether one of my text boxes contains a number. The text box is called: CustomerName. Here's the code I am currently using:
Function HasNumber(strData As String) As Boolean
Dim iCnt As Integer
For iCnt = 1 To Len(strData)
If IsNumeric(Mid(strData, iCnt, 1)) Then
HasNumber = True
Exit Function
End If
Next iCnt
End Function
Private Sub CustomerName_AfterUpdate()
If HasNumber(CustomerName) Then
MsgBox "Only letters are allowed for this field."
Exit Sub
End If
End Sub
For some reason when I enter numbers into this field and then click out of it (i.e. update it) it doesn't come up with a msgbox or anything. What can I do to fix this?
Instead of using some custom code, I would use this validation rule on the CustomerName column of your table, or on the validation rule of your text box:
Not Like "*[0-9]*"
See here for a reference of validation rules.
Try it like this:
If HasNumber(CustomerName.Text) Then

Konami Code on autohotkey

up up down down left right left right b a enter :: Msgbox, konami code.
is there a way to do this?
yes its actually pretty simple...
comb := "up|down|down|left|right|left|right|b|a|enter"
~up::
Loop, parse, comb, |
{
input, var,T.900,{%a_loopfield%}
if inStr(ErrorLevel, "Timeout")
return
}
msgbox Konami Code!!!
return
The first "up" is the one that will trigger the sequence hence only one "up" in the combination variable.
you can change the combination to whatever you want, but then you would have to change the hotkey to the first "key" that you want to press.