0

I'm using AppleScript to launch a quick-and-dirty shell script:

tell application "Terminal"
activate
do script "$(" & quoted form of MyScriptPath & ")"
end tell

Which properly launches a Terminal window and inputs what I would expect:

~$ $('/my script path/myscript.sh')

However, it seems that anything outputted to STDOUT (via echo) is evaluated as if it was inside the $( ) when evaluating/calling the script in the first place:

#!/bin/sh
echo "foobar"

produces:

-bash: foobar: command not found

I've searched far and wide and have not really found a suitable way to escape spaces in the path (rather than using "quoted form of") in AppleScript before sending the script location to Terminal, but I'd much prefer that. I'm using "do script" rather than "do shell script" because the script launching in Terminal is interactive and needs to be focused.

How can I echo to STDOUT when calling the script through $( )?

2
  • What are you trying to accomplish? In particular, why are using $(...)? That means to substitute the output of the enclosed command into that current command line under construction. If you run $(/path/to/myscript.sh) directly in a shell it will produce the same error because the substitution occurs for the first word of the new command. Commented Feb 20, 2013 at 23:49
  • Thanks for the explanation. I was using $() because of my assumption that a quoted string wouldn't cause the script at that path to execute. Cheers Commented Feb 21, 2013 at 1:49

1 Answer 1

1

You don't need $(...) to run a command, only to include the output of that command in another string. You simply need

tell application "Terminal"
activate
do script "/my script path/myscript.sh"
end tell
Sign up to request clarification or add additional context in comments.

1 Comment

Doh! Thank you for confirming to me that I was overcomplicating things :) My problem here was thinking I needed the $() (or ``) to evaluate a string into a command. For some reason I assumed that a quoted path would need to be eval'd for the script to execute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.