Code: Select all
#Requires AutoHotkey v2.0
#NoTrayIcon
#SingleInstance Force
global pathObj := {}
Persistent
OnClipboardChange(ClipChanged)
return
#e::
{
try {
; MsgBox(pathObj.type . "~~~" . pathObj.value . "!!!" . HasProp(pathObj, 'type'))
if (HasProp(pathObj, 'type')) {
switch pathObj.type {
case 'dir':
OpenExplore(pathObj.value)
case 'file': (
OpenExplore(pathObj.value, pathObj.file)
)
case 'url':
Run(pathObj.value)
default:
Run("::{20d04fe0-3aea-1069-a2d8-08002b30309d}")
}
} else {
Run("::{20d04fe0-3aea-1069-a2d8-08002b30309d}")
}
}
catch Error {
MsgBox('Error')
; Run("::{20d04fe0-3aea-1069-a2d8-08002b30309d}")
}
}
; Open or activate Explore through a folder path, and focus file if you set
OpenExplore(path, focusFile := "") {
isActived := 0
SetTitleMatchMode(3)
If WinExist(path) {
WinActivate
isActived := 1
}
if(isActived && focusFile = "") {
return
}
Focus(delay := 100){
if(focusFile){
Sleep(delay)
Send(focusFile)
}
}
Activate(window) {
try explorer_path := window.Document.Folder.Self.Path
If (explorer_path = "") {
return 'continue'
}
; 如果存在同路径
if (RegExMatch(explorer_path, StrReplace(path, "\", "\\"))) {
if(isActived = 0){
WinActivate("ahk_id " WinGetID(explorer_path))
isActived := 1
}
if(window.Document.FocusedItem.Name != focusFile){
Focus()
}
return 'break'
}
}
; 如果没有激活的窗口
if (LoopWindows(Activate) != "break") {
; Open new explore window
Run("explore " . path)
Focus()
}
}
LoopWindows(fn) {
If WinExist("ahk_class CabinetWClass") ; explorer
{
loopResult := ""
for window in ComObject("Shell.Application").Windows
{
; return break or you want join text
output := fn(window)
switch output {
case 'break':
loopResult .= output
break
case 'continue':
continue
default:
loopResult .= output
}
}
return loopResult
}
}
ClipChanged(Type) {
global
clipSaved := Trim(A_Clipboard, '`"')
isFileOrDir := FileExist(clipSaved) != "" && FileGetAttrib(clipSaved) != ""
; MsgBox(clipSaved isFileOrDir)
switch 1 {
case (isFileOrDir && DirExist(clipSaved) != ""):
pathObj := { type: 'dir', value: clipSaved }
case isFileOrDir:
pathObj := { type: 'file', value: RegExReplace(clipSaved, "i)\\[^\\]+$"), file: RegExReplace(clipSaved, ".+\\(?=[^\\]+$)") }
case (RegExMatch(clipSaved, "(?:https?:\/\/|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(?:\/\S*)?$") > 0):
pathObj := { type: 'url', value: clipSaved }
default:
pathObj := { value: clipSaved }
}
if (HasProp(pathObj, 'type')) {
ToolTip(pathObj.type . ": " . pathObj.value)
Sleep(1000)
ToolTip() ; 关闭提示.
}
}