Advertisement
Guest User

NavigateToSiblingDir mod2

a guest
Jul 26th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;original
  2. ;https://github.com/q2apro/ahk-snippets/blob/master/explorer-navigate-sibling-folder.ahk
  3. ;https://pastebin.com/sZVcY14F
  4.  
  5. #If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
  6. ^Home::
  7. ^End::
  8. ^pgUp::
  9. ^pgDn:: NavigateToSiblingDir(A_ThisHotkey)
  10.  
  11. NavigateToSiblingDir(key)
  12. {
  13.     oShell := ComObjCreate("Shell.Application")
  14.     WinGet, hWnd,, A
  15.     for oWin in oShell.Windows
  16.     {
  17.         if (hWnd = oWin.hwnd)
  18.         {
  19.             oFolder := oWin.Document.Folder
  20.             startDirName  := oFolder.Self.Name
  21.             parentDirPath := oFolder.ParentFolder.Self.Path
  22.             break
  23.         }
  24.     }
  25.  
  26.     ;兄弟フォルダを格納した配列。キーにフォルダ名、値にパス。あとでパスの取得に使う
  27.     siblingFolders := {}
  28.     ;複数のフォルダ名が格納された`nで区切られた文字列。エクスプローラー風のソートに使う
  29.     sortString :=
  30.  
  31.     for item in oShell.Namespace(parentDirPath).Items
  32.     {
  33.         ;item.IsFolderだとZipファイルもフォルダ扱いになるので変更
  34.         if !InStr(FileExist(item.Path), "D")
  35.             continue
  36.         siblingFolders[item.Name] := item.Path
  37.         sortString .= item.Name "`n"
  38.     }
  39.    
  40.     sortString := RTrim(sortString, "`n")
  41.     ;StrCmpLogicalWを使ってソート
  42.     Sort, sortString, F SortLikeExplorer
  43.  
  44.     ;ソート結果に基づいてstartDirName(現在のフォルダ)を基準に直前と直後の兄弟フォルダ名を取得
  45.     ;最初と最後の兄弟フォルダ名も取得
  46.     Loop, Parse, sortString, `n
  47.     {
  48.         if (A_Index = 1)
  49.             FirstSiblingName := A_LoopField
  50.         if (found && nextSiblingName := A_LoopField)
  51.             found := false
  52.         if (A_LoopField = startDirName && found := true)
  53.             prevSiblingName := prev
  54.         prev := A_LoopField
  55.     }
  56.        
  57.     if (key = "^Home")
  58.         oWin.Navigate(siblingFolders[FirstSiblingName])
  59.     if (key = "^End")
  60.         oWin.Navigate(siblingFolders[prev])
  61.     if ((key = "^pgup") && prevSiblingName)
  62.         oWin.Navigate(siblingFolders[prevSiblingName])
  63.     if ((key = "^pgdn") && nextSiblingName)
  64.         oWin.Navigate(siblingFolders[nextSiblingName])
  65. }
  66.  
  67. SortLikeExplorer(lhs, rhs)
  68. {
  69.     Return DllCall("shlwapi\StrCmpLogicalW", "Str", lhs, "Str", rhs)
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement