Running Applescript from Shell

Until now, I’ve been using the following Applescript (dragged into the XOJO project window) to turn on Bluetooth.

tell application "System Events" to tell process "SystemUIServer"
  set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
  click bt
  tell (first menu item whose title is "Turn Bluetooth On") of menu of bt
    click
    click menu item "Turn Bluetooth On"
  end tell
end tell

It works fine, but I’d like to change it to run from a shell using the following

osascript -e 'my_Applescript_source_code'

single line command format. From what I’ve seen on Applescripting sites, if I replace the line endings in the original script with ‘¬’ (option-lowercase-L) it’s supposed to work. The resulting shell code is:

osascript -e 'tell application "System Events" to tell process "SystemUIServer"¬ set bt to (first menu bar item whose description is "bluetooth") of menu bar 1¬ click bt¬ tell (first menu item whose title is "Turn Bluetooth Off") of menu of bt¬  click¬  click menu item "Turn Bluetooth Off"¬ end tell¬end tell¬'

However, when I try it, I get this result returned from the shell:

67:296: syntax error: Expected end of line but found unknown token. (-2741)

BTW, I think I’ve properly escaped the double quotes as follows:

Dim theShellCode As String = "osascript -e 'tell application ""System Events"" to tell process ""SystemUIServer""¬ set bt to (first menu bar item whose description is ""bluetooth"") of menu bar 1¬ click bt¬ tell (first menu item whose title is ""Turn Bluetooth Off"") of menu of bt¬ click¬ click menu item ""Turn Bluetooth Off""¬ end tell¬end tell¬'"+EndOfLine
Does anyone have any ideas why this doesn’t work?

I’ve been able to run other applescripts using osascript -e in a shell, but they are all single line scripts.

FWW I don’t think this allowed anymore for macOS 10.15 and higher.

Not a problem. This is for my own use, and I’m running Sierra. (It’s my understanding that the problem with Catalina is that it’s only the Finder that can’t be the target of an Applescript.)
Anyway, I have no plans to run Catalina.
Ever.
:slight_smile:

I never knew you can use “¬” - in my keyboard layout option-l it’s the letter “?”. Have you tried to put every line into separate -e argument (that’s the way recommended by man osascript)?

Dim theShellCode As String = "osascript -e 'tell application ""System Events"" to tell process ""SystemUIServer""' -e 'set bt to (first menu bar item whose description is ""bluetooth"") of menu bar 1' -e 'click bt' -e 'tell (first menu item whose title is ""Turn Bluetooth Off"") of menu of bt'  -e 'click'  -e 'click menu item ""Turn Bluetooth Off""' -e 'end tell' -e 'end tell'"

Thank you. That worked perfectly.