autohotkey - Trying to delete random items in Listbox - autohotkey

I'm trying a parsing loop, this works good except I can't delete the first item in the list. Probably something to do with the backtick n. Can't figure it out yet!
Update - OK, needed to duplicate the StringReplace line without a backtick n, so as to get the first item in the list.
Gui, Add, ListBox, 8 x26 y37 w100 h100 vColorChoice, Red|Green|Blue|Black|White
Gui +Delimiter`n
Gui, Add, Button, Default, Delete
Gui, show
return
ButtonDelete:
Gui, Submit, NoHide
ControlGet, ListBoxContents, List,, ListBox1
StringReplace, ListBoxContents, ListBoxContents, |, `n, All
msgbox %ColorChoice% ; ColorChoice here contains the files selected to delete
Loop, Parse, ColorChoice, `n
{
StringReplace, ListBoxContents, ListBoxContents, %A_LoopField%`n
StringReplace, ListBoxContents, ListBoxContents, %A_LoopField%
msgbox %ListBoxContents%
}
GuiControl,, ColorChoice, `n%ListBoxContents%
return
GuiClose:
ExitApp

Related

In Autohotkey Format the output of a ListBox?

In Autohotkey, I have the following code :
^r::
Gui, Add, ListBox, Multi vMyListBox r10, Strawberries|Pears|Oranges|`r|Beans|Peas|Tomatoes|Turnips|
Gui, Add, Button, Default, OK
Gui, Show
return
ButtonOK:
Gui, Submit
MsgBox % MyListBox
send, % MyListBox
return
The output is, e.g. :
Strawberries|Beans|Turnips
But I would like it to be :
Strawberries, Beans, Turnips,
How should I proceed ?
Thanks in advance
Sounds like a job for RegEx.
Using RegExReplace() with the MyListBox variable as the haystack and "\|" as the needle. See this interactive for an explanation of this particular RegEx needle.
Saving the replaced version of the String in a variable called NewStr gives us this line of code:
NewStr := RegExReplace(MyListBox, "\|" , Replacement := ", ")
Current Code:
^r::
Gui, Add, ListBox, Multi vMyListBox r10, Strawberries|Pears|Oranges|`r|Beans|Peas|Tomatoes|Turnips|
Gui, Add, Button, Default, OK
Gui, Show
return
ButtonOK:
Gui, Submit
MsgBox % MyListBox
NewStr := RegExReplace(MyListBox, "\|" , Replacement := ", ")
MsgBox % NewStr
send, % NewStr
return
However, this version does not add a trailing ", ". In order to add this, let's just append a ", " to the NewStr variable we just created with NewStr.=", "
Final Code:
^r::
Gui, Add, ListBox, Multi vMyListBox r10, Strawberries|Pears|Oranges|`r|Beans|Peas|Tomatoes|Turnips|
Gui, Add, Button, Default, OK
Gui, Show
return
ButtonOK:
Gui, Submit
MsgBox % MyListBox
NewStr := RegExReplace(MyListBox, "\|" , Replacement := ", ")
NewStr.=", "
MsgBox % NewStr
send, % NewStr
return

Autohotkey - How to navigate a radio list using up and down arrow keys?

I have the following script. I'm not sure how I can modify it to navigate the results using the up or down arrow keys. To run it, just create a .txt file and add some words in there (one word per line), then edit the filepath to where your .txt file is located. Then just search for a keyword that is found in at least 2 of the lines. e.g
Word1
Word2
If you search for word, it will return both word1 and word2. Then I just need to choose between them using up and down arrow key.
F9::
{
FilePath := "D:\5. Programs\AutoHotkey L x64\test.txt"
Gui, Destroy
Gui, Font, S20
Gui, Add, Text, X25 Y10 W450 H30 +Center , Search Box
Gui, Font, S10
Gui, Add, Edit, XP Y+20 W450 H20 vSearchString,
Gui, Color
Gui, Show, W500 H100, Search Box
return
#IfWinActive, Search Box
{
Enter::
Gui, Submit, NoHide
ResultList := []
ChosenString := ""
Loop, Read, %FilePath%
{
if (InStr(A_LoopReadLine, SearchString))
{
ResultList.Push(A_LoopReadLine)
}
}
if (!ResultList.Length())
{
;MsgBox % "No match found!"
return
}
Gui, DisplayResults:New,-MaximizeBox -MinimizeBox , Search results
Gui, Add, Text, w250, % "Click on one of the results below:"
for key, value in ResultList
{
if (key = 1)
Gui, Add, Radio, gSelectResult vChosenString, %value%
else
Gui, Add, Radio, gSelectResult, %value%
}
Gui, DisplayResults:Show
return
SelectResult:
Gui, DisplayResults:Submit
;Msgbox, % ResultList[ChosenString]
Clipboard := % ResultList[ChosenString]
return
}
}
return
You don't want the GUI to disappear?
Add NoHide in line number 44 like this:
Gui, DisplayResults:Submit, NoHide.

Hold RMB after button and click

I can't figure this out.
I'm trying to accomplish something like this:
Press a GUI button
When I click the mouse next hold the RMB
When I click again release the RMB
Current work:
Toggle=0
GUI, Add, Button, w50 h50, Nbutton
GUI, Show, x50 y50
return
ButtonNButton:
{
Toggle:=!Toggle
}
if GetKeyState("LButton","P")
if (Toggle == 1)
MsgBox, Do
Toggle:=!Toggle
return
This should work to accomplish what you want.
Note that right now, this example script is limited to only work in Notepad. You should update the script to target whatever program/game/window you want the hotkey to be active in.
Toggle := 0
GUI, Add, Button, w50 h50, Nbutton
GUI, Show, x50 y50
return
ButtonNButton:
Toggle:=!Toggle
; Update this line to match whatever window you want the hotkey active
; in, or delete this line to make the hotkey active everywhere.
Hotkey, IfWinActive, ahk_class Notepad
if Toggle
{
Hotkey, ~*LButton Up, RBSwitch, On
}
else
{
if GetKeyState("RButton") ;release RButton when hotkey is disabled
Send {RButton Up}
Hotkey, ~*LButton Up, RBSwitch, Off
}
RETURN
RBSwitch:
if GetKeyState("RButton")
Send {RButton Up}
else
Send {RButton Down}
RETURN

Input text on any application with autohotkey gui

I'm trying to find a solution to add predefined text to any application so instead of remembering a hotkey/hotstring combination I'll just need to click on the GUI button for the text.
This is what I have right now:
Gui, Add, Button, x22 y20 w120 h40 , Title
Gui, Add, Button, x22 y70 w120 h40 , Paragraph
; Generated using SmartGUI Creator 4.0
Gui, Show, x152 y89 h131 w222, New GUI Window
Return
ButtonTitle:
Send Title
return
ButtonParagraph:
Send Paragraph
return
GuiClose:
ExitApp
My problem is that I'm not able to make it work properly. I just want to click the button and that word gets shot to the notepad/word/any application.
The solution would be to always keep track of the current and the last active window. To achieve that you could use a shell hook to get notified when the active window changes: https://autohotkey.com/board/topic/66726-method-to-detect-active-window-change/
So you could have two variables currentWin and lastWin and when the active window changes you set lastWin := currentWin and currentWin := activeWin.
Soemthing like this maybe:
Gui, Add, Button, x22 y20 w120 h40 , Title
Gui, Add, Button, x22 y70 w120 h40 , Paragraph
; Generated using SmartGUI Creator 4.0
Gui, Show, x152 y89 h131 w222, New GUI Window
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage(wParam, lParam) {
If (wParam=4) { ;HSHELL_WINDOWACTIVATED
lastWin := currentWin
currentWin := "ahk_id " . lParam
}
}
ButtonTitle:
WinActivate, % lastWin
Send Title
return
ButtonParagraph:
WinActivate, % lastWin
Send Paragraph
return
GuiClose:
ExitApp
(completely untested)
Hi Stephan and Forivin,
Thank you for your help. I used the alt+tab approach to solve the problem though I need to ensure that I get notepad on alt+tab keystroke or else it goes to another application. The below solves the problem.
Gui, Add, Button, x22 y20 w120 h40 , Title
Gui, Add, Button, x22 y70 w120 h40 , Paragraph
Gui, Show, x152 y89 h131 w222, New GUI Window
Return
ButtonTitle:
Send, !{Esc}
Send Title
return
ButtonParagraph:
Send, !{Esc}
Send Paragraph
return
GuiClose:
ExitApp

Auto-HotKey GUI Button to simulate a keyboard button

I have a button on a gui Titled "F4," Now when i click that button i want it to simulate the F4 Button on my keyboard.
Is it possible to do it?
This is the button:
Gui, Add, Button, x20 y260 w50 h20, F4
Your question seems to be how to switch back to the orginal application, so that the data will be sent there, not to AHK itself.
I pasted an example below just for you to look at. At around line 83 you will see that I first check which window is active and then, if required, switch back to the previous window using !{Esc} , before sending the actual data. This was just an exercise which I wrote long ago, to create multiple paste options. I am sure there are lost of things that can be improved, but it will give you some idea as to how you can solve this.
#SingleInstance Force
#installKeybdHook
#Persistent
; Written by Robert Ilbrink, The Netherlands
Menu, Tray, Icon , Shell32.dll, 247, 1
SetTitleMatchMode, 2
Global Nr := 1
Next=Copy
x:=A_ScreenWidth-250
y:=A_ScreenHeight/2
; ================================== GUI ==================================
ShowListBox:
Gui, +Resize
Gui, Add, ListBox, h270 w195 vMyListBox gMyListBox, 01 %Array_1%|02 %Array_2%|03 %Array_3%|04 %Array_4%|05 %Array_5%|06 %Array_6%|07 %Array_7%|08 %Array_8%|09 %Array_9%|10 %Array_10%|11 %Array_11%|12 %Array_12%|13 %Array_13%|14 %Array_14%|15 %Array_15%|16 %Array_16%|17 %Array_17%|18 %Array_18%|19 %Array_19%|20 %Array_20%
;Gui, Add, Button, x10 y275 w45 h20, Paste
Gui, Add, Button, x10 y275 w45 h20, Refresh
Gui, Add, Button, x60 y275 w45 h20, Help
Gui, Add, Button, x110 y275 w45 h20, Special
Gui, +AlwaysOnTop
WinGet, State, MinMax,
If (State = -1)
Return
Else
Gui, Show, x%x% y%y%,Clippy CapsLock = %Next%
Return
GuiSize:
GuiControl, Move, MyListBox, % "w" A_GuiWidth - 20
GuiControl, Move, MyListBox, % "h" A_GuiHeight - 30
GuiControl, Move, Refresh, % "y" A_GuiHeight - 25
GuiControl, Move, Help, % "y" A_GuiHeight - 25
GuiControl, Move, Special, % "y" A_GuiHeight - 25
return
; ================================== SHORTCUTS ==================================
;+^1::MySend(Array_1) ; [Shift]+[Ctrl]+1 pastes item 1
;+^2::MySend(Array_2) ; [Shift]+[Ctrl]+2 pastes item 2
;+^3::MySend(Array_3) ; [Shift]+[Ctrl]+3 pastes item 3
;+^4::MySend(Array_4) ; [Shift]+[Ctrl]+4 pastes item 4
;+^5::MySend(Array_5) ; [Shift]+[Ctrl]+5 pastes item 5
;+^6::MySend(Array_6) ; [Shift]+[Ctrl]+6 pastes item 6
;+^7::MySend(Array_7) ; [Shift]+[Ctrl]+7 pastes item 7
;+^8::MySend(Array_8) ; [Shift]+[Ctrl]+8 pastes item 8
;+^9::MySend(Array_9) ; [Shift]+[Ctrl]+9 pastes item 9
;+^0::MySend(Array_10) ; [Shift]+[Ctrl]+0 pastes item 10
;+^q::MySend(Array_11) ; [Shift]+[Ctrl]+q pastes item 11
;+^w::MySend(Array_12) ; [Shift]+[Ctrl]+w pastes item 12
;+^e::MySend(Array_13) ; [Shift]+[Ctrl]+e pastes item 13
;+^r::MySend(Array_14) ; [Shift]+[Ctrl]+r pastes item 14
;+^t::MySend(Array_15) ; [Shift]+[Ctrl]+t pastes item 15
;+^y::MySend(Array_16) ; [Shift]+[Ctrl]+y pastes item 16
;+^u::MySend(Array_17) ; [Shift]+[Ctrl]+u pastes item 17
;+^i::MySend(Array_18) ; [Shift]+[Ctrl]+i pastes item 18
;+^o::MySend(Array_19) ; [Shift]+[Ctrl]+o pastes item 19
;+^p::MySend(Array_20) ; [Shift]+[Ctrl]+p pastes item 20
; ================================== NUMPAD SHORTCUTS ==================================
#Numpad1::MySend(Array_1) ; [Win]+1 pastes item 1
#Numpad2::MySend(Array_2) ; [Win]+2 pastes item 2
#Numpad3::MySend(Array_3) ; [Win]+3 pastes item 13
#Numpad4::MySend(Array_4) ; [Win]+4 pastes item 4
#Numpad5::MySend(Array_5) ; [Win]+5 pastes item 5
#Numpad6::MySend(Array_6) ; [Win]+6 pastes item 6
#Numpad7::MySend(Array_7) ; [Win]+7 pastes item 7
#Numpad8::MySend(Array_8) ; [Win]+8 pastes item 8
#Numpad9::MySend(Array_9) ; [Win]+9 pastes item 9
#Numpad0::MySend(Array_10) ; [Win]+0 pastes item 10
+#Numpad1::MySend(Array_11) ; [Shift]+[Win]+1 pastes item 11
+#Numpad2::MySend(Array_12) ; [Shift]+[Win]+2 pastes item 12
+#Numpad3::MySend(Array_13) ; [Shift]+[Win]+3 pastes item 13
+#Numpad4::MySend(Array_14) ; [Shift]+[Win]+4 pastes item 14
+#Numpad5::MySend(Array_15) ; [Shift]+[Win]+5 pastes item 15
+#Numpad6::MySend(Array_16) ; [Shift]+[Win]+6 pastes item 16
+#Numpad7::MySend(Array_17) ; [Shift]+[Win]+7 pastes item 17
+#Numpad8::MySend(Array_18) ; [Shift]+[Win]+8 pastes item 18
+#Numpad9::MySend(Array_19) ; [Shift]+[Win]+9 pastes item 19
+#Numpad0::MySend(Array_20) ; [Shift]+[Win]+0 pastes item 20
; ================================== SEND FUNCTION ==================================
MySend(Item)
{
ClipBoard = %Item%
IfWinActive, MultiClipBoard
Send, !{Esc}
Sleep, 100
Send, ^v{Space}
}
; ================================== SINGLE or DOUBLE CLICK ==================================
MyListBox: ; Single Click (Normal) or Double Click (DoubleClick) to paste data back to other application
if A_GuiControlEvent <> Normal ; Alternatively use "DoubleClick"
return
GuiControlGet, MyListBox ; Retrieve the ListBox's current MultiClipBoard.
StringTrimLeft, ClipBoard, MyListBox, 3
Send, !{Esc}
Sleep, 200
Send, ^v
;Send, {Enter}
;SendInput, %MyListBox%{Tab}
return
; ================================== PASTE BUTTON ==================================
ButtonPaste: ; Select an item with [Up] / [Down] arrows and press [Paste] to paste data back to other application
Gui, Submit, NoHide
StringTrimLeft, ClipBoard, MyListBox, 3
Send, !{Esc}
Sleep, 200
Send, ^v
;SendInput, %MyListBox%{Tab}
Return
; ================================== EXIT / CLOSE BUTTONS ==================================
GuiClose:
GuiEscape:
Gui, Destroy
ExitApp
; ================================== REFRESH BUTTON ==================================
ButtonRefresh:
Gui,+LastFound
WinGetPos,x,y,w,h
Gui, Destroy
GoSub, ShowListBox
Return
; ================================== Copy Into the List ==================================
Browser_Favorites:: ; [Star] key to copy to clipboard.
!c:: ; [Alt]+c to copy to multiClipboard.
#c:: ; [Win]+c to copy to multiClipboard.
Send, ^c
Sleep, 100
Array_%Nr% := ClipBoard
Nr++
If (Nr >20) ; wrap around after 20 entries
Nr:=1
WinGet, State, MinMax, MultiClipBoard
If (State = -1)
Return ; GUI, Show
Gui,+LastFound
WinGetPos,x,y,w,h
Gui, Destroy
GoSub, ShowListBox
Return
; ================================== CLEAR / OPEN / SAVE ==================================
ButtonSpecial:
SetTimer, ChangeButtonNames, 30
MsgBox, 4098, Special Action, [Clear]`, Clears all items`n[Open]`, Open a list`n[Save]`, Save current list.
IfMsgBox, Abort ; ================================== CLEAR ==================================
{
MsgBox, 4100,, Are you sure to clear the list?
IfMsgBox No
Return
Nr := 1
Loop, 20
{
Array_%Nr% =
Nr++
}
Nr := 1
Gui,+LastFound
WinGetPos,x,y,w,h
Gui, Destroy
GoSub, ShowListBox
Exit
}
Else ifMsgBox, Retry ; ================================== OPEN ==================================
{
FileSelectFile, SelectedFile, 3, , Open a file, Text Documents (*.txt)
If SelectedFile =
Exit
Else
{
Loop, read, %SelectedFile%
{
If A_LoopReadLine =
Break
Array_%A_Index% := A_LoopReadLine
If (A_Index > 20)
Break
Nr := A_Index + 1
}
}
Gui,+LastFound
WinGetPos,x,y,w,h
Gui, Destroy
GoSub, ShowListBox
Exit
}
Else ifMsgBox, Ignore ; ================================== SAVE ==================================
{
FileSelectFile, SelectedFile, S, , Save the list as a Text Document, (*.txt)
StringReplace, SelectedFile, SelectedFile, `.txt,
FileDelete, %SelectedFile%.txt
Loop, 20
{
CurrentLine := Array_%A_Index%
FileAppend, %CurrentLine%`n, %SelectedFile%.txt ; % Array_%A_Index%, doesn't work
}
}
Exit
; ================================== HELP BUTTON ==================================
ButtonHelp:
MsgBox, , MultiClipBoard Help, Press [Alt]+c or [Win]+c to copy highlighted data onto the clipboard (TEXT only).`n`nClick on an item to paste it to the last active application.`n`nPress [Refresh] to refresh the screen after items were added while minimized.`n`nPress [Special] to select`n[Clear] to clear the list (after confirmation),`n[Load] to load items from a file or`n[Save] to save current items to a file. `n`n[Shift]+[Ctrl]+[nr] (1...0) will paste item 1...10.`n[Shift]+[Ctrl]+[letter] (qwertyuiop) will paste item 11...20.
; ================================== RENAME STANDARD MSGBOX BUTTONS FOR SPECIAL MENU ==================================
ChangeButtonNames:
IfWinNotExist, Special Action
return ; Keep trying.
SetTimer, ChangeButtonNames, off
WinActivate
ControlSetText, Button1, &Clear
ControlSetText, Button2, &Open
ControlSetText, Button3, &Save
return
; ================================== ToggleCopy ==================================
CapsLock::
Send % "{CTRL DOWN}" (!paste ? "c" : "v") "{CTRL UP}"
;SoundBeep, % (!paste ? "300" : "3000"), 100
paste := !paste
;SplashTextOn, 150, 20,%WindowTitle%, % (!paste ? "Press " MyKey " to Copy" : "Press " MyKey " to Paste")
;WinMove, %WindowTitle%, , 800, 5
Menu, Tray, Icon , Shell32.dll, % (paste ? "248" : "247"), 1 ; Toggle between Up/Down Arrow in Icon
Next:=(!paste ? " Copy" : " Paste")
Gui,+LastFound
WinGetPos,x,y,w,h
Gui, Destroy
GoSub, ShowListBox
Send, !{Esc}
Return
; ================================== END ==================================
Taken from this forum post:
Gui, +LastFound +AlwaysOnTop -Caption +E0x08000000
Gui, Font, s22, Arial
Gui, Add, Button, x0 y0 w100 h50 gButton, F4
Gui, Show, x0 y0 w100 h50 NoActivate
; Define a hotkey to show/hide the Gui:
F1::
toggle := !toggle
If (toggle)
Gui, Cancel
else
Gui, Show, NoActivate
return
Button:
SendInput, {F4}
return