I implemented it as Ankit suggested. The implementation got a little bit trickier because the rewritten path cannot be used to paste the actual file (not the path as text) anymore. I have worked around this by detecting situations where I guess that only the text representation will be used. Technically, as I understand the Windows clipboard implementation, it would even be possible to only change the text representation of the path but I leave that task open to someone else. Not sure if that can be done with AutoHotKey.
Because of this, the AutoHotKey script was written for the following workflow:
- Do what you usually do to copy a path.
- We try to do what is necessary to have a clean path in the clipboard.
- If we cannot do it by default (we don’t know that it is save), we do nothing and you have to manually make the path in the clipboard clean by pressing Shift+Super+C.
For details have a look at the code:
#SingleInstance force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, RegEx
;; Use this until I hit the first issue then document here and set back to default value.
SetDefaultMouseSpeed, 0
;; Copy clean file/directory path to clipboard (use forward slashes as file separators) {{{
;; https://stackoverflow.com/questions/1589930/so-what-is-the-right-direction-of-the-paths-slash-or-under-windows/1589959#1589959
;; WARNING: This clipboard substitution has the issue that after the substitution, pasting the file does not work anymore!!
;; Because of this, we don’t run the substitution OnClipboardChange globally but only when we consider it save and otherwise using a (manual) shortcut.
;; Situations where we know it is save:
;; * Double Commander calls CopyFullNamesToClip.
;; * Location bar in Explorer has focus. See limitations below!
;; The expected workflow is:
;; 1. Do what you usually do to copy a path.
;; 2. We try to do what is necessary to have a clean path in the clipboard.
;; 3. If we cannot do it by default (we don’t know that it is save), we do nothing and you have to manually make the path in the clipboard clean by pressing Shift+Super+C.
;; Ref: Get-CleanPath in ../../MS_Shell/Modules/ypidDotfiles/ypidDotfiles.psm1
;; Seems up to and including Windows 10, UNC paths with forward slashes don’t work.
;; At least //files.example.org/home and \\files.example.org/home and //files.example.org\home don’t work.
clean_path_in_clipboard() {
If (RegExMatch(Clipboard, "^(?i)(?:[a-z]:)?\\[^\\]")) {
StringReplace, Clipboard, Clipboard,\,/, All
}
Return
}
;; Shift+Super+C | Clean file/directory path in clipboard {{{
+#C::
; ClipSaved := ClipboardAll
; Clipboard =
; Send ^c
;; Ensure that we are only working on text.
; ClipWait
; currentPath =
; WinGetClass explorerClass, A
; ControlGetText currentPath, Edit1, ahk_class %explorerClass%
; msgbox %currentPath%
; If (ErrorLevel) {
; Clipboard := ClipSaved
; MsgBox, 48, Clipboard copy warning, Failed to copy to clipboard.
; Return
; }
clean_path_in_clipboard()
Return
;; }}}
;; Shift+Alt+C | Hook Double Commander calls to CopyFullNamesToClip and run clean_path_in_clipboard afterwards.
;; We can "safely" do this because when CopyFullNamesToClip is called, the user wants to copy the path as text.
#UseHook
#IfWinActive ahk_exe doublecmd.exe
+!c::
Send +!c
clean_path_in_clipboard()
Return
#IfWinActive
#UseHook off
OnClipboardChange:
;; Fix file path when in transit in Explorer (or Double Commander).
;; Ensure that we are only working on text.
If (WinActive("ahk_exe (?i)(?:explorer.exe|doublecmd.exe)") and A_EventInfo == 1) {
;; Location bar in Explorer has focus.
;; Run clean_path_in_clipboard after copying text to clipboard in Explorer when cursor is above "Location bar" known as Edit1 (bad programming/variable naming M$??).
;; Technically this is not 100 % bulletproof because you could do the copy to clipboard with Ctrl+L followed Ctrl+C while the cursor focuses some other control.
If (WinActive("ahk_exe (?i)(?:explorer.exe)")) {
MouseGetPos, , , , control_below_cursor
If (control_below_cursor == "Edit1") {
clean_path_in_clipboard()
}
}
;; We cannot do this globally, see WARNING above.
; clean_path_in_clipboard()
}
return
;; }}}
(This is also tracked on GitHub: https://github.com/ypid/dotfiles/blob/master/windows/neo-vars/source/custom.ahk)
Also thanks to Katharsas for opening a feedback request at "Feedback Hub App for Windows 10". Native support would be preferred but I don’t think Microsoft will do that in any reasonable timeframe. So we just have to do it ourselves by this kind of hacking. Forward slashes are the better cross platform path separator. Never let Microsoft dictate you anything. Enjoy ;-)
1I added this as suggestion in the Feedback Hub App for Windows 10. It needs upvotes there though... – Katharsas – 2017-06-23T01:23:46.473
@Katharsas Can you share a link or does Microsoft not want to have it public ;-) ? – ypid – 2018-12-08T01:36:56.100
Can you not add the code at the java level of the logic that uses the path you paste to set it as another variable after replacing all
\
from one variable with/
and then you can just copy and paste accordingly and be assured the java code transforms accordingly? – Pimp Juice IT – 2018-12-08T06:30:47.930you're trying to solve the wrong problem. There are many other tools to copy every type of paths like path copy copy or path copy. What's a good tool/addin for Windows to copy a file's path?
– phuclv – 2018-12-08T10:26:41.437Vote link: https://aka.ms/AA3kvsh Needs to be opened with Feedback Hub App it seems (browser should ask you). I only had created draft before for some reason.
– Katharsas – 2018-12-12T20:25:38.8231
To achieve this you can use a simple Autohotkey script, that script will be activated either by a Key combination or automatically whenever focus goes to address field of Explorer. And it will access clipboard and alter all slashes. When you paste it to java code you will have required format
– Ankit – 2012-09-24T13:03:55.140Thanks, Looks interesting. However I'm on a work computer and would prefer a solution that does not involve install new programs. – Gal – 2012-09-24T13:15:25.033
@Gal: http://www.autohotkey.com/download/ click "ZIP archive" (portable version)
– Ryan_S – 2012-09-25T03:51:34.587@Gal Autohotkey can generate an .exe version for it script, and that exe will work on a computer without any installing of it self and without Autohotkey installed. – Ankit – 2012-10-04T14:32:43.957