osascript / syntax error: Expected end of line but found command name. (-2741)
Asked Answered
S

2

18

I'm running into problems with a shell script that utilizes a small portion of Applescript. When I compile it with Applescript editor it works. It does not though within a shell script.

44:49: syntax error: Expected end of line but found command name. (-2741) 23:28: syntax error: Expected end of line but found “after”. (-2741)

Here is the shell code:

osascript -e 'tell application "System Events" -e 'activate'

osascript -e 'tell process "Application 10.5" -e 'set frontmost to true' -e 'end tell'

osascript -e 'delay 1' -e 'keystroke return' -e 'delay 1' -e 'keystroke return'

end tell

Applescript (that works):

tell application "System Events"
activate
tell process "Application 10.5"
    set frontmost to true
end tell

delay 1
keystroke return
delay 1
keystroke return

end tell

[updated] / [solved]

This took care of any kind of problems I was having trying to modify the applescript to work within a shell script:

## shell script code

echo "shell script code"
echo "shell script code"

## applescript code

osascript <<EOF
tell application "Scriptable Text Editor"
    make new window
    activate
    set contents of window 1 to "Hello World!" & return
end tell
EOF

## resume shell script...

It's very cool that you're able to put pure applescript directly into a shell script. ;-)

Schistosome answered 6/8, 2011 at 3:2 Comment(0)
P
7

Each osascript(1) command is completely separate process, and therefore a completely separate script, so you can’t use state (such as variables) between them. You can build a multi-line script in osascript using multiple -e options -- they all get concatenated with line breaks between them to form the script. For a sufficiently long script, a separate file or a “here document”, as you used in your eventual solution, is a good way to go.

Also, if your script is mostly (or entirely!) AppleScript, you can make a “shell” script that simply is AppleScript using a shebang file that invokes osascript:

#!/usr/bin/osascript
display dialog "hello world"

...and then use do shell script as necessary.

Playroom answered 5/4, 2013 at 17:27 Comment(0)
N
3

Instead of using the -e flag, you can simply store your applescript code in a small text file and call

osascript /path/to/script

Also, if you're telling an application or process to do just one thing, you can write it like this:

tell process "MyProcess" to perform action.

Now that I think about it, running each line separately with the -e flag probably won't work because I don't think all the lines will connect and run as one program. For example, I just tested using osascript -e to set a variable. I then used a separate osascript -e to read the variable, and it couldn't.

[*]

Nims answered 6/8, 2011 at 3:24 Comment(1)
Thanks for the explanation. I actually figured out an excellent way to put the applescript directly into the shell script without having to go through all the work of modifying it to work. (see above) –Schistosome

© 2022 - 2024 — McMap. All rights reserved.