简体   繁体   English

从终端/bash 获取 Macbook 屏幕尺寸

[英]Get Macbook screen size from terminal/bash

Does anyone know of any possible way to determine or glean this information from the terminal (in order to use in a bash shell script)?有谁知道从终端确定或收集此信息的任何可能方法(以便在 bash shell 脚本中使用)?

On my Macbook Air, via the GUI I can go to "About this mac" > "Displays" and it tells me: Built-in Display, 13-inch (1440 x 900)在我的 Macbook Air 上,我可以通过 GUI 转到“关于本机”>“显示器”,它告诉我:内置显示器,13 英寸 (1440 x 900)

I can get the screen resolution from the system_profiler command, but not the "13-inch" bit.我可以从 system_profiler 命令获得屏幕分辨率,但不能获得“13 英寸”位。 I've also tried with ioreg without success.我也试过 ioreg 没有成功。 Calculating the screen size from the resolution is not accurate, as this can be changed by the user.根据分辨率计算屏幕尺寸是不准确的,因为这可以由用户更改。

Has anyone managed to achieve this?有没有人设法实现这一目标?

I think you could only get the display model-name which holds a reference to the size:我认为您只能获得包含尺寸参考的显示模型名称:

ioreg -lw0 | grep "IODisplayEDID" | sed "/[^<]*</s///" | xxd -p -r | strings -6 | grep '^LSN\|^LP'

will output something like:将输出类似:

LP154WT1-SJE1

which depends on the display manufacturer.这取决于显示器制造商。 But as you can see the first three numbers in this model name string imply the display-size: 154 == 15.4''但是正如您所看到的,此型号名称字符串中的前三个数字表示显示尺寸:154 == 15.4''

EDIT编辑

Found a neat solution but it requires an internet connection:找到了一个巧妙的解决方案,但它需要互联网连接:

curl -s http://support-sp.apple.com/sp/product?cc=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -c 9-` |
sed 's|.*<configCode>\(.*\)</configCode>.*|\1|'

hope that helps希望有帮助

The next script:下一个脚本:

model=$(system_profiler SPHardwareDataType | \
/usr/bin/perl -MLWP::Simple -MXML::Simple -lane '$c=substr($F[3],8)if/Serial/}{
print XMLin(get(q{http://support-sp.apple.com/sp/product?cc=}.$c))->{configCode}')

echo "$model"

will print for example:将打印例如:

MacBook Pro (13-inch, Mid 2010)

Or the same without perl but more command forking:或者没有perl但更多的命令分支相同:

model=$(curl -s http://support-sp.apple.com/sp/product?cc=$(system_profiler SPHardwareDataType | sed -n '/Serial/s/.*: \(........\)\(.*\)$/\2/p')|sed 's:.*<configCode>\(.*\)</configCode>.*:\1:')
echo "$model"

It is fetched online from apple site by serial number, so you need internet connection.它是通过序列号从苹果网站在线获取的,因此您需要连接互联网。

I've found that there seem to be several different Apple URLs for checking this info.我发现似乎有几个不同的 Apple URL 用于检查此信息。 Some of them seem to work for some serial numbers, and others for other machines.其中一些似乎适用于某些序列号,而另一些则适用于其他机器。 eg:例如:

https://selfsolve.apple.com/wcResults.do?sn= $Serial&Continue=Continue&num=0 https://selfsolve.apple.com/RegisterProduct.do?productRegister=Y&country=USA&id= $Serial http://support-sp.apple.com/sp/product?cc= $serial (last 4 digits) https://selfsolve.apple.com/agreementWarrantyDynamic.do https://selfsolve.apple.com/wcResults.do?sn= $Serial&Continue=Continue&num=0 https://selfsolve.apple.com/RegisterProduct.do?productRegister=Y&country=USA&id= $Serial http://support- sp.apple.com/sp/product?cc= $serial(最后 4 位数字) https://selfsolve.apple.com/agreementWarrantyDynamic.do

However, the first two URLs are the ones that seem to work for me.但是,前两个 URL 似乎对我有用。 Maybe it's because the machines I'm looking up are in the UK and not the US, or maybe it's due to their age?也许是因为我正在查找的机器在英国而不是美国,或者是因为它们的年龄?

Anyway, due to not having much luck with curl on the command line (The Apple sites redirect, sometimes several times to alternative URLs, and the -L option doesn't seem to help), my solution was to bosh together a (rather messy) PHP script that uses PHP cURL to check the serials against both URLs, and then does some regex trickery to report the info I need.无论如何,由于在命令行上使用 curl 不太走运(Apple 站点重定向,有时多次重定向到替代 URL,并且 -L 选项似乎没有帮助),我的解决方案是将一个(相当混乱的) PHP 脚本,它使用 PHP cURL 根据两个 URL 检查序列号,然后执行一些正则表达式技巧来报告我需要的信息。

Once on my web server, I can now curl it from the terminal command line and it's bringing back decent results 100% of the time.一旦在我的网络服务器上,我现在可以从终端命令行卷曲它,并且它在 100% 的时间带回不错的结果。 I'm a PHP novice so I won't embarrass myself by posting the script up in it's current state, but if anyone's interested I'd be happy to tidy it up and share it on here (though admittedly it's a rather long winded solution to what should be a very simple query).我是 PHP 新手,所以我不会通过将脚本发布到当前状态来让自己尴尬,但是如果有人感兴趣,我很乐意整理它并在此处分享(尽管不可否认,这是一个相当冗长的解决方案)应该是一个非常简单的查询)。 This info really should be simply made available in system_profiler.这个信息真的应该简单地在 system_profiler 中提供。 As it's available through System Information.app, I can't see a reason why not.因为它可以通过 System Information.app 获得,所以我找不到原因。

Hi there for my bash script , under GNU/Linux : I make the follow to save嗨,我的 bash 脚本,在 GNU/Linux 下:我进行以下操作以保存

# Resolution Fix
echo `xrandr --current | grep current | awk '{print $8}'` >> /tmp/width
echo `xrandr --current | grep current | awk '{print $10}'` >> /tmp/height
cat /tmp/height | sed -i 's/,//g' /tmp/height
WIDTH=$(cat /tmp/width)
HEIGHT=$(cat /tmp/height)
rm /tmp/width /tmp/height
echo "$WIDTH"'x'"$HEIGHT" >> /tmp/Resolution
Resolution=$(cat /tmp/Resolution)
rm /tmp/Resolution
# Resolution Fix

and the follow in the same script for restore after exit from some app / game并在退出某些应用程序/游戏后在同一脚本中进行恢复

in some SO This its execute command directly在某些 SO 这直接执行命令

ResolutionRestore=$(xrandr -s $Resolution)

But if dont execute call the variable with this to execute the varible content但是如果不执行调用这个变量来执行变量内容

$($ResolutionRestore)

And the another way you can try its with the follow for example另一种方法是您可以尝试以下操作,例如

RESOLUTION=$(xdpyinfo | grep -i dimensions: | sed 's/[^0-9]*pixels.*(.*).*//' | sed 's/[^0-9x]*//')
VRES=$(echo $RESOLUTION | sed 's/.*x//')
HRES=$(echo $RESOLUTION | sed 's/x.*//')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM