Safari没有内置的“黑暗主题”或者像Chrome这样的夜间模式扩展,所以当我需要在黑暗的房间做作业时(当我的室友睡觉的时候),它真的很疼。谢天谢地,Safari可以优先更改其默认css工作表。所以我写了一些东西,
* {
color: rgb(176, 176,176) !important;
background: #000000 !important;
}
a:link {
color: rgb(99,202,67)!important;
}它通常工作很好,但我需要反复切换样式表,特别是当它不是文本材料时。
因此,我想知道如何创建转换样式表的快捷方式?
那么使用AppleScript呢?
发布于 2018-08-21 13:28:38
在各种应用程序中,我有相当多的设置,我定期在两种状态之间切换,并使用AppleScript实现任务的自动化。我个人尽量不使用UI脚本(如果可能的话),因为它可能有其问题,但是,在您的特殊情况下,我相信使用AppleScript的UI脚本解决方案是需要完成的。
也就是说,如果Safari是关闭的,那么在查看UI时对~/Library/Preferences/com.apple.Safari.plist文件所做的更改可能也可以使用defaults命令进行,然后打开Safari。但是,我假设您希望能够在Safari打开的情况下动态地进行此更改,而不必关闭/重新打开来完成任务,因此需要UI脚本。
我接受了您问题中的代码,并将其保存为MyStyleSheet.css。
然后,我手动地完成了将其设置为样式表的过程: Safari > Preferences…>高级>样式表:
完成后,我将其设置为None Selected,然后编写以下AppleScript脚本:
示例AppleScript代码:
if not running of application "Safari" then
display dialog "Safari is not open." buttons {"OK"} default button "OK"
return
end if
tell application "Safari" to activate
delay 0.5
tell application "System Events"
tell application process "Safari"
click menu bar item "Safari" of menu bar 1
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
click UI element "Advanced" of toolbar 1 of window 1
end tell
tell group 1 of group 1 of window "Advanced" of application process "Safari"
set currentStyleSheet to (value of pop up button 1)
click pop up button 1
if currentStyleSheet contains "None Selected" then
click menu item "MyStyleSheet" of menu 1 of pop up button 1
else
click menu item "None Selected" of menu 1 of pop up button 1
end if
end tell
click button 1 of window "Advanced" of application process "Safari"
end tell我将上面的示例AppleScript代码保存为名为Toggle样式表的AppleScript应用程序,并测试了它的工作原理。请注意,样式表更改后关闭Safari首选项可能会出现时间延迟。滞后时间取决于Safari窗口是如何打开的,以及系统上同时发生的其他活动。无论是手动更改还是使用AppleScript,这种延迟都是一样的。
请注意,示例AppleScript代码就是这样的,目的是展示完成给定任务的方法。用户有责任根据需要添加任何适当的错误处理和附加的delay命令。
https://stackoverflow.com/questions/51941083
复制相似问题