bash 更改屏幕分辨率和运行程序的脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5801701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:51:53  来源:igfitidea点击:

Script to change screen resolution and run a program?

bashscriptingubuntu

提问by Cory G.

I have never done any scripting (and am rather new to Linux as well). What I would like is an icon that the user can click on that does the following things:
1. Change the screen resolution to 640x480.
2. Run a program (let's call it /path/myprogram)
3. When the program terminates, change the screen resolution back to 1024x768.

我从来没有写过任何脚本(而且我对 Linux 也很陌生)。我想要的是一个用户可以单击的图标,它可以执行以下操作:
1. 将屏幕分辨率更改为 640x480。
2. 运行一个程序(我们称之为/path/myprogram
3. 当程序终止时,将屏幕分辨率改回 1024x768。

Apparently there is something called "xrandr" which might be able to do this, but I'm not sure how it works. Also, the screen is set to "upside down" right now, and should stay that way throughout (I don't know if that is important for this script or not).

显然有一种叫做“xrandr”的东西可以做到这一点,但我不确定它是如何工作的。此外,屏幕现在设置为“颠倒”,并且应该一直保持这种状态(我不知道这对这个脚本是否重要)。

Also, I've noticed that in Ubuntu, when clicking on scripts, it brings up a prompt: "Run in Terminal", "Display", "Run", etc. Is there some way to make it default to "Run" so that this prompt is not displayed at all?

另外,我注意到在 Ubuntu 中,当点击脚本时,它会出现一个提示:“在终端中运行”、“显示”、“运行”等。有没有办法让它默认为“运行”所以根本不显示此提示?

Thank you very much.

非常感谢。

[[EDIT]]

[[编辑]]

I made a script using xrandrand it works...almost. It does not behave the same as going to Preferences -> Monitors. When I use the script to change to 640x480, part of the right side of the screen gets cut off. Also, it changes the refresh rate to 73Hz instead of 60Hz for some reason (and if I try changing the refresh rate to 60Hz from Preferences afterwards, the image becomes a bit distorted).

我用xrandr它制作了一个脚本,它几乎可以工作。它的行为与转到首选项 - > 监视器不同。当我使用脚本更改为 640x480 时,屏幕右侧的一部分被切断。此外,它出于某种原因将刷新率更改为 73Hz 而不是 60Hz(如果我之后尝试将刷新率从“首选项”更改为 60Hz,图像会变得有点失真)。

I tried using xrandr --propto figure out what is happening, and the only difference between the regular Preferences -> Monitors way and the xrandrway appears to be this line:

我试图xrandr --prop弄清楚发生了什么,常规 Preferences -> Monitors 方式和xrandr方式之间的唯一区别似乎是这一行:

   640x480        72.8     75.0     60.0* 
   640x480        72.8*    75.0     60.0  

The heading for that section is (I think it's a heading?):

该部分的标题是(我认为这是一个标题?):

VGA1 connected 640x480+0+0 inverted (normal left inverted right x axis y axis) 0mm x 0mm

The numbers seem like they would correspond to the refresh rate, but that doesn't explain why the image is getting cut off/distorted.

这些数字似乎与刷新率相对应,但这并不能解释为什么图像被切断/扭曲。

So basically, I have no idea what's going on and no idea how to fix it. I need the resolution change to behave the same as Preferences -> Monitors.

所以基本上,我不知道发生了什么,也不知道如何解决它。我需要更改分辨率以与首选项 - > 监视器相同。

Also, as far as the desktop icon goes, it turns out that Ubuntu has something called a "Launcher" and that worked nicely. Thank you anyway. :)

此外,就桌面图标而言,事实证明 Ubuntu 有一个叫做“启动器”的东西,并且运行良好。还是很感谢你。:)

回答by Mikel

Resizing the screen:

调整屏幕大小:

xrandr --size 640x480

Desktop icon:

桌面图标:

[Desktop Entry]
Encoding=UTF-8
Name=My App
Comment=What the app does
Exec=programname
Icon=??
Terminal=false
Type=Application
StartupNotify=true
Categories=??

and put it in ~/.local/share/applications/programname.desktopor /usr/share/applications/programname.desktop.

并将其放入~/.local/share/applications/programname.desktopor 中/usr/share/applications/programname.desktop

See Desktop Entry Specificationand Desktop Entry Filesfor full details.

有关完整详细信息,请参阅桌面条目规范桌面条目文件