Guest User

NavigateToSiblingDir mod

a guest
Jul 24th, 2018
102
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.  
  4. NavigateToSiblingDir(UpDown)
  5. {
  6.     oShell := ComObjCreate("Shell.Application")
  7.     WinGet, hWnd,, A
  8.     for oWin in oShell.Windows
  9.     {
  10.         if (hWnd = oWin.hwnd)
  11.         {
  12.             oFolder := oWin.Document.Folder
  13.             startDirName  := oFolder.Self.Name
  14.             parentDirPath := oFolder.ParentFolder.Self.Path
  15.             break
  16.         }
  17.     }
  18.  
  19.     ;兄弟フォルダを格納した配列。キーにフォルダ名、値にパス。あとでパスの取得に使う
  20.     siblingFolders := {}
  21.     ;複数のフォルダ名が格納された`nで区切られた文字列。エクスプローラー風のソートに使う
  22.     sortString :=
  23.  
  24.     for item in oShell.Namespace(parentDirPath).Items
  25.     {
  26.         ;item.IsFolderだとZipファイルもフォルダ扱いになるので変更
  27.         if !InStr(FileExist(item.Path), "D")
  28.             continue
  29.         siblingFolders[item.Name] := item.Path
  30.         sortString .= item.Name "`n"
  31.     }
  32.    
  33.     sortString := RTrim(sortString, "`n")
  34.     ;StrCmpLogicalWを使ってソート
  35.     Sort, sortString, F SortLikeExplorer
  36.  
  37.     ;ソート結果に基づいてstartDirName(現在のフォルダ)を基準に直前と直後の兄弟フォルダ名を取得
  38.     Loop, Parse, sortString, `n
  39.     {
  40.         if (found && nextSiblingName := A_LoopField)
  41.             break
  42.         if (A_LoopField = startDirName && found := true)
  43.             prevSiblingName := prev
  44.         prev := A_LoopField
  45.     }
  46.     if (UpDown && prevSiblingName)
  47.         oWin.Navigate(siblingFolders[prevSiblingName])
  48.     if (!UpDown && nextSiblingName)
  49.         oWin.Navigate(siblingFolders[nextSiblingName])
  50. }
  51.  
  52. SortLikeExplorer(lhs, rhs)
  53. {
  54.     Return DllCall("shlwapi\StrCmpLogicalW", "Str", lhs, "Str", rhs)
  55. }
Add Comment
Please, Sign In to add comment