媒介
最近在看视频的过程中,发现部分竖屏视频是横过来的,必要旋转才气正常展示。但有些应用的内置视频播放器又不支持视频旋转,导致这些视频根本没法观看。
通过体系设置中的显示器设置,改变显示器的旋转,可以满足这个需求,但在欣赏的过程中,一会儿改成横屏,一会儿改成竖屏,手动利用太麻烦了,因此想看看可不可以设置快捷键来快速举行切换。
搜刮了一下,发现相干内容还是比力少的,而且时间也比力久了,脚本根本不可用了。因此只能自己搞定了。
我的Mac环境
机型:MacBook Air 2023
芯片:Apple M2
体系版本:macOS Sequoia 15.0.1
我的需求
通过快捷键快速切换外接显示器的旋转方向
利用步骤
- 安装displayplacer
我是通过brew安装的,如果没安装过brew,可以去搜一下怎么安装brew。大概也可以按照displayplacer的教程自行安装。
- brew install displayplacer
复制代码
- 打开Mac自带的"自动利用" app,开始编写脚本
选择"快速利用"
选择资源库-实用工具-运行Shell脚本,双击或拖动到右侧。确认下Shell使用的是zsh
填入脚本
- output=$(/opt/homebrew/bin/displayplacer list)
- mainId=$(echo "$output" | awk '/Persistent screen id/ {prev=$NF} /Contextual screen id: 1/ {print prev}')
- secondId=$(echo "$output" | awk '/Persistent screen id/ {prev=$NF} /Contextual screen id: 2/ {print prev}')
- echo "$mainId"
- echo "$secondId"
- rotation=$(echo "$output" | grep 'Rotation: 90')
- if [[ -n $rotation ]]; then
- /opt/homebrew/bin/displayplacer "id:$mainId degree:0 origin:(0,0)" \
- "id:$secondId degree:0 origin:(1710,0)"
- else
- /opt/homebrew/bin/displayplacer "id:$mainId degree:0 origin:(0,0)" \
- "id:$secondId degree:90 origin:(1710,0)"
- fi
- echo "Success"
复制代码 下面我表明一下脚本内容
/opt/homebrew/bin/displayplacer:这是我的displayplacer的安装位置,可以通过在"终端"app中实行which displayplacer查察。
displayplacer list指令:查察当前的显示器信息,内容大概是这样的,敏感信息我用xxxxxx取代了,不紧张的信息我用…取代了
- Persistent screen id: xxxxxx
- Contextual screen id: 1
- Serial screen id: xxxxxx
- Type: MacBook built in screen
- Resolution: 1710x1107
- Hertz: 60
- Color Depth: 8
- Scaling: on
- Origin: (0,0) - main display
- Rotation: 0 - rotate internal screen example (may crash computer, but will be rotated after rebooting): `displayplacer "id:xxxxxx degree:90"`
- Enabled: true
- Resolutions for rotation 0:
- ......
- mode 8: res:1710x1107 hz:60 color_depth:8 scaling:on <-- current mode
- ......
- Persistent screen id: xxxxxx
- Contextual screen id: 2
- Serial screen id: xxxxxx
- Type: 27 inch external screen
- Resolution: 2560x1440
- Hertz: 60
- Color Depth: 8
- Scaling: on
- Origin: (1710,0)
- Rotation: 0
- Enabled: true
- Resolutions for rotation 0:
- ......
- mode 61: res:2560x1440 hz:60 color_depth:8 scaling:on <-- current mode
- ......
复制代码 下面的指令,重要是从上面这些信息中提取screenId。要提取的是Persistent screen id的值,但是我们要通过Contextual screen id: 的值来判定哪个是Mac的内置屏幕,哪个是外接屏幕。我要旋转的是外接屏幕。所以mainId提取了内置屏幕的Id,secondId提取了外接屏幕的Id。
- mainId=$(echo "$output" | awk '/Persistent screen id/ {prev=$NF} /Contextual screen id: 1/ {print prev}')
- secondId=$(echo "$output" | awk '/Persistent screen id/ {prev=$NF} /Contextual screen id: 2/ {print prev}')
复制代码 下面的指令是用来判定屏幕是否旋转了90度,后面会根据这个结果实行不同的指令
留意:这里默认隐含的逻辑是我只会旋转一个屏幕(外接屏幕),因此这里实在并没有区分是内置屏幕旋转了90度还是外接屏幕旋转了90度。
- rotation=$(echo "$output" | grep 'Rotation: 90')
复制代码 末了,我们根据屏幕是否旋转,实行不同的指令。-n表示不为空,如果rotation不为空,阐明有屏幕旋转90度(我的需求默认这个旋转的屏幕是外接屏幕),因此实行指令将外接屏幕(secondId)旋转返来(degree:0)。反之,如果rotation为空,阐明没有屏幕旋转,因此实行指令将外接屏幕旋转90度。
origin是用来控制屏幕排列的,我将内置屏幕设置为主屏幕,并且内置屏幕在左,外接屏幕在右,所以内置屏幕mainId设置成origin 0,0),外接屏幕secondId设置成origin 1710,0)。这个1710,实在就是我的内置屏幕的横向分辨率,从上面displayplacer list的内容中可以找到。
- if [[ -n $rotation ]]; then
- /opt/homebrew/bin/displayplacer "id:$mainId degree:0 origin:(0,0)" \
- "id:$secondId degree:0 origin:(1710,0)"
- else
- /opt/homebrew/bin/displayplacer "id:$mainId degree:0 origin:(0,0)" \
- "id:$secondId degree:90 origin:(1710,0)"
- fi
复制代码 脚本的内容表明完了,你可以根据自己的需求,以及displayplacer的文档,尽情修改脚本,以满足你的需求。修改脚本后,可以直接在"终端"app中运行,看一下效果是不是自己想要的,确认无误后将脚本保存到这里即可。建议将自己的脚本备份一份。
3. 键盘Command+S保存,填一个名字,比如"屏幕转转转",保存后就可以关闭"自动利用"APP了。
4. 下面我们就可以设置快捷键了。打开体系设置-键盘-键盘快捷键…
进入键盘快捷键页面,选择服务-通用,打开通用下拉框,就能看到我们新建的"屏幕转转转"服务了
看到右侧的谁人"无"了吗?双击它,出现一个输入框,这个就是设置快捷键的地方了。出现输入框后,按键盘上的具体键位,就可以设置成快捷键了。我设置成了control + option + command + r。
设置乐成后,点击完结果OK了。
试一下,按下快捷键,外接屏幕是不是旋转了90度?再按一次,是不是又规复正常了?
大功告成!
怎么删除这个服务?
进入~/Library/Services文件夹,删除你创建的谁人文件即可。比如我的是"屏幕转转转"。
快速进入指定文件夹的方法:打开"访达"app,键盘按Shift + Command + G,打开"前往文件夹"弹窗,输入~/Library/Services路径,回车,即可快速打开这个文件夹。
Update 2024/12/02
作者在使用时,发现这个服务不是到处都可用的,在某些特殊的软件中,它是没有服务这个选项的,咱们的服务天然也无法使用了。
正常应该是这样的,比如Finder大概Safari中
而在某些不能使用服务的软件中,比如百度网盘,是这样的
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |