如何获取在Mac OS X中显示ID显示名称?(How to Get the Display Nam

2019-06-28 04:30发布

我在想,如果你能帮助使用Mac OS X中其显示的ID号(10.5)我弄清楚如何progmatically获得的显示名称显示器? 要求是,如果我给一个功能显示器ID,它会提供收益(或反之亦然)的显示名称。

显示名称看起来是这样的:“彩色液晶”,“SAMSUNG”

显示ID看起来是这样的:“69671872”,“893830283”

NSScreen可可(OBJ - C的),或在CGGetActiveDisplayList石英(C),让你得到一个监视器显示的ID号。 无论看起来有一个方法来获得的显示名称。 不好了! 下面的代码为NSScreen让显示器ID:

NSArray *screenArray = [NSScreen screens];
NSDictionary *screenDescription = [[screenArray objectAtIndex:0] deviceDescription];
NSLog(@"Device ID: %@", [screenDescription objectForKey:@"NSScreenNumber"]);

系统概述 ,并在系统预置 显示 ,参照显示由显示名称,而不是显示ID。

我问,因为我想运行AppleScript,它需要一个显示名称,而不是显示ID。 任何帮助深表感谢! :)

Answer 1:

这给你的本地化显示名称:

static void KeyArrayCallback(const void* key, const void* value, void* context) { CFArrayAppendValue(context, key);  }

- (NSString*)localizedDisplayProductName
{
    NSDictionary* screenDictionary = [[NSScreen mainScreen] deviceDescription];
    NSNumber* screenID = [screenDictionary objectForKey:@"NSScreenNumber"];
    CGDirectDisplayID aID = [screenID unsignedIntValue];            
    CFStringRef localName = NULL;
    io_connect_t displayPort = CGDisplayIOServicePort(aID);
    CFDictionaryRef dict = (CFDictionaryRef)IODisplayCreateInfoDictionary(displayPort, 0);
    CFDictionaryRef names = CFDictionaryGetValue(dict, CFSTR(kDisplayProductName));
    if(names)
    {
        CFArrayRef langKeys = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
        CFDictionaryApplyFunction(names, KeyArrayCallback, (void*)langKeys);
        CFArrayRef orderLangKeys = CFBundleCopyPreferredLocalizationsFromArray(langKeys);
        CFRelease(langKeys);
        if(orderLangKeys && CFArrayGetCount(orderLangKeys))
        {
            CFStringRef langKey = CFArrayGetValueAtIndex(orderLangKeys, 0);
            localName = CFDictionaryGetValue(names, langKey);
            CFRetain(localName);
        }
        CFRelease(orderLangKeys);
    }
    CFRelease(dict);
    return [(NSString*)localName autorelease];
}


Answer 2:

或者,如果你不想与优选定位阵列一塌糊涂,传递kIODisplayOnlyPreferredName标志IODisplayCreateInfoDictionary()

这里是一个少的CoreFoundation,更多的可可和有所降低的代码,将做同样的事情:

NSString* screenNameForDisplay(CGDirectDisplayID displayID)
{
    NSString *screenName = nil;

    NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName);
    NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];

    if ([localizedNames count] > 0) {
        screenName = [[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] retain];
    }

    [deviceInfo release];
    return [screenName autorelease];
}


Answer 3:

这里是一个整体的应用程序,把它在一起(http://cl.ly/40Hw):

/* 
 DisplayID.m
 Author: Robert Harder, rob@iHarder.net
 with help from http://stackoverflow.com/questions/1236498/how-to-get-the-display-name-with-the-display-id-in-mac-os-x

 Returns a list of display names and display IDs.
 Add the flag -v for more information on the screens.

 Compile from the command line:
   cc DisplayID.m -o DisplayID \
     -framework AppKit -framework Foundation -framework IOKit \
     -arch x86_64 -arch i386 -arch ppc7400

 Examples:

   $ DisplayID
   Color LCD : 69675202

   $ DisplayID -v
   Color LCD : 69675202
   {
       NSDeviceBitsPerSample = 8;
       NSDeviceColorSpaceName = NSCalibratedRGBColorSpace;
       NSDeviceIsScreen = YES;
       NSDeviceResolution = "NSSize: {72, 72}";
       NSDeviceSize = "NSSize: {1440, 900}";
       NSScreenNumber = 69675202;
   }
 */

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <IOKit/graphics/IOGraphicsLib.h>

#define str_eq(s1,s2)  (!strcmp ((s1),(s2)))

NSString* screenNameForDisplay(CGDirectDisplayID displayID )
{
    NSString *screenName = nil;

    NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName);
    NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];

    if ([localizedNames count] > 0) {
        screenName = [[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] retain];
    }

    [deviceInfo release];
    return [screenName autorelease];
}


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    BOOL verbose = NO;
    BOOL extraVerbose = NO;

    if( argc >= 2 ){
        if( str_eq( "-v",argv[1]) ){
            verbose = YES;
        } else if( str_eq( "-vv", argv[1] ) ){
            verbose = YES;
            extraVerbose = YES;
        } else {
            printf("USAGE: %s [-v[v]]\n", argv[0]);
            printf("Prints a list of names and numeric IDs for attached displays.\n");
            printf("  -v    Verbose mode. Prints more information about each display.\n");
            printf("  -vv   Extra verbose. Prints even more information.\n");
            return argc;
        }
    }

    NSArray *screenArray = [NSScreen screens];
    for( NSScreen *screen in screenArray ){

        NSDictionary *screenDescription = [screen deviceDescription];

        NSNumber *displayID = [screenDescription objectForKey:@"NSScreenNumber"];
        NSString *displayName =screenNameForDisplay([displayID intValue]);


        printf( "%s : %d\n", [displayName UTF8String], [displayID intValue]);       
        if( verbose ){
            printf( "%s\n", [[screenDescription description] UTF8String] );         
        }
        if( extraVerbose ){ 
            NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort([displayID intValue]), kIODisplayOnlyPreferredName);
            printf( "%s\n", [[deviceInfo description] UTF8String] );
        }

    }   // end for:


    [pool drain];
    return 0;
}


Answer 4:

分类rulez =)

NSArray *screens = [NSScreen screens];

for (NSScreen *screen in screens) {
    NSLog([NSString stringWithFormat:@"%@", [screen displayID]]);
    NSLog([NSString stringWithFormat:@"%@", [screen displayName]]);
}  

NSScreen + DisplayInfo.h

#import <Cocoa/Cocoa.h>

@interface NSScreen (DisplayInfo)

-(NSString*) displayName;
-(NSNumber*) displayID;

@end

NSScreen + DisplayInfo.m

#import "NSScreen+DisplayInfo.h"   
#import <IOKit/graphics/IOGraphicsLib.h>

@implementation NSScreen (DisplayInfo)

-(NSString*) displayName
{
    CGDirectDisplayID displayID = [[self displayID] intValue];

    NSString *screenName = nil;

    NSDictionary *deviceInfo = (NSDictionary *)CFBridgingRelease(IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName));
    NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];

    if ([localizedNames count] > 0) {
        screenName = [localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]];
    }

    return screenName;
}

-(NSNumber*) displayID
{
    return [[self deviceDescription] valueForKey:@"NSScreenNumber"];
}
@end


Answer 5:

我创建了一个使用罗伯特实施哈德上github.com示例项目 。
@罗伯特 - 更难谢谢你提供的主意!



文章来源: How to Get the Display Name with the Display ID in Mac OS X?