使用AppKit以编程方式更改macOS上全屏应用程序的顺序 [英] Programmatically change order of full screen apps on macOS with AppKit

查看:69
本文介绍了使用AppKit以编程方式更改macOS上全屏应用程序的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个macOS实用程序,该实用程序将允许我以编程方式更改计算机上全屏应用程序的顺序.

I’m trying to build a macOS utility that will allow me to programmatically change the order of full screen apps on my machine.

例如,假设我的计算机上的全屏应用当前是按从左到右的顺序排序的:

For example, let’s say the full screen apps on my machine are currently ordered as follows from left to right:

  1. Chrome
  2. iTerm
  3. Xcode

我希望能够在AppKit中调用一个方法,例如将Xcode从全屏索引2移动到索引0".但是,我找不到能够帮助我完成此工作的AppKit API.

I want to be able to call a method in AppKit to say something like "Move Xcode from full screen index 2 to index 0". However, I can't identify an AppKit API that will help me do this.

  • NSScreen 仅提供物理显示,而不是全屏应用程序/空间
  • NSApplication 仅给出 my 应用程序窗口的列表
  • CGWindowListCopyWindowInfo 看起来很有希望,但是它没有有关全屏应用程序顺序的信息.它还不允许我获取 NSWindow 对象,以希望更改顺序
  • NSScreen only gives the physical displays, not the full screen apps / spaces
  • NSApplication only gives the list of my app's windows
  • CGWindowListCopyWindowInfo looked promising, but it doesn't have information about the order of full screen apps. It also doesn't allow me to get a NSWindow object to have any hope of changing the order

我是否正在尝试使用AppKit进行操作?

Is what I'm trying to do even possible with AppKit?

谢谢!

推荐答案

空格(桌面和全屏应用程序都是空格)是系统窗口管理器的功能,并且只能由用户通过应用程序 Mission Control进行管理.

Spaces (desktops and fullscreen apps are both just spaces) are a feature of the system Window Manager and only manageable by the user through the app Mission Control.

当然,窗口管理器需要公开用于管理的API,否则 Mission Control 如何与之交互?但是该API不是公开的.

Of course, the Window Manager needs to expose an API for management, otherwise how would Mission Control interact with it? But that API is not public.

这些私有API的反向工程标头可以在网上找到,例如
https://github.com/NUIKit/CGSInternal

Reverse engineered headers for those private APIs can be found online, e.g.
https://github.com/NUIKit/CGSInternal

有了这些标题,您可以编写如下代码:

With those headers in place, you can write code like this:

#import "CGSInternal/CGSSpace.h"

CGSSpaceID activeSpace = 
    CGSGetActiveSpace(_CGSDefaultConnection());
NSLog(@"activeSpace: %zu", activeSpace);

CFArrayRef spaceArray = 
    CGSCopySpaces(_CGSDefaultConnection(), kCGSAllSpacesMask);
NSLog(@"allSpaces: %@", (__bridge id)spaceArray);

但是通常的警告在这里适用:

But the usual warnings apply here:

您不能使用私有API将代码提交到Apple的App Store.没有官方文档,关于该API的所有已知信息都经过了反向工程,可能并不总是正确的.由于该API不公开,因此Apple可以随时更改它,而无需事先发出任何警告,因此您的工作代码可能会在每次系统更新时中断.

You cannot commit code using private APIs to Apple's App Store. There is no official documentation, all that is known about that API has been reversed engineered and may not always be correct. As the API is not public, Apple can change it at any time without any previous warning and thus your working code could break with every system update.

这篇关于使用AppKit以编程方式更改macOS上全屏应用程序的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆