如何在Terminal.app中使用随机背景颜色


2

在X11天,我有一个脚本,可以随机选择一个柔和的背景颜色,然后打开一个新的xterm。这很容易做到,因为我可以传递一个参数(我记得)-background。

Terminal.app有这样的界面吗?我更喜欢通过bash脚本执行此操作,但如果需要,我可以处理AppleScript。

我倾向于在一个终端窗口中使用选项卡,这也可能使事情复杂化。

我现在在莫哈韦。


让我们看看是否有人为此找到了AppleScript解决方案。如何处理字体颜色以确保您始终可以读取终端中的文本?
nohillside

Answers:


4

您确实可以使用AppleScript更改终端背景颜色。

例如:

tell application "Terminal"
  set background color of first window to {33717, 0, 21000, 32288}
end tell

您可以从命令行执行它:

osascript -e 'tell application "Terminal"' -e 'set background color of first window to {33717, 0, 21000, 32288}' -e 'end tell'

然后,您可以更改更多属性:

tell application "Terminal"
    set background color of first window to {33717, 0, 21000, 32288}
    set cursor color of first window to "white"
    set normal text color of first window to "white"
    set bold text color of first window to "yellow"
end tell

要从终端执行这种较长的脚本,您可以将其保存到单独的文件中,~/.terminal_change_color.scpt然后从终端调用它:

osascript ~/.terminal_change_color.scpt

然后,您可以将其添加到您,~/.bash_profile以便为您打开的每个新终端窗口执行它:

echo "osascript ~/.terminal_change_color.scpt" >> ~/.bash_profile

下面是一个较长的脚本来选择随机背景颜色。正如@nohillside注意到的那样,您还需要调整文本颜色以使其可读(深色背景上的白色文本和浅色背景上的黑色文本)。脚本处理它。

global kColorValueMaximum
set kColorValueMaximum to 65535

-- Choose a random color for the background
set randomRed to (random number) * kColorValueMaximum
set randomGreen to (random number) * kColorValueMaximum
set randomBlue to (random number) * kColorValueMaximum
set myBackgroundColor to {randomRed, randomGreen, randomBlue}

-- Select appropriate text colors based on that background
set {myTextColor, myBoldColor} to my ContrastingTextColors(myBackgroundColor)

-- Now inflict them on the frontmost window
tell application "Terminal"
    set targetWindow to window 1
    set background color of targetWindow to myBackgroundColor
    set cursor color of targetWindow to myTextColor
    set normal text color of targetWindow to myTextColor
    set bold text color of targetWindow to myBoldColor
end tell

on ContrastingTextColors(myColor)
    set whiteColor to {kColorValueMaximum, kColorValueMaximum, kColorValueMaximum, kColorValueMaximum}
    set lightGreyColor to {40000, 40000, 40000, kColorValueMaximum}
    set blackColor to {0, 0, 0, kColorValueMaximum}
    set darkGreyColor to {20000, 20000, 20000, kColorValueMaximum}

    -- From http://www.wilsonmar.com/1colors.htm
    set myRed to (item 1 of myColor) / kColorValueMaximum
    set myGreen to (item 2 of myColor) / kColorValueMaximum
    set myBlue to (item 3 of myColor) / kColorValueMaximum
    set magicY to (0.3 * myRed) + (0.59 * myGreen) + (0.11 * myBlue)
    if (magicY < 0.5) then
        return {whiteColor, lightGreyColor}
    else
        return {blackColor, darkGreyColor}
    end if
end ContrastingTextColors

或者,您可以在终端首选项>配置文件中创建30个主题,并随机选择一个:

tell application "Terminal"
    set themes to {"Basic", "Homebrew", "Man Page", "Novel", "Pro"}
    set theme to some item of themes
    set current settings of window 1 to settings set theme
end tell

致记:


神圣的GEEZE !!! :-)。我明天需要玩这个并验证一切。谢谢你的精彩回答。
pedz

我调用了大脚本.xyz.scpt(现在)然后做了:while :; do osascript .xyz.scpt; 睡2; 完成并观察颜色每两秒钟变化......很有趣。再次感谢你!
pedz

1

我稍微添加了Yoric的精彩答案,因为我更喜欢我称之为“柔和”的颜色。用以下代码替换脚本长脚本的前七行:

global kColorValueMaximum
set kColorValueMaximum to 65535
set kColorValueRange to 32768
set kColorValueMinimum to kColorValueMaximum - kColorValueRange

-- Choose a random color for the background
set randomRed to (random number) * kColorValueRange + kColorValueMinimum
set randomGreen to (random number) * kColorValueRange + kColorValueMinimum
set randomBlue to (random number) * kColorValueRange + kColorValueMinimum

对我来说,制作范围16384太精简了。32768有时会给出略带深色的颜色,因此您可能需要根据自己的喜好进行调整。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.