显示连接的显示器/显示器/分辨率的终端命令?

Posted

技术标签:

【中文标题】显示连接的显示器/显示器/分辨率的终端命令?【英文标题】:Terminal command to show connected displays/monitors/resolutions? 【发布时间】:2013-12-04 15:30:06 【问题描述】:

有没有办法通过 OS X 中的终端获取有关连接的显示器和显示器及其分辨率的信息?

我有一些安装在多台显示器上运行,有时如果未连接显示器,GFLW 窗口似乎无法打开 - 我想检查显示器是否正确连接,并可能将其转储到日志文件,但我还没有找到一种编程方式来做到这一点而不深入了解 Obj-C/Cocoa

【问题讨论】:

你应该在apple.stackexchange.com问这个问题 【参考方案1】:

您可以使用system_profiler SPDisplaysDataTypedefaults read /Library/Preferences/com.apple.windowserver.plist

$ system_profiler SPDisplaysDataType
Graphics/Displays:

    NVIDIA GeForce GT 640M:

      Chipset Model: NVIDIA GeForce GT 640M
      Type: GPU
      Bus: PCIe
      PCIe Lane Width: x16
      VRAM (Total): 512 MB
      Vendor: NVIDIA (0x10de)
      Device ID: 0x0fd8
      Revision ID: 0x00a2
      ROM Revision: 3707
      Displays:
        iMac:
          Display Type: LCD
          Resolution: 1920 x 1080
          Pixel Depth: 32-Bit Color (ARGB8888)
          Main Display: Yes
          Mirror: Off
          Online: Yes
          Built-In: Yes
          Connection Type: DisplayPort
$ defaults read /Library/Preferences/com.apple.windowserver.plist

    DisplayResolutionEnabled = 1;
    DisplaySets =     (
                (
                        
                Active = 1;
                Depth = 4;
                DisplayID = 69731456;
                DisplayProductID = 40978;
                DisplaySerialNumber = 0;
                DisplayVendorID = 1552;
                Height = 1080;
                IODisplayLocation = "ioservice:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/P0P2@1/IOPCI2PCIBridge/GFX0@0/NVDA,Display-A@0/NVDA";
                IOFlags = 7;
                LimitsHeight = 1080;
                LimitsOriginX = 0;
                LimitsOriginY = 0;
                LimitsWidth = 1920;
                MirrorID = 0;
                Mirrored = 0;
                Mode =                 
                    BitsPerPixel = 32;
                    BitsPerSample = 8;
                    DepthFormat = 4;
                    Height = 1080;
                    IODisplayModeID = "-2147479552";
                    IOFlags = 7;
                    Mode = 1;
                    PixelEncoding = "--------RRRRRRRRGGGGGGGGBBBBBBBB";
                    RefreshRate = 0;
                    SamplesPerPixel = 3;
                    UsableForDesktopGUI = 1;
                    Width = 1920;
                    kCGDisplayBytesPerRow = 7680;
                    kCGDisplayHorizontalResolution = 103;
                    kCGDisplayModeIsInterlaced = 0;
                    kCGDisplayModeIsSafeForHardware = 1;
                    kCGDisplayModeIsStretched = 0;
                    kCGDisplayModeIsTelevisionOutput = 0;
                    kCGDisplayModeIsUnavailable = 0;
                    kCGDisplayModeSuitableForUI = 1;
                    kCGDisplayPixelsHigh = 1080;
                    kCGDisplayPixelsWide = 1920;
                    kCGDisplayResolution = 1;
                    kCGDisplayVerticalResolution = 103;
                ;
                OriginX = 0;
                OriginY = 0;
                PixelEncoding = "--------RRRRRRRRGGGGGGGGBBBBBBBB";
                Resolution = 1;
                Unit = 0;
                UnmirroredHeight = 1080;
                UnmirroredLimitsHeight = 1080;
                UnmirroredLimitsOriginX = 0;
                UnmirroredLimitsOriginY = 0;
                UnmirroredLimitsWidth = 1920;
                UnmirroredMode =                 
                    BitsPerPixel = 32;
                    BitsPerSample = 8;
                    DepthFormat = 4;
                    Height = 1080;
                    IODisplayModeID = "-2147479552";
                    IOFlags = 7;
                    Mode = 1;
                    PixelEncoding = "--------RRRRRRRRGGGGGGGGBBBBBBBB";
                    RefreshRate = 0;
                    SamplesPerPixel = 3;
                    UsableForDesktopGUI = 1;
                    Width = 1920;
                    kCGDisplayBytesPerRow = 7680;
                    kCGDisplayHorizontalResolution = 103;
                    kCGDisplayModeIsInterlaced = 0;
                    kCGDisplayModeIsSafeForHardware = 1;
                    kCGDisplayModeIsStretched = 0;
                    kCGDisplayModeIsTelevisionOutput = 0;
                    kCGDisplayModeIsUnavailable = 0;
                    kCGDisplayModeSuitableForUI = 1;
                    kCGDisplayPixelsHigh = 1080;
                    kCGDisplayPixelsWide = 1920;
                    kCGDisplayResolution = 1;
                    kCGDisplayVerticalResolution = 103;
                ;
                UnmirroredOriginX = 0;
                UnmirroredOriginY = 0;
                UnmirroredResolution = 1;
                UnmirroredWidth = 1920;
                Width = 1920;
            
        )
    );
    ForceOldStyleMemoryManagement = 0;

【讨论】:

太棒了!非常感谢……正是我想要的。希望让我的一些应用程序更智能地连接显示器。 您知道更改分辨率和相关刷新率的命令吗? 如果您有一个用例,您需要比system_profiler 执行得更快的东西,xrandr 是一个合理的选择 - 信息不太完整,机器解析相对更难,但已经足够了对于一些简单的东西。在我的例子中,它是一个空间交换热键,我想根据是否连接外部显示器来表现不同。【参考方案2】:

您还可以使用名为cscreen 的命令行工具:

# install homebrew if you don't have it already
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# install cscreen
brew install --cask cscreen

# list displays
cscreen -l

输出:

DisplayID  Index     Depth     Width     Height  Refresh
       1       1        32      1440        900    60
       2       2        32      1920       1080    60
use -h to display all usage options

注意:

第一次尝试打开cscreen 时,操作系统将不允许打开它,因为它没有签名。 您必须转到System Preferences 中的Security & Privacy允许

Homebrew package search

【讨论】:

以上是关于显示连接的显示器/显示器/分辨率的终端命令?的主要内容,如果未能解决你的问题,请参考以下文章

Linux 下怎么用命令修改分辨率

如何在PuTTy终端上显示完整的输出

只有一个800x600的分辨率,且系统设置显示里也只有这个分辨

如何解决ssh连接CentOS后中文显示乱码

linuxhistory命令,终端显示和日志文件显示不一致

mac终端命令