how to combine code below?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

how to combine code below?

09 Sep 2020, 06:44

I have 2 scripts below - 1 for general google queries and folder directories, and 2 for registry entries.

How do I merge 2 with 1? i.e. when I highlight a registry entry with left/right click, it can be recognized as such.

Code: Select all

;; Go to anything that is in the currently selected text: URLs, email addresses, Windows paths, or just "Google it" USING WIN+Q
$#Q::
~LButton & RButton:: ; Open in existing Chrome Window
    ;Tip("Clipping...")  ;; include my mouse-tip library for this https://gist.github.com/2400547
    clip := CopyToClipboard()
    if (!clip) {
        return
    }
    addr := ExtractAddress(clip)
    if (!addr)
    {
        ; Google it
        ;Tip("Searching for [" SubStr(clip, 1, 50) "] ...")
        addr := "https://www.google.com/search?q=" . clip
    }
    else {
        ; Go to it using system's default methods for the address
        ;Tip("Going to " Substr(addr, 1, 25) " ...")
    }

    Run %addr%
    return

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	SetTitleMatchMode, 3
;; Go to anything that is in the currently selected text: URLs, email addresses, Windows paths, or just "Google it" USING Hold Left Mouse and Click Right Button
~LButton & MButton:: ; Open in NEW Chrome Window
    ;Tip("Clipping...")
    clip := CopyToClipboard()
    if (!clip) {
        return
    }
    addr := ExtractAddress(clip)
    if (!addr)
    {
        ; Google it
        ;Tip("Searching for [" SubStr(clip, 1, 50) "] ...")
        addr := "https://www.google.com/search?q=" . clip
    }
    else {
        ; Go to it using system's default methods for the address
        ;Tip("Going to " Substr(addr, 1, 25) " ...")
    }


	WinGetActiveTitle, TabCurrentlyBrowsing
	Run %addr%
		while (WinActive(TabCurrentlyBrowsing))
			Sleep, 10
		while (!WinActive(TabCurrentlyBrowsing))
		{
		SendInput, ^+{Tab}
		Sleep, 50
		}
return



	; Ctrl Win Q to run Google Images
$^#Q::
    Send, ^c
    Sleep 50
    parameter = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe https://www.google.com.sg/search?q="%clipboard%"&hl=en&gbv=2&tbs=isz:lt,islt:vga&tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h
    Run %parameter%
    Return


	^#x:: ;Google Images for multiple queries in Excel
Xl := ComObjActive("Excel.Application")

For SelectedCell in Xl.Selection
{
	Query := StrReplace(SelectedCell.Value, A_Space, "+")
,	GoogleImageSearch := "C:\Users\%A_UserName%\AppData\Local\Google\Chrome\Application\chrome.exe "
.	"https://www.google.com.sg/search?q=" Query "&hl=en&gbv=2&tbs=isz:lt,islt:vga&"
.	"tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&"
.	"bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h"
	Run % GoogleImageSearch
}
return


;; utility functions

;; Safely copies-to-clipboard, restoring clipboard's original value after
;; Returns the captured clip text, or "" if unsuccessful after 4 seconds
CopyToClipboard()
{
    ; Wait for modifier keys to be released before we send ^C
    KeyWait LWin
    KeyWait Alt
    KeyWait Shift
    KeyWait Ctrl

    ; Capture to clipboard, then restore clipboard's value from before capture
    ExistingClipboard := ClipboardAll
    Clipboard =
    SendInput ^{Insert}
    ClipWait, 4
    NewClipboard := Clipboard
    Clipboard := ExistingClipboard
    if (ErrorLevel)
    {
        ;MsgBox, The attempt to copy text onto the clipboard failed.
        ;Tip("The attempt to copy text onto the clipboard failed.")
        return ""
    }
    return NewClipboard
}

;; Extracts an address from anywhere in str.
;; Recognized addresses include URLs, email addresses, domain names, Windows local paths, and Windows UNC paths
ExtractAddress(str)
{
    if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
        return match1
    if (RegExMatch(str, "S)(\w+@[\w.]+\.(com|net|org|gov|cc|edu|info|sg))", match))
        return "mailto:" . match1
    if (RegExMatch(str, "S)(www\.\S+)", match))
        return "http://" . match1
    if (RegExMatch(str, "S)(\w+\.(com|net|org|gov|cc|edu|info|sg))", match))
        return "http://" . match1
    if RegExMatch(str, "S)([a-zA-Z]:[\\/][^/:*?""<>|]+)", match)
        return match1
    if (RegExMatch(str, "S)(\\\\[\w\-]+\\.+)", match))
        return match1
	if InStr(str, "%APPDATA%")
		Return StrReplace(str, "%APPDATA%", A_AppData)
	return ""
}

Code: Select all

; Use Ctrl + Alt + R to navigate to Registry Address RegEdit

;Open Regedit and navigate to RegPath.
;RegPath accepts both HKEY_LOCAL_MACHINE and HKLM formats.
RegJump(RegPath)
{
	;Must close Regedit so that next time it opens the target key is selected
	WinClose, Registry Editor ahk_class RegEdit_RegEdit

	If (SubStr(RegPath, 0) = "\") ;remove trailing "\" if present
		RegPath := SubStr(RegPath, 1, -1)

	;Extract RootKey part of supplied registry path
	Loop, Parse, RegPath, \
	{
		RootKey := A_LoopField
		Break
	}

	;Now convert RootKey to standard long format
	If !InStr(RootKey, "HKEY_") ;If short form, convert to long form
	{
		If RootKey = HKCR
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CLASSES_ROOT
		Else If RootKey = HKCU
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CURRENT_USER
		Else If RootKey = HKLM
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_LOCAL_MACHINE
		Else If RootKey = HKU
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_USERS
		Else If RootKey = HKCC
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CURRENT_CONFIG
	}

	;Make target key the last selected key, which is the selected key next time Regedit runs
	RegWrite, REG_SZ, HKCU, Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, LastKey, %RegPath%
	Run, Regedit.exe
}

^#r::
^!r::
	SendInput ^c
	Sleep 100 ;Wait for copied text to become available
	If InStr(Clipboard, "HKEY_")
		RegJump( Clipboard ) ;Go to Registry path
Return
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: how to combine code below?

10 Sep 2020, 09:35

What's wrong with putting the scripts together in one file? Do I miss something?

Code: Select all

;; Go to anything that is in the currently selected text: URLs, email addresses, Windows paths, or just "Google it" USING WIN+Q
$#Q::
~LButton & RButton:: ; Open in existing Chrome Window
    ;Tip("Clipping...")  ;; include my mouse-tip library for this https://gist.github.com/2400547
    clip := CopyToClipboard()
    if (!clip) {
        return
    }
    addr := ExtractAddress(clip)
    if (!addr)
    {
        ; Google it
        ;Tip("Searching for [" SubStr(clip, 1, 50) "] ...")
        addr := "https://www.google.com/search?q=" . clip
    }
    else {
        ; Go to it using system's default methods for the address
        ;Tip("Going to " Substr(addr, 1, 25) " ...")
    }

    Run %addr%
    return

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	SetTitleMatchMode, 3
;; Go to anything that is in the currently selected text: URLs, email addresses, Windows paths, or just "Google it" USING Hold Left Mouse and Click Right Button
~LButton & MButton:: ; Open in NEW Chrome Window
    ;Tip("Clipping...")
    clip := CopyToClipboard()
    if (!clip) {
        return
    }
    addr := ExtractAddress(clip)
    if (!addr)
    {
        ; Google it
        ;Tip("Searching for [" SubStr(clip, 1, 50) "] ...")
        addr := "https://www.google.com/search?q=" . clip
    }
    else {
        ; Go to it using system's default methods for the address
        ;Tip("Going to " Substr(addr, 1, 25) " ...")
    }


	WinGetActiveTitle, TabCurrentlyBrowsing
	Run %addr%
		while (WinActive(TabCurrentlyBrowsing))
			Sleep, 10
		while (!WinActive(TabCurrentlyBrowsing))
		{
		SendInput, ^+{Tab}
		Sleep, 50
		}
return



	; Ctrl Win Q to run Google Images
$^#Q::
    Send, ^c
    Sleep 50
    parameter = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe https://www.google.com.sg/search?q="%clipboard%"&hl=en&gbv=2&tbs=isz:lt,islt:vga&tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h
    Run %parameter%
    Return


	^#x:: ;Google Images for multiple queries in Excel
Xl := ComObjActive("Excel.Application")

For SelectedCell in Xl.Selection
{
	Query := StrReplace(SelectedCell.Value, A_Space, "+")
,	GoogleImageSearch := "C:\Users\%A_UserName%\AppData\Local\Google\Chrome\Application\chrome.exe "
.	"https://www.google.com.sg/search?q=" Query "&hl=en&gbv=2&tbs=isz:lt,islt:vga&"
.	"tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&"
.	"bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h"
	Run % GoogleImageSearch
}
return


;; utility functions

;; Safely copies-to-clipboard, restoring clipboard's original value after
;; Returns the captured clip text, or "" if unsuccessful after 4 seconds
CopyToClipboard()
{
    ; Wait for modifier keys to be released before we send ^C
    KeyWait LWin
    KeyWait Alt
    KeyWait Shift
    KeyWait Ctrl

    ; Capture to clipboard, then restore clipboard's value from before capture
    ExistingClipboard := ClipboardAll
    Clipboard =
    SendInput ^{Insert}
    ClipWait, 4
    NewClipboard := Clipboard
    Clipboard := ExistingClipboard
    if (ErrorLevel)
    {
        ;MsgBox, The attempt to copy text onto the clipboard failed.
        ;Tip("The attempt to copy text onto the clipboard failed.")
        return ""
    }
    return NewClipboard
}

;; Extracts an address from anywhere in str.
;; Recognized addresses include URLs, email addresses, domain names, Windows local paths, and Windows UNC paths
ExtractAddress(str)
{
    if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
        return match1
    if (RegExMatch(str, "S)(\w+@[\w.]+\.(com|net|org|gov|cc|edu|info|sg))", match))
        return "mailto:" . match1
    if (RegExMatch(str, "S)(www\.\S+)", match))
        return "http://" . match1
    if (RegExMatch(str, "S)(\w+\.(com|net|org|gov|cc|edu|info|sg))", match))
        return "http://" . match1
    if RegExMatch(str, "S)([a-zA-Z]:[\\/][^/:*?""<>|]+)", match)
        return match1
    if (RegExMatch(str, "S)(\\\\[\w\-]+\\.+)", match))
        return match1
	if InStr(str, "%APPDATA%")
		Return StrReplace(str, "%APPDATA%", A_AppData)
	return ""
}



; Use Ctrl + Alt + R to navigate to Registry Address RegEdit

;Open Regedit and navigate to RegPath.
;RegPath accepts both HKEY_LOCAL_MACHINE and HKLM formats.
RegJump(RegPath)
{
	;Must close Regedit so that next time it opens the target key is selected
	WinClose, Registry Editor ahk_class RegEdit_RegEdit

	If (SubStr(RegPath, 0) = "\") ;remove trailing "\" if present
		RegPath := SubStr(RegPath, 1, -1)

	;Extract RootKey part of supplied registry path
	Loop, Parse, RegPath, \
	{
		RootKey := A_LoopField
		Break
	}

	;Now convert RootKey to standard long format
	If !InStr(RootKey, "HKEY_") ;If short form, convert to long form
	{
		If RootKey = HKCR
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CLASSES_ROOT
		Else If RootKey = HKCU
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CURRENT_USER
		Else If RootKey = HKLM
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_LOCAL_MACHINE
		Else If RootKey = HKU
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_USERS
		Else If RootKey = HKCC
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CURRENT_CONFIG
	}

	;Make target key the last selected key, which is the selected key next time Regedit runs
	RegWrite, REG_SZ, HKCU, Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, LastKey, %RegPath%
	Run, Regedit.exe
}

^#r::
^!r::
	SendInput ^c
	Sleep 100 ;Wait for copied text to become available
	If InStr(Clipboard, "HKEY_")
		RegJump( Clipboard ) ;Go to Registry path
Return


reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: how to combine code below?

10 Sep 2020, 19:59

I want to use the same hotkey for ALL these searches, not 1 for google/folder search, and a Separate one for registry entries.
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: how to combine code below?

11 Sep 2020, 03:06

Ah, I get it. I 'd say it's done like this: (for the first hotkey, you can paste the same change in the second if you want)

Code: Select all

;; Go to anything that is in the currently selected text: URLs, email addresses, Windows paths, or just "Google it" USING WIN+Q
$#Q::
~LButton & RButton:: ; Open in existing Chrome Window
    ;Tip("Clipping...")  ;; include my mouse-tip library for this https://gist.github.com/2400547
    clip := CopyToClipboard()
    if (!clip) {
        return
    }
    addr := ExtractAddress(clip)
	
	
    if (!addr)
    {
		If InStr(clip, "HKEY_") {
			RegJump( clip ) ;Go to Registry path
			Return
		}
		
		; Google it
        ;Tip("Searching for [" SubStr(clip, 1, 50) "] ...")
        addr := "https://www.google.com/search?q=" . clip
    }
    else {
        ; Go to it using system's default methods for the address
        ;Tip("Going to " Substr(addr, 1, 25) " ...")
    }

    Run %addr%
    return

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(not tested)

Let me know if there is more.
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: how to combine code below?

16 Sep 2020, 07:01

looking good so far. If the HKEY directory is formatted a different way, how do I use the script to auto-fix and direct to the right place?

E.g.

Code: Select all

HKEY_LOCAL_MACHINE” > “SOFTWARE” > “Policies” > “Google” > “Chrome
Correct format should look like this:

Code: Select all

HKEY_LOCAL_MACHINE\Software\Policies\Google\Chrome
The other thing I could not overcome is:

For websites that do not end with .com (E.g. .co, .net, .org, or other IPV6 domains), my script runs a google search instead of attempting to run the URL. Any way around this issue? 🤔
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: how to combine code below?

16 Sep 2020, 07:55

This would do the reg key thingy:

Code: Select all

Clip := ":“HKEY_LOCAL_MACHINE” > “SOFTWARE” > “Policies” > “Google” > “Chrome"

20SubKeys := "O)(HKEY_\w+)[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?"
RegExMatch(Clip, 20SubKeys, oKeys)
CompleteKey := ""
Loop 20	; There are no more then 20 subpatterns in 20SubKeys.
{
	If !oKeys.Value[A_Index] {
		Break	; There are no more subpatterns.
	}
	
	CompleteKey .= oKeys.Value[A_Index] "\"
}
CompleteKey := RTrim(CompleteKey, "\")

MsgBox % CompleteKey
Or as a function to implement it easily:

Code: Select all

Clip := ":“HKEY_LOCAL_MACHINE” > “SOFTWARE” > “Policies” > “Google” > “Chrome"
MsgBox % CleanRegKey(Clip)
ExitApp

CleanRegKey(DirtyKey) {
	20SubKeys := "O)(HKEY_\w+)[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?[^\w]*(\w+)?"
	RegExMatch(DirtyKey, 20SubKeys, oKeys)
	CompleteKey := ""
	Loop 20	; There are no more then 20 subpatterns in 20SubKeys.
	{
		If !oKeys.Value[A_Index] {
			Break	; There are no more subpatterns.
		}
		
		CompleteKey .= oKeys.Value[A_Index] "\"
	}
	Return RTrim(CompleteKey, "\")
}
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: how to combine code below?

17 Sep 2020, 03:35

I have 2 questions:

1. is it possible to nest 2 functions together? If so, how do I do it? Mine below doesn't work:

Code: Select all

 if (!addr)
    {
		If InStr(clip, "HKEY_") {
		
			CleanRegKey(RegJump( clip )) ;Go to Registry path
			Return
		}

2. The 2nd method I tried was to clean the key before going back to the main script. However it does not work too:

Code: Select all

;Open Regedit and navigate to RegPath.
;RegPath accepts both HKEY_LOCAL_MACHINE and HKLM formats.
RegJump(RegPath)
{
	;Must close Regedit so that next time it opens the target key is selected
	WinClose, Registry Editor ahk_class RegEdit_RegEdit

	If (SubStr(RegPath, 0) = "\") ;remove trailing "\" if present
		RegPath := SubStr(RegPath, 1, -1)

	;Extract RootKey part of supplied registry path
	Loop, Parse, RegPath, \
	{
		RootKey := A_LoopField
		Break
	}

	;Now convert RootKey to standard long format
	If !InStr(RootKey, "HKEY_") ;If short form, convert to long form
	{
		If RootKey = HKCR
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CLASSES_ROOT
		Else If RootKey = HKCU
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CURRENT_USER
		Else If RootKey = HKLM
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_LOCAL_MACHINE
		Else If RootKey = HKU
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_USERS
		Else If RootKey = HKCC
			StringReplace, RegPath, RegPath, %RootKey%, HKEY_CURRENT_CONFIG
	}

	;Make target key the last selected key, which is the selected key next time Regedit runs
	CleanRegKey(RegPath)
	RegWrite, REG_SZ, HKCU, Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, LastKey, %RegPath%
	Run, Regedit.exe
}
Using this as example:

Code: Select all

HKEY_LOCAL_MACHINE” > “SOFTWARE” > “Policies” > “Google” > “Chrome
Appreciate your help, thanks!
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: how to combine code below?

17 Sep 2020, 04:15

I see, this is indeed possible. In addition to the right code I'll tell something more about how functions work. With nested functions you start with the function that does the final action. In this case the overall action you want to perform is RegJump(), isn't it?
So that's where you start:
RegJump(clip)
The next challange was cleaning clip so that RegJump() can read it. This is where CleanRegKey() comes in but my function takes some text as a parameter (I called it DirtyKey) and spits the clean text out. Just like (1+1) results to 2, CleanRegKey("DirtyText") reslults to "CleanText". And your RegJump(clip) was just needing that clean text instead of clip.

So long story short, switch RegJump and CleanRegKey:

Code: Select all

 if (!addr)
    {
		If InStr(clip, "HKEY_") {
			RegJump( CleanRegKey(clip) ) ;Go to Registry path
			Return
		}
    }
As for you second method, the way my function is made you have to store the result of CleanRegKey(RegPath) to a variable and then use that variable where it's needed:

Code: Select all

	;Make target key the last selected key, which is the selected key next time Regedit runs
	RegPath := CleanRegKey(RegPath)	; Sotore the result of CleanRegKey() in RegPath.
	RegWrite, REG_SZ, HKCU, Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, LastKey, %RegPath%
	Run, Regedit.exe
The way you used CleanRegKey() in method2 is also possible but then CleanRegKey() needs to define it's parameter DirtyText as ByRef. That way the function can change the content of the variable outside it's function but it makes method1 unable to use.

Code: Select all

; Other way of defining the function that will allow your method2 to work but it breaks method1.
CleanRegKey(ByRef DirtyKey) {
Let me know if you want something cleared up or want to know something more.
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: how to combine code below?

17 Sep 2020, 17:50

Wow! Your explanation is very detailed and helpful! thank you so much!

I just have one last question. For Regex, do you know of any website/app I can use to come out with the regex parameters by simply giving the input & output?

Roughly understand how it works, but always have trouble getting it to clean the correct characters out.

Thanks again!
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: how to combine code below?

18 Sep 2020, 02:17

I use the Regex Tester from rbrtryn to test the expressions in combination with the RegEx Quick reference from the AHK help file. Especialy the Regex Tester allowed me to really start building regular expressions. I do not know of a script like you mentioned, would be really cool though.
Regex Tester: https://autohotkey.com/board/topic/81045-regular-expression-tester/ (first post, behind the spoiler)
RegEx Quick reference: https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: how to combine code below?

18 Sep 2020, 19:39

very cool. gona try out the tester you recommended. Thanks!
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: how to combine code below?

21 Sep 2020, 18:20

Hello,

I recently tried the code again on a new directory (yes it exists on my registry), but it did not navigate there successfully. Any idea why?

Code: Select all

Computer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Identity\Identities
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

dyatactic

22 Sep 2020, 07:36

Ah, well. The Regular Expression was searching for 'word characters' and non-'word characters' followed by more optional 'word characters'. I guess the . (dot) is not a 'word character'. I just discovered that a reg-key can contain pretty much any character so that makes it quite hard filter weird characters since they can legimatly be part of a key name. I can just broaden the allowence of the RegEx and hope it works for almost all keys.

Another option would be, if you only use two or three kinds of regkey formatting, to make a RegEx specificaly for those two/three formats. Though, when someone makes his own key format (say HKEY_CURRENT_USER >>>> Software >>>> Microsoft >>>> etc.) CleanRegKey() won't clean that one properly.

This is the new CleanRegKey() function. I added the dot as a possible character too.

Code: Select all

CleanRegKey(DirtyKey) {
	Static 20SubKeys := ""
	
	If !20SubKeys {	; This part only runs the first time the function is called.
		20SubKeys := "O)(HKEY_[\w|\.]+)"
		RegEx1     := "[^\w]*"
		RegEx2     := "([\w|\.]+)?"	; Now allows word characters and a dot (\.). Feel free to add more characters, seperate them with a pipe (|).
		Loop 19
			20SubKeys .= RegEx1 . RegEx2
		
	}
	
	RegExMatch(DirtyKey, 20SubKeys, oKeys)	; Store an array with matches in oKeys.
	CompleteKey := ""
	Loop 20	; There are no more then 20 subpatterns in 20SubKeys.
	{
		If (oKeys.Value[A_Index] = "") {
			Break	; There are no more subpatterns.
		}
		
		CompleteKey .= oKeys.Value[A_Index] "\"
	}
	Return RTrim(CompleteKey, "\")
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 116 guests