Different InputBoxes depending on the result of ClipWait Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Different InputBoxes depending on the result of ClipWait

Post by Avastgard » 31 Jan 2023, 16:40

I have this code:

Code: Select all

#y::
AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
ClipboardOld = %ClipboardAll%
Clipboard =  ; Must start off blank for detection to work.
Send ^c
ClipWait 1
if ErrorLevel  ; ClipWait timed out.
	return
; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring:
; The same is done for any other characters that might otherwise
; be a problem in raw mode:
StringReplace, Hotstring, Clipboard, ``, ````, All  ; Do this replacement first to avoid interfering with the others below.
StringReplace, Hotstring, Hotstring, `r`n, ``r, All  ; Using `r works better than `n in MS Word, etc.
StringReplace, Hotstring, Hotstring, `n, ``r, All
StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
StringReplace, Hotstring, Hotstring, `;, ```;, All
Clipboard = %ClipboardOld%  ; Restore previous contents of clipboard.
; This will move the InputBox's caret to a more friendly position:
SetTimer, MoveCaret, 10
; Show the InputBox, providing the default hotstring:

InputBox, Hotstring, Nova correção, Forneça a palavra correta no lado direito. É possível também editar o lado esquerdo.`n`nExemplo`n::dso::dos,,,,,,,,::%Hotstring%::%Hotstring%

if ErrorLevel <> 0  ; The user pressed Cancel.
	return
; Otherwise, add the hotstring and reload the script:
FileAppend, `n%Hotstring%, %A_ScriptFullPath%  ; Put a `n at the beginning in case file lacks a blank line at its end.
Reload
Sleep 200 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The hotstring just added appears to be improperly formatted.  Would you like to open the script for editing? Note that the bad hotstring is at the bottom of the script.
IfMsgBox, Yes, Edit
	return

MoveCaret:
IfWinNotActive, Nova correção
	return
; Otherwise, move the InputBox's insertion point to where the user will type the abbreviation.
Send {HOME}
Loop % StrLen(Hotstring) + 4
	SendInput {Right}
SetTimer, MoveCaret, Off
SendInput {Ctrl down}{Shift down}{Right}{Ctrl up}{Shift up}
return
In summary, this is a prompt to make you add autocorrection strings to a script. This is what it does:

1. Shows an InputBox when you press Win + y and you have previously selected text.
2. The edit field defaults do the selected text in a basic hotstring syntax, like ::text::text.
3. The caret is placed right after the colon so you can edit the hotstring output.
4. You edit the hotstring, press OK and it's appended to the end of the file.
5. If you press Win + y with no previously selected text, nothing happens.

What I want is item 5 to be different. Instead of nothing happening, I would like the InpuBox to also show up, but with no default text in the edit field so I can type my own. I tried doing it, but I keep messing my Ifs

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Different InputBoxes depending on the result of ClipWait  Topic is solved

Post by mikeyww » 31 Jan 2023, 18:19

Code: Select all

If ErrorLevel {   ; ClipWait timed out
 ; Do more stuff
 Return
}
Explained: Block

Beyond the fact that AHK v1 is officially deprecated, StringReplace is also deprecated as a v1 command. You may want to get up to date at some point! :) AHK v1 scripts will continue to run with v1.

Code: Select all

#Requires AutoHotkey v2.0
#y:: {
 A_Clipboard := ""
 Send '^c'
 If ClipWait(1) {
  MsgBox 'Copied!'
 } Else {
  MsgBox 'Timed out!'
 }
}

gmoises
Posts: 74
Joined: 18 Nov 2017, 16:43

Re: Different InputBoxes depending on the result of ClipWait

Post by gmoises » 31 Jan 2023, 18:34

It is working, but no Clipboard TimeOut
mikeyww posted a great option to implement the TimeOut

Code: Select all

#y::
	AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
	ClipboardOld = %ClipboardAll%
	Clipboard =  ; Must start off blank for detection to work.
	Send ^c
	ClipWait 1
;	if ErrorLevel  ; ClipWait timed out.
;		return
	; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring:
	; The same is done for any other characters that might otherwise
	; be a problem in raw mode:
	If (Clipboard = "")		; nothing was selected
		Default := ""
	Else
	{
		StringReplace, Hotstring, Clipboard, ``, ````, All  ; Do this replacement first to avoid interfering with the others below.
		StringReplace, Hotstring, Hotstring, `r`n, ``r, All  ; Using `r works better than `n in MS Word, etc.
		StringReplace, Hotstring, Hotstring, `n, ``r, All
		StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
		StringReplace, Hotstring, Hotstring, `;, ```;, All
		Clipboard = %ClipboardOld%  ; Restore previous contents of clipboard.
		Default := "::" . Hotstring . "::" . Hotstring
		; This will move the InputBox's caret to a more friendly position:
		SetTimer, MoveCaret, 10
	}
	; Show the InputBox, providing the default hotstring:
	InputBox, Hotstring
			, Y correção
			, Forneça a palavra correta no lado direito. É possível também editar o lado esquerdo.`n`nExemplo`n::dso::dos,,,,,,,
			, % Default

	if ErrorLevel <> 0  ; The user pressed Cancel.
		return
	; Otherwise, add the hotstring and reload the script:
	FileAppend, `n%Hotstring%, %A_ScriptFullPath%  ; Put a `n at the beginning in case file lacks a blank line at its end.
	Reload
	Sleep 200 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
	MsgBox, 4,, The hotstring just added appears to be improperly formatted.  Would you like to open the script for editing? Note that the bad hotstring is at the bottom of the script.
	IfMsgBox, Yes, Edit
		return

MoveCaret:
IfWinNotActive, Nova correção
	return
; Otherwise, move the InputBox's insertion point to where the user will type the abbreviation.
Send {HOME}
Loop % StrLen(Hotstring) + 4
	SendInput {Right}
SetTimer, MoveCaret, Off
SendInput {Ctrl down}{Shift down}{Right}{Ctrl up}{Shift up}
return
Last edited by gmoises on 31 Jan 2023, 19:06, edited 1 time in total.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Different InputBoxes depending on the result of ClipWait

Post by mikeyww » 31 Jan 2023, 19:05

If you comment the lines with ;, they will never execute because they are comments.

The first script that I posted shows how you can use braces to create a block for your ErrorLevel. Inside the block, you put all of the commands that apply to that condition (the timeout). Your final command in the block will be Return. This will prevent the subroutine's remaining commands (after the block) from executing when the timeout occurs.

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Different InputBoxes depending on the result of ClipWait

Post by Avastgard » 09 Feb 2023, 12:00

Thanks for the help as always, @mikeyww!

Post Reply

Return to “Ask for Help (v1)”