Mac屏幕常亮,禁止休眠的小技巧

2023-01-29 Note 458

最烦在终端进行编译的时候黑屏休眠,最烦在下载大型文件的时候黑屏休眠。

最烦人不在屏幕前的时候,工作因为屏幕休眠而停止。这样的烦恼不知道你们有没有。于是乎各种防休眠的小APP就成了大家必备工具了。我也不例外,一直用着KeepingYouAwake有几年,每次人不在屏幕前,但又有工作在跑的情况下,都会去开启它。软件也确实小巧省心,以至于从未在这方面有过其他想法。

最近在玩自动化任务,也就是那个扫地僧和快捷指令的高级玩法,也是从这个方向考虑到,屏幕常亮完全可以通过快捷指令来实现。其根本核心就是caffeinate,中文理解就是咖啡因,防止系统程序休眠。

详细说明

Syntax
      caffeinate [-disu] [-t timeout] [-w pid] [utility arguments...]

Key
   -d      Create an assertion to prevent the display from sleeping.

   -i      Create an assertion to prevent the system from idle sleeping.

   -m      Create an assertion to prevent the disk from idle sleeping.

   -s      Create an assertion to prevent the system from sleeping. This
           assertion is valid only when system is running on AC power.

   -u      Create an assertion to declare that user is active.
           If the display is off, this option turns the display on and prevents the display from going
           into idle sleep. If a timeout is not specified with '-t' option, then this assertion is
           taken with a default of 5 second timeout.

   -t      Specifies the timeout value in seconds for which this assertion has to be valid.
           The assertion is dropped after the specified timeout.
           Timeout value is not used when an utility is invoked with this command.

   -w      Waits for the process with the specified pid to exit. Once the  the process exits, the
           assertion is also released.  This option is ignored when used with utility option.
Location: /usr/bin/caffeinate

Examples
Prevent sleep for 1 hour (3600 seconds)
$ caffeinate -u -t 3600

Make caffeinate fork a process, exec "make" in it, and hold an assertion that prevents idle sleep as long as that process is running:
$ caffeinate -i make

通过以上的解释,基本可以掌握一个终端命令就是caffeinate -u -t 36000 也就是屏幕常亮10小时。

说到这,可能会引来一顿喷,在Mac设置的节能里不是可以控制屏幕常亮时常吗?搞这么复杂是为了纯装逼吗?

大叔:是有这个设置,但是这个系统级的设置,改过之后是不是还要改回来呢?有没有可能忘记改了呢?我的想法就是,偶尔使用一下,就不去动系统设置了。改来改去真有可能会忘记恢复它。

为了更便捷的使用,下面跟着步骤走,打开【快捷指令】【右上角+号】【右侧选择脚本】【一直往下拉找到运行Shell脚本】,将caffeinate -u -t 36000贴入,保存指令,此时你也可以对指令的名称,颜色,图标进行DIY设计,还可以修改36000秒这个时间值。

那么,以后需要屏幕常亮的时候,只需要运行这个快捷指令,即可实现屏幕常亮,不会自动休眠,并且该指令会运行在顶部右侧的状态栏里,不管你的窗口主界面是什么软件,都不会影响这个快捷指令的默认运行,而当你不再需要屏幕常亮只需要去到右上角里将这个快捷指令取消即可。

此时,再回想,如此简单的操作为什么还要再去安装一个APP呢?不是APP不好,而是需求极简就以最极简的方式去完成它。我是大叔,极简而又极致的骚货。

1 条评论

发布评论