user1892304
user1892304

Reputation: 637

Expected end of line, etc. but found “"”

I'm trying to make an AppleScript that will toggle Bluetooth, but I can't seem to get past the following error:

Expected end of line, etc. but found “"”.

Here's my code:

tell application "System Preferences"
reveal pane id "com.apple.preferences.Bluetooth"
tell application "System Events" to tell process "System Preferences"
    set bluetooth to checkbox "On" of window 1
    set bluetoothBool to value of checkbox "On" of window 1 as boolean
    tell bluetooth
        if bluetoothBool = false then
            click bluetooth
            display dialog "Bluetooth on" with title "Bluetooth"
                buttons "OK" "Turn Bluetooth off"
                default button "OK"
        else if bluetoothBool = true then
            click bluetooth
            display dialog "Bluetooth off" with title "Bluetooth"
                buttons "OK" "Turn Bluetooth on"
                default button "OK"
        end if
    end tell
end tell
quit

end tell

Upvotes: 4

Views: 6651

Answers (1)

andrewdotn
andrewdotn

Reputation: 34873

"OK" "Turn Bluetooth off" needs to be {"OK", "Turn Bluetooth off"}.

Also, display dialog statements need to go all on one line, unless you "continue” a line with ¬, entered by typing Option-l (lowercase L).

tell application "System Preferences"
    reveal pane id "com.apple.preferences.Bluetooth"
    tell application "System Events" to tell process "System Preferences"
        set bluetooth to checkbox "On" of window 1
        set bluetoothBool to value of checkbox "On" of window 1 as boolean
        tell bluetooth
            if bluetoothBool = false then
                click bluetooth
                display dialog "Bluetooth on" with title ¬
                    "Bluetooth" buttons {"OK", "Turn Bluetooth off"} ¬
                    default button "OK"
            else if bluetoothBool = true then
                click bluetooth
                display dialog "Bluetooth off" with title ¬
                    "Bluetooth" buttons {"OK", "Turn Bluetooth on"} ¬
                    default button "OK"
            end if
        end tell
    end tell
    quit
end tell

Source: AppleScript Language Guide

Upvotes: 4

Related Questions