如何缩进 AppleScript oneliner

How to indent AppleScript oneliner

我有一个 AppleScript oneliner,我想缩进。但我不知道如何做到这一点。

这是单行本:

tell application "System Events" to tell appearance preferences to set dark mode to not dark mode

以下是我尝试完成的方法:

tell application "System Events" to 
    tell appearance preferences to 
        set dark mode to not dark mode
    end tell
end tell

显然这行不通。

我错过了什么?

如果您希望将命令保留为 "one-liner",但将其分成多行,则需要使用续行符,在 AppleScript 中,该字符表示为 ¬。这可以通过按 Enter 或按 Script Editor 中输入]⌥L.

然后你可以像这样拆分你的一行:

tell application "System Events" to ¬
    tell appearance preferences to ¬
        set dark mode to not dark mode

您可以尝试将续行符放在不同的位置以实现不同类型的缩进,例如:

tell application "System Events" to tell ¬
    appearance preferences to set ¬
    dark mode to not dark mode

如果您想将单行代码(称为 simple tell 命令更改为所谓的 compound tell 命令——一个以 end tell 结尾的命令——那么你应该在每个你希望复合的 tell 之后省略 to

tell application "System Events"
    tell appearance preferences
        set dark mode to not dark mode
    end tell
end tell