Calling osascript with do shell script to fork process

I am trying to figure out how to call oascript using do shell script from an AppleScript, and am having some problems.

So for testing, I am using this simple script (which works fine in the AppleScript editor):

--Simple AppleScript
tell application "System Events" to count (every application process whose visible = true)

Now when I try this, it does NOT work (generates syntax error)m, and I cannot figure out how to make it work… probably has something to do with nested quatation marks, but have tried many variations and cannot figure it out:

--This does NOT work
do shell script "/usr/bin/osascript -e \"tell application \"System Events\" to count (every application process whose visible = true)\""

However, I did come across this method which does work:

--This does work
do shell script "/usr/bin/osascript <<EOF
tell application \"System Events\" to count (every application process whose visible = true)
EOF"

Now, if you want to fork something off to osascript and have the applescript continue without waiting for osascript to finish, you can theoretically use “&> /dev/null &” to send it to null and have it return to applescript right away.
So on the command line it would look like

osascript -e "some commnad" &> /dev/null &

The problem is, that I cannot get “&> /dev/null &” to work with the EOF method, and since the first (non EOF) method does not work at all, I cannot even test this on it.

So any ideas on why the non-EOF do shell script does not work?

And any ideas on how to get “&> /dev/null &” to work?

Thanks

Yep. Try:

do shell script "/usr/bin/osascript -e 'tell application \"System Events\" to count (every application process whose visible = true)' "

Shane’s given the best solution. But for information, to nest double-quotes, you have to escape the inner escape characters too: blackslash-backslash backslash-quote.

do shell script "/usr/bin/osascript -e \"tell application \\\"System Events\\\" to count (every application process whose visible = true)\""

Awesome!! :smiley: :smiley:

Thanks Shane and Nigel … both your solutions worked!

Happy New Year

I know the title says that is is for studio but it can also be used for scripts as well. You can find the topic here

@DJ Bazzie Wazzie - Good post … wish I had of found that a few days ago!