0

Noob here I'm using autohot key to sorty by date modified whenever im in file explorer.

Problem is that in some folders if the order of the columns is different my script doesn't work.

Does anyone know if there's a unique code for date modified?

2
  • Any particular Operating System? – Moab Apr 18 '18 at 20:06
  • Apologies Windows 7 64Bit – AMSGH Apr 19 '18 at 21:22
2

This can be done using Accessibility functions to find the button (that you would click on) in the Explorer window, and then execute it programmatically.

Unfortunately, it appears that the link to download the accessibility library (acc.ahk) is no longer valid (https://autohotkey.com/board/topic/77303-acc-library-ahk-l-updated-09272012/), so you may need to find a copy on one of the other forum pages (maybe start on the new forum) or on github somewhere, and then include the additional extended functions below.

For the code below, figuring out how to determine which control name and childPath to use as arguments to the accessibility functions is beyond the scope of explanation here, but those can be determined using Spy++ and the Accessibility Info Viewer.

When executed, this code has the same effect as clicking the mouse on the button on the sorting column header, so if it's sorted one way, executing it again will sort the other way.

If you wanted to sort by one of the other fields, you could also replace "Date" in the myName argument with one of the other columns to sort by (Name, Type, etc.).

This code requires acc.ahk as noted above and is tested working to sort by date for an Explorer window. It is also robust against the column names changing or moving around because the call to acc_doIfChildName() iterates through the list of column names that are present, until it finds the myName argument that was passed in, and then executes the defaultAction for that button. If it doesn't find a matching button it will just return False for the foundAndExecuted return value.

#Persistent
#SingleInstance, Force
#NoEnv

;-----------------------------------------------------------------------------------------------------------------------
; Press F2 to Sort by date in any active Explorer window
;-----------------------------------------------------------------------------------------------------------------------
#IfWinActive ahk_class CabinetWClass ahk_exe Explorer.exe
F2::
    Tooltip % "Sorting by date..."  ; acc functions can take a second, show user status in progress...

    myHwnd:=ControlGet(cmd:="Hwnd", val:="", ctl:="ShellView", "ahk_class CabinetWClass ahk_exe explorer.exe")
    retVal:=acc_doIfChildName(myName:="Date", childPath:="4.1.4.1", statusBarObj:="ahk_id" myHwnd, loopBackward:=False)    

    Tooltip   ; clear status Tooltip
Return
#IfWinActive

ExitApp

#Include acc.ahk

;-----------------------------------------------------------------------------------------------------------------------
; acc_doIfChildName() - Iterate all children looking for the correct childname to try and execute...
;-----------------------------------------------------------------------------------------------------------------------
acc_doIfChildName(myName, childPathArg, myWinTitleOrObj, loopBackward:=False, startIndex:=1) {

    ;-------------------------------------------------------------------------------
    ; Set for multiple dialogs below during debugging
    ;-------------------------------------------------------------------------------
    debug:=False

    Loop, % childCount:=acc_get("childCount", childPathArg, childID:=0, myWinTitleOrObj) {

        If acc_doIfName(myName, childPath:=childPathArg "." (loopBackward ? childCount+1-A_Index : A_Index), myWinTitleOrObj)       ; Loop backwards to find faster since it will be at the end
            Return foundAndExecuted:=True
        Else If debug {
            ctlName :=acc_get("name",  childPath, childID:=0, myWinTitleOrObj)
            ctlValue:=acc_get("value", childPath, childID:=0, myWinTitleOrObj)
            MsgBox % "A_Index=" A_Index "`n" . "ctlName=" ctlName "`n" . "ctlValue=" ctlValue "`n"
        }
    }

    Return foundAndExecuted:=False
}


;-----------------------------------------------------------------------------------------------------------------------
; acc_doIfName() - doDefaultAction() if searchItemName matches the control name at childPath
;-----------------------------------------------------------------------------------------------------------------------
acc_doIfName(myName, childPath, myWinTitle) {
    Return acc_doIf("Name", myName, childPath, myWinTitle)
}



;-----------------------------------------------------------------------------------------------------------------------
; acc_doIf() - doDefaultAction() if field and value match correctly for childPath
;-----------------------------------------------------------------------------------------------------------------------
acc_doIf(accField, searchItem, childPath, myWinTitle) {

    foundAndExecuted:=False
    Try { 
        If ((ctlName:=acc_get(accField, ChildPath, childID:=0, myWinTitle))=searchItem) {

            oAcc := acc_Get("Object", ChildPath, ChildID, myWinTitle)
            oAcc.accDoDefaultAction(ChildID)
            foundAndExecuted:=True
        }
    }

    Return foundAndExecuted
}



;-----------------------------------------------------------------------------------------------------------------------
; ControlGet()
;-----------------------------------------------------------------------------------------------------------------------
ControlGet(Cmd, Value:="", Control:="", WinTitle:="", WinText:="", ExcludeTitle:="", ExcludeText:="") {
    ControlGet, v, % Cmd, % Value, % Control, % WinTitle, % WinText, % ExcludeTitle, % ExcludeText
    Return, v
}
5
  • I tried the new script and acc.ahk. It seems i get an error in acc.ahk. Acc_ObjectFromPoint(ByRef idChild = , x = , y = ) Error: Unsupported parameter default – AMSGH May 15 '18 at 2:19
  • Update. I think i fixed that line. Now i get an error on line 73 – AMSGH May 15 '18 at 2:52
  • Update working now... Don't know how. – AMSGH May 15 '18 at 3:07
  • Update - Code works in every folder except when i'm in the downloads folder...C:\Users\username\Downloads – AMSGH May 15 '18 at 3:18
  • Can you tell if functionality changes when the horizontal and/or vertical scroll bars are visible (vs. no visible scroll bar?). I looked at the order of accessibility objects and it looks like scroll bars will get inserted into the childPath listing when they are visible (ahead of the sorting column buttons). There would probably have to be additional work to enumerate past those to the write childPath if that's the problem. – JJohnston2 May 18 '18 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.