Open file or folder path

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
foguetes
Posts: 19
Joined: 19 Oct 2018, 04:58

Open file or folder path

29 Jul 2019, 09:32

I'm trying to create a keyboard shortcut to open a selected folder or file path.

I'm using Windows key + F1 to open a folder and Windows key + F2:

Code: Select all

; open path in explorer

#F1::

SendInput, ^c 			     ; copy current selection to clipboard
ClipWait, 30 
Run, explore %clipboard%
return

; open file path in explorer

#F2::

SendInput, ^c 			     ; copy current selection to clipboard
ClipWait, 30 
Run, open %clipboard%
return

Sometimes it works but most times I get this error:
W8jfDR5[1].png
W8jfDR5[1].png (21.98 KiB) Viewed 7837 times
I can't figure out what makes it work or not.
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Open file or folder path

29 Jul 2019, 15:10

Try:

Code: Select all

; open path in explorer

#F1::
Clipboard:=""
SendInput, ^c 			     ; copy current selection to clipboard
ClipWait, 1 ; wait max 1 second
if !ErrorLevel
     Run, explore %clipboard%
return

; open file path in explorer

#F2::
Clipboard:=""
SendInput, ^c 			     ; copy current selection to clipboard
ClipWait, 1
if !ErrorLevel
    Run, %clipboard%
return
foguetes
Posts: 19
Joined: 19 Oct 2018, 04:58

Re: Open file or folder path

30 Jul 2019, 05:19

The problem was actually being caused by the fact that double clicking the text in Chrome to select the entire line would copy a line break to the clipboard.

I also noticed that using the open command on folders has the same behavior as using the explore command.

So I added a Regex to remove line breaks from the clipboard and removed the explore command shortcut:

Code: Select all

; open path

#F1::
Clipboard:=""
SendInput, ^c 			     ; copy current selection to clipboard
ClipWait, 1
original := Clipboard 
stripped := RegexReplace(original, "`r`n", "") 
if !ErrorLevel
     Run, open %stripped%
return
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Open file or folder path

30 Jul 2019, 07:24

It was very unclear what you are doing. Nice that you on the right way.
One thing though: ErrorLevel is usually used immediately after it was (potentially) assigned. Here I believe is no problem, but if you had e.g. a timer where ErrorLevel could be assigned too, then it could behave in an unpredicted way. So use better:

Code: Select all

ClipWait, 1
;Sleep, -1 ; this line is only for extreme cases!
if !ErrorLevel {
     original := Clipboard 
     stripped := RegexReplace(original, "`r`n", "") 
     Run, open %stripped%
}
bye!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mstrauss2021 and 329 guests