Answers:
您确实可以使用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
致记:
我稍微添加了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有时会给出略带深色的颜色,因此您可能需要根据自己的喜好进行调整。