1

Can someone tell me how I can get all the selected files on Windowsd desktop in Python? I've been searching for a way to do it and I can't find anyone. The only one I found is for C#, but I don't code in C#, so I don't even know if it works: Get list of selected files from Windows Desktop (if someone understands it and could explain/convert it, that'd be appreciated too). I've found something very near of this, but I can only make it get the number of seleted files, not their path, as I'd like:

import ctypes
from commctrl import LVM_GETITEMCOUNT,LVM_GETSELECTEDCOUNT
#The LVM_GETITEMCOUNT came with the script, I got the other one from Microsoft documentation about SendMessage(), and both are near, but none returns the paths, only numbers
import pywintypes
import win32gui

GetShellWindow = ctypes.windll.user32.GetShellWindow

def get_desktop():
    """Get the window of the icons, the desktop window contains this window"""
    shell_window = GetShellWindow()
    shell_dll_defview = win32gui.FindWindowEx(shell_window, 0, "SHELLDLL_DefView", "")
    if shell_dll_defview == 0:
        sys_listview_container = []
        try:
            win32gui.EnumWindows(_callback, sys_listview_container)
        except pywintypes.error as e:
            if e.winerror != 0:
                raise
        sys_listview = sys_listview_container[0]
    else:
        sys_listview = win32gui.FindWindowEx(shell_dll_defview, 0, "SysListView32", "FolderView")
    return sys_listview

def _callback(hwnd, extra):
    class_name = win32gui.GetClassName(hwnd)
    if class_name == "WorkerW":
        child = win32gui.FindWindowEx(hwnd, 0, "SHELLDLL_DefView", "")
        if child != 0:
            sys_listview = win32gui.FindWindowEx(child, 0, "SysListView32", "FolderView")
            extra.append(sys_listview)
            return False
    return True

def get_item_count(window):
    return win32gui.SendMessage(window, LVM_GETSELECTEDCOUNT)

desktop = get_desktop()
print(get_item_count(desktop))

I've searched on the commands that can be sent to a window, but I didn't find anyone to get the path of the selected items (maybe I missed one?).

A way I found of getting the selected files from Windows Explorer windows, but now from the desktop: https://stackoverflow.com/a/21250927/8228163.

Any help (with any Windows, preferably 7) is much appreciated. Thanks in advance!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Edw590
  • 447
  • 1
  • 6
  • 23
  • This question has an answer [here](https://stackoverflow.com/a/52959617/3005834) – Bro Nicholas Oct 31 '18 at 23:51
  • Actually that's a mixed answer (written by me) from 2 other answers, but that only gets the paths from files on Windows Explorer windows, not from the desktop (as it isn't a window). I can't make it get them from the desktop. – Edw590 Nov 01 '18 at 00:02
  • The desktop is just an Explorer window, and provides a programming interface. It boils down to calling [IFolderView::Items](https://learn.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-ifolderview-items), filtering on the `SVGIO_SELECTION` flag. Most of the code required is presented in this blog entry: [Manipulating the positions of desktop icons](https://blogs.msdn.microsoft.com/oldnewthing/20130318-00/?p=4933). You'll want to use a Python module that exposes COM in a natural way. – IInspectable Nov 01 '18 at 07:16
  • From the 2nd link that IInspectable posted, you need to translate the code of "FindDesktopFolderView()" to python. Once you have the view object, you can use [my code](https://stackoverflow.com/a/43968304/7571258) to enumerate the selected files of the view object (starting from comment _"# Get an IDataObject that contains the items of the view"_). – zett42 Nov 01 '18 at 07:52

0 Answers0