如何在 Mac OS X 中获取显示名称和显示 ID?
我想知道您是否可以帮助我弄清楚如何在 Mac OS X (10.5) 中使用显示器的显示 ID 编号以编程方式获取显示器的显示名称? 一个要求是,如果我向函数提供显示 ID,它将提供显示名称作为回报(反之亦然)。
显示名称如下所示:“Color LCD”、“SAMSUNG”
显示 ID 如下所示:“69671872”、“893830283”
Cocoa (Obj-C) 中的 NSScreen 或 CGGetActiveDisplayList 在 Quartz (C) 中,允许您获取显示器的显示 ID 号。 两者似乎都没有获取显示名称的方法。 不好了! 以下是 NSScreen 获取显示器 ID 的代码:
NSArray *screenArray = [NSScreen screens];
NSDictionary *screenDescription = [[screenArray objectAtIndex:0] deviceDescription];
NSLog(@"Device ID: %@", [screenDescription objectForKey:@"NSScreenNumber"]);
系统分析器,以及系统偏好设置下的显示器,参考显示器按显示名称,而不是显示 ID。
我这样问是因为我想运行 AppleScript,它需要显示名称而不是显示 ID。 任何帮助深表感谢! :)
I was wondering if you could help me figure out how to progmatically get the Display Name for a monitor by using its Display ID number in Mac OS X (10.5)? A requirement is if I give a function the Display ID, it'll provide the Display Name in return (or vice versa).
Display Name looks something like this: "Color LCD", "SAMSUNG"
Display ID looks something like this: "69671872", "893830283"
NSScreen in Cocoa (Obj-C), or CGGetActiveDisplayList in Quartz (C), allow you to get the Display ID number for a monitor. Neither appear to have a method to get the Display Name. Oh no! Here's the code for NSScreen to get the Display ID:
NSArray *screenArray = [NSScreen screens];
NSDictionary *screenDescription = [[screenArray objectAtIndex:0] deviceDescription];
NSLog(@"Device ID: %@", [screenDescription objectForKey:@"NSScreenNumber"]);
System Profiler, and Displays under System Preferences, reference displays by Display Name, not Display ID.
I'm asking as I want to run an AppleScript, and it requires a Display Name rather than a Display ID. Any help is MUCH appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将为您提供本地化的显示名称:
This gives you the localized display name:
或者,如果您不想弄乱首选本地化数组,请将
kIODisplayOnlyPreferredName
标志传递给IODisplayCreateInfoDictionary()
这是一个更少的 CoreFoundation、更多的 Cocoa 和稍微简化的代码,会做同样的事情:
注意:必须链接 CoreGraphics 框架 (
-framework CoreGraphics
)Or if you don't want to mess with the preferred localization array, pass the
kIODisplayOnlyPreferredName
flag toIODisplayCreateInfoDictionary()
Here is a less CoreFoundation, more Cocoa and somewhat reduced code that will do the same thing:
Note: the CoreGraphics framework must be linked (
-framework CoreGraphics
)分类规则z =)
NSScreen+DisplayInfo.h
NSScreen+DisplayInfo.m
Categories rulez =)
NSScreen+DisplayInfo.h
NSScreen+DisplayInfo.m
这是一个将其组合在一起的完整应用程序(http://cl.ly/40Hw):
And here's a whole app that puts it together (http://cl.ly/40Hw):
其他答案使用
CGDisplayIOServicePort
其中已被弃用。从 macOS 10.15 开始
-[NSScreen localizedName]
可用:
对于显示 ID,您仍然可以使用
-[NSScreen deviceDescription]
:Other answers use
CGDisplayIOServicePort
which was deprecated.As of macOS 10.15
-[NSScreen localizedName]
is available:For the display ID you can still use
-[NSScreen deviceDescription]
:我使用 Robert Harder 的实现在 github.com 上创建了一个示例项目。
@robert-harder 感谢您提供这个想法!
I created an example project on github.com using the implementation of Robert Harder.
@robert-harder Thank you for providing the idea!