Sign in to follow this  
Followers 0
prologician

"New Folder" Hotkey via COM Manipulation

1 post in this topic

I decided a few days ago to try my hand at creating a single hotkey to handle creating a new folder inside the Windows shell (yes, I am aware of such answers like "!fwf", but these are limited to Explorer windows. They fail to work on the Desktop, where there is no "File" menu, and having a file selected alters the popup menu). As such, I wanted to try to obtain a solution by using COM objects instead.

I've sorta hit a wall, however. Mainly, I don't know how to get access to the Windows Desktop COM object. I came across this post, but it never had a follow-up response to it (and I don't really know how to deal with COM objects through explicit DLL calls...).

Here's what I have thus far (it's by no means perfect, but it at least gives an idea of what I'm doing). I figure that, if I could get access to the Desktop object, that the latter portion of this code should be reusable.

opt("WinTitleMatchMode", 4)

HotKeySet("^n", "shellNewFolder")

while 1
    sleep(100)
wend

func shellNewFolder()
    local $hwnd = WinGetHandle("[ACTIVE]")  ;If there's no handle, the comparison later fails.
    local $activeWindow = 0

    local $oShell = ObjCreate("shell.application")    
    if IsObj($oShell) then
        ; $oshell is a Shell

        local $oShellWindows = $oShell.windows()
        if isobj($oShellWindows) then
            ; $oShellWindows is a ShellWindows

            for $oWindow in $oShellWindows
                if hwnd($owindow.HWND) = $hwnd then
                    $activeWindow = $oWindow
                    exitloop
                endif
            next
        endif
    endif
    if $activeWindow = 0 then
        HotKeySet("^n")
        Send("^n")
        HotKeySet("^n", "shellNewFolder")
        return
    endif
    ; $activeWindow is "an InternetExplorer object that represents the Shell window"
    ; msgbox(0, "Debug!", "selected window is a shell window.")

    local $error = 0
    local $oActiveDoc = $activewindow.document
    if not isobj($oActiveDoc) then
        $error = 1
    else
        ; $oActiveDoc is at least a IShellFolderViewDual2

        local $oActiveFolder = $oActiveDoc.folder
        if not isobj($oActiveFolder) then
            $error = 1
        else
            ; $oActiveFolder is at least a Folder3....
            local $activeFolderPath = $oActiveFolder.self.path
            if StringRight($activeFolderPath, 1) <> '\' then $activeFolderPath &= '\'

            ; Create the new folder I need.
            local $counter = 1
            local $newFolderName = "New Folder"
            while FileExists($activeFolderPath & $newFolderName)
                $counter += 1
                $newFolderName = "New Folder (" & $counter & ")"
            wend

            if not DirCreate($activeFolderPath & $newFolderName) then
                $error = 1
            else
                local $newFolder = 0

                do
                    local $oFolderItems = $oActiveFolder.items
                    if not isobj($oFolderItems) then
                        $error = 1
                    else
                        for $oItem in $oFolderItems
                            if $oItem.name = $newfoldername then
                                $newFolder = $oItem
                                exitloop
                            endif
                        next
                    endif
                until $newfolder <> 0 or $error

                if not $error then
                    $oActiveDoc.selectitem($newFolder, 7)   ; Deselect all except $item + put into edit mode.
                endif
            endif
        endif
    endif
    
    if $error then
        msgbox(0x1010, "New Folder", "Unable to create new folder.")
    endif
endfunc

Thanks in advance for any assistance on this! :)

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0