Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. ;-)

like image 713
dr. null Avatar asked Aug 06 '11 03:08

dr. null


2 Answers

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.

like image 115
Chris N Avatar answered Nov 15 '22 20:11

Chris N


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.

[*]

like image 38
pinerd314159 Avatar answered Nov 15 '22 21:11

pinerd314159