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

查看:17
本文介绍了使用 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. iTerm
  2. 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 只给出我的 应用程序窗口的列表
  • 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.

当然,Window Manager 需要暴露一个 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天全站免登陆