Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;original
- ;https://github.com/q2apro/ahk-snippets/blob/master/explorer-navigate-sibling-folder.ahk
- ;https://pastebin.com/sZVcY14F
- #If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
- ^Home::
- ^End::
- ^pgUp::
- ^pgDn:: NavigateToSiblingDir(A_ThisHotkey)
- NavigateToSiblingDir(key)
- {
- oShell := ComObjCreate("Shell.Application")
- WinGet, hWnd,, A
- for oWin in oShell.Windows
- {
- if (hWnd = oWin.hwnd)
- {
- oFolder := oWin.Document.Folder
- startDirName := oFolder.Self.Name
- parentDirPath := oFolder.ParentFolder.Self.Path
- break
- }
- }
- ;兄弟フォルダを格納した配列。キーにフォルダ名、値にパス。あとでパスの取得に使う
- siblingFolders := {}
- ;複数のフォルダ名が格納された`nで区切られた文字列。エクスプローラー風のソートに使う
- sortString :=
- for item in oShell.Namespace(parentDirPath).Items
- {
- ;item.IsFolderだとZipファイルもフォルダ扱いになるので変更
- if !InStr(FileExist(item.Path), "D")
- continue
- siblingFolders[item.Name] := item.Path
- sortString .= item.Name "`n"
- }
- sortString := RTrim(sortString, "`n")
- ;StrCmpLogicalWを使ってソート
- Sort, sortString, F SortLikeExplorer
- ;ソート結果に基づいてstartDirName(現在のフォルダ)を基準に直前と直後の兄弟フォルダ名を取得
- ;最初と最後の兄弟フォルダ名も取得
- Loop, Parse, sortString, `n
- {
- if (A_Index = 1)
- FirstSiblingName := A_LoopField
- if (found && nextSiblingName := A_LoopField)
- found := false
- if (A_LoopField = startDirName && found := true)
- prevSiblingName := prev
- prev := A_LoopField
- }
- if (key = "^Home")
- oWin.Navigate(siblingFolders[FirstSiblingName])
- if (key = "^End")
- oWin.Navigate(siblingFolders[prev])
- if ((key = "^pgup") && prevSiblingName)
- oWin.Navigate(siblingFolders[prevSiblingName])
- if ((key = "^pgdn") && nextSiblingName)
- oWin.Navigate(siblingFolders[nextSiblingName])
- }
- SortLikeExplorer(lhs, rhs)
- {
- Return DllCall("shlwapi\StrCmpLogicalW", "Str", lhs, "Str", rhs)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement