5

I'm trying to get the number of items on the desktop using win32gui in python 2.7.

The following code: win32gui.SendMessage(win32gui.GetDesktopWindow(), LVM_GETITEMCOUNT) returns zero and I have no idea why.

I wrote win32api.GetLastError() afterwards and it returned zero either.

Thanks in advance.

EDIT: I need to use this method because the final goal is to get the positions of the icons, and it's done by a similar method. So I just wanted to make sure that I know how to use this method. Also, I think that it can give a different output than listing the content of desktop (can it?). And thirdly, my sources of how to get the positions suggested to do it this way - http://www.codeproject.com/Articles/639486/Save-and-restore-icon-positions-on-desktop for example.

EDIT2:

Full code for getting the count (doesn't work for me):

import win32gui
from commctrl import LVM_GETITEMCOUNT
print win32gui.SendMessage(win32gui.GetDesktopWindow(), LVM_GETITEMCOUNT)

Thanks again!

SOLUTION:

import ctypes
from commctrl import LVM_GETITEMCOUNT
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_GETITEMCOUNT)

desktop = get_desktop()
get_item_count(desktop)
0

2 Answers 2

1

You can use os.listdir:

import os

len(os.listdir('path/desktop'))
Sign up to request clarification or add additional context in comments.

17 Comments

I actually want to get the positions of the icons and before that I need the number of icons. I have to use the same method for the positions anyway. Also I'm not sure that these methods will always return the same output - it's a good question. I primarily use this method because my sources suggested so (codeproject.com/Articles/639486/… for example)
what actual code are you using? listdir returns everything on my desktop, I don't use so windows but I see people using commctrl.LVM_GETITEMCOUNT?
Listdir doesn't help me to get the positions. And to get the positions I need to understand the method of win32gui and messages.
well your question was how to get the count so I did answer your question, editing and changing a question is not the way to go
OK the answer is in the second question here, stackoverflow.com/questions/1669111/… GetDesktopWindow() returns is the actual desktop but you need FindWindowEx to access the Explorer desktop where the icons are stored
|
0

You can also do it purely with win32:

import win32com.client

shell = win32com.client.Dispatch("Shell.Application")
desktop_folder = shell.NameSpace(0) # Sometimes you need to use a lowercase S for some reason
desktop_items = desktop_folder.Items()
count = desktop_items.Count
    
print("Amount of icons:", count)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.