Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript–execute multi line code

I have some apple script code:

tell application "System Events"
    key code 97
end tell

How do i write the code as a osascript -e command in Terminal? Everytime I try using \n or the such, I get errors. Sorry if I'm not being specific enough.

like image 253
Cplusplusplus Avatar asked Aug 22 '15 13:08

Cplusplusplus


People also ask

How do I make multiple lines in terminal?

Using a Backslash. The backslash (\) is an escape character that instructs the shell not to interpret the next character. If the next character is a newline, the shell will read the statement as not having reached its end. This allows a statement to span multiple lines.

How do I run AppleScript in Java?

A Java AppleScript example Runtime runtime = Runtime. getRuntime(); String applescriptCommand = "tell app \"iTunes\"\n" + "activate\n" + "set sound volume to 40\n" + "set EQ enabled to true\n" + "play\n" + "end tell"; String[] args = { "osascript", "-e", applescriptCommand }; Process process = runtime. exec(args);


1 Answers

You have a couple of options:

  1. Pass each line of the AppleScript code as a separate -e option:

    osascript -e 'tell application "System Events"' -e 'key code 97' -e 'end tell'
    
  2. Pipe the AppleScript code to osascript's STDIN:

    osascript <<END
      tell application "System Events"
        key code 97
      end tell
    END
    

Oh, and you can also save AppleScript code as an executable shell script. Just add #!/usr/bin/osascript at the top of the code and save it as a plain text file:

#!/usr/bin/osascript

tell application "System Events"
  key code 97
end tell
like image 134
foo Avatar answered Sep 22 '22 08:09

foo