WinExist returns string - autohotkey

this code
msgbox, % WinExist ("NotExistsWindow")
shows msgbox with text "NotExistsWindow"
It should be "0x0".
Note: window with tile "NotExistsWindow" does not exist.
What is wrong? Thank you.

The function name must be immediately followed by an open-parenthesis, without any spaces inbetween:
msgbox, % WinExist("NotExistsWindow")
For details, see Functions.

Related

AHK: convert ClipboardAll to string (without calling Clipboard instead)

In an AHK script, I want to convert data I read from the clipboard earlier via ClipboardAll into a string I can manipulate, but StrGet() very oddly cannot do that.
!2:: clipget()
clipget()
{
clip := ClipboardAll
MsgBox % clip
MsgBox % NumGet(clip)
MsgBox % StrGet(clip)
Return
}
Firts MsgBox returns empty, because clip is not a string, as expected.
Second returns a numerical representation of the bit data, as expected.
The third returns nothing, not as expected.
I know I could just use clipstr = Clipboard in the first place to have it converted in the first place, but then I would have to read the clipboard twice as in:
clip := ClipboardAll
clipstr := Clipboard
to get both string data and bit data, but that looks uncool and takes longer if the data on the cliboard is large.
A dumb but functional way way could also be
Clipboard := clip
Sleep 500
clipstr := Clipboard
needless to say is not what I am looking for.
So, is there any way to get a string from ClipboardAll bit data with something like
clip := ClipboardAll
clipstr := MagicalFunction(clip)
?
Im not sure if this will help. But the clipboard is not the piece of string that is currently copied. The clipboard is a place where your computer holds your copied stuff, its a place where you can find your previous copied stuff.
A code I found in ahk's Clipboard doc is this
Loop, parse, clipboard, `n, `r
{
MsgBox, %A_LoopField% is your current copied first line..
}
I'm not very familiar with ahk's variable system but I think this should give a string output from %A_LoopField%
in AHK script: Save ClipBoardAll to File.
in external text editor: Write to the BEGINING of File some text eg. "rrr" (or you can do it by "copy +" in command line bat file)
in AHK script: Read File to variable.

Why does remapping key interfere with FormatTime?

I have this code:
FormatTime, CurrentYearMonth,, yyyy-MM
MsgBox, %CurrentYearMonth%
f3::Run D:\folders\%CurrentYearMonth%
a::a
Pressing F3 only opens D:\folders, and there is no message pops up. In order to make it work properly, I need to use
#InputLevel 1
FormatTime, CurrentYearMonth,, yyyy-MM
MsgBox, %CurrentYearMonth%
f3::Run D:\folders\%CurrentYearMonth%
#InputLevel 0
a::a
But I don't understand why this should be the case. I read about #InputLevel and SendLevel but I don't understand much. Do you know why?
I suspect your code is after the top auto-execute section of your script, this will mean your F3 hotkey (f3::) will execute only that line and %CurrentYearMonth% will not be populated. You could format your code like this,
f3::
FormatTime, CurrentYearMonth,, yyyy-MM
MsgBox, %CurrentYearMonth%
Run D:\folders\%CurrentYearMonth%
return
F3 will run all the code from f3:: until return. This is easier to read and understand.
More details on return - https://www.autohotkey.com/docs/commands/Return.htm

How to determine when the Select File Dialog is active?

I'm trying to determine when the file selection dialog is active, but I can't.
SetTitleMatchMode,1
FileSelectFile, SelectedFile
Winwaitactive,Select File -
Msgbox,File Select Dialog is active ; This is never fired
if (SelectedFile = "")
MsgBox, The user didn't select anything.
else
MsgBox, The user selected the following:`n%SelectedFile%
Could you please tell me what I'm doing wrong?
On that thread, code execution stops on the FileSelectFile line until a file has been selected. So your code will never get past the WinWaitActive command, because by the time that command runs, the window you're trying to match isn't open anymore.
And by the looks of things, you can't interrupt that thread without breaking the FileSelectFile command's functionality.
So the exact thing you're trying to do isn't possible without true multithreading.
But maybe you'd be fine to just make a simple assumption like this:
MsgBox, % "File select will begin after you click OK"
FileSelectFile, output
MsgBox, % "File select ended"

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

Clear previously printed message with fprintf

While executing a script, I have used
msg='Please wait...';
a=fprintf('%6s\n',msg);
fprintf('\n');
before the results get displayed. However after the results disclosure on the screen "Please wait..." is still appearing although I used
clear a
Thus, is there a way of turning off in the code
fprintf()
Clear a: the clear function delete the selected variable from the memory. To clear the console you have to use clc.
But you can also comment or delete the line where fprintf is called.
As already stated you have to use clc to clear the command window.
You can also erase just the last characters by inserting multiple backspaces. For instance:
fprintf(repmat(char(8), [1 numel(msg)]));