如何列出Mac上Python中所有工作区的所有窗口?

时间:2017-05-28 22:00:34

标签: python macos python-2.7 fullscreen quartz

以下Python 2代码打印当前工作空间中所有窗口的列表:

#!/usr/bin/python
import Quartz
for window in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID):
    print("%s - %s" % (window['kCGWindowOwnerName'], window.get('kCGWindowName', u'Unknown').encode('ascii','ignore')))

虽然它不会打印全屏应用程序(因为它在另一个工作区中)。

如何修改上述脚本以列出所有桌面的所有窗口?

2 个答案:

答案 0 :(得分:3)

以下脚本应返回任何桌面/工作区/显示,全屏和详细信息(坐标,pid,标题等)的窗口信息:

  public List<TabItem> tabItem;
    public MainWindow()
    {
        try {
            InitializeComponent();
            //nardimo array za TabItem
            tabItem = new List<TabItem>();

            //Dodamo zaznamek (tabItem)






            TabItem novTab = this.AddTabItem();


           //bajndam tab
            tabControl.DataContext = tabItem;
            tabControl.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
public TabItem AddTabItem()
    {
        int count = tabItem.Count;
        TabItem tab = new TabItem();



        tab.Header = string.Format("Zavihek {0}", count+1);
        tab.Name = string.Format("zavihek{0}", count);



        WebBrowser wb = new WebBrowser();

        wb.Name = string.Format("Brskalnik{0}", count);

        string a = "http://www.google.com";

        wb.Navigate(a);
        Url.Text = a;
        tab.Content = wb;

        tabItem.Insert(count, tab);


        return tab;

    }

答案 1 :(得分:1)

这里的关键是对IFormFile file的第一个参数使用正确的选项。因此,除了使用optionOnScreenOnly属性(列出当前屏幕上的所有窗口)之外,还需要添加excludeDesktopElements属性。

  

excludeDesktopElements:排除列表中属于桌面元素的所有窗口,包括背景图片和桌面图标。

E.g。

CGWindowListCopyWindowInfo

或者对于所有窗口,也可以使用kCGWindowListOptionAll属性。

  

kCGWindowListOptionAll:列出所有窗口,包括屏​​幕和屏幕外窗口。使用此选项检索列表时,list = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements & kCGWindowListOptionOnScreenOnly, kCGNullWindowID) 参数应设置为relativeToWindow

对于其他媒体资源,请检查CGWindow.h in CoreGraphics

因此原始代码可以更改为:

kCGNullWindowID