How to indent AppleScript oneliner

Viewed 79

I have an AppleScript oneliner that I would like to indent. But I wonder how to do this.

Here is the oneliner:

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

Here is how I tried to accomplish it:

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

Apperently this won't work.

What am I missing?

1 Answers

If you wish to keep your command as a "one-liner", but divide it over more than one line, then you need to make use of a line continuation character, which, in AppleScript, is denoted by ¬. This can be entered in Script Editor either by pressing ⌥Enter, or by pressing ⌥L.

Then you can split your one-liner like so:

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

You can experiment with placing the line continuation character in different positions to achieve different types of indentation, e.g.:

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

If you want to change your one-liner (referred to as a simple tell command) to what is called a compound tell command—one that ends with end tell—then you should omit the to after each tell you wish to compound:

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