1

AppleScript allows you to run shell commands with do shell script. It's easy to assign a variable to an output of such command.

set theResult to do shell script "echo everything"

>> theResult = "everything"

On macOS you also can run Javascript code in Terminal with osascript:

osascript -l JavaScript << EOF

console.log('everything');

EOF

>> everything

However, if you execute this piece from AppleScript, you get nothing:

set theResult to do shell script "osascript -l JavaScript << EOF

console.log('everything');

EOF"

>> theResult = ""

I also tried to assign a shell variable to the osascript output and to print it with echo. Again, works in Terminal:

theString=$(osascript -l JavaScript << EOF

console.log('everything');

EOF
)

echo $theString"

>> everything

But it still does not work when executed via do shell script:

set theResult to do shell script "theString=$(osascript -l JavaScript << EOF

console.log('everything');

EOF
)

echo $theString"

>> theResult = ""

Why is this happening? How to make it work?

I'll appretiate your help.

My goal is to be able to do some text processing with Javascript within an AppleScript script.

I'm not interested in putting Javascript code into external .js file, I prefer to keep it in place.

0

console.log within osascript outputs to stderr (standard error), not stdout (standard out). Redirect stderr to stdout for the output to be captured to a variable using 2>&1.

set theResult to do shell script "osascript -l JavaScript 2>&1 << EOF
console.log('everything');
EOF"
1
  • Thank you. This worked. – SleepyAbyss Dec 29 '20 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .