running shell from applescript

Hi all,

does anybody knows why inside my applscript a script won't run properly while it will if called from the terminal?


one example


do shell script "directory/myscript.sh"


will not work, while



tell application "Terminal"


activate

do script "directory/myscript.sh"

end tell


will work perfectly?

Posted on Aug 23, 2012 1:31 PM

Reply
9 replies

Aug 23, 2012 1:51 PM in response to personal_username

As twtwtw says, your environment from withing an Applescript (or Automator workflow) is NOT going to be the same as when running a script from the termianl


You might try running a script that contans:


( echo "your current working directory"

pwd

echo "your user and group IDs"

id -a

echo "your environment variables"

printenv

echo "your args"

for v; do echo "$v"; done

) >/tmp/your_applescript_environment.txt


then run your script using a FULL PATH to the script and go look at the file generated in /tmp to see what your environment looks like. If you have dependencies on any environemnt variables NOT present in the .txt file you will have to modify your script so it does not need them or it gets the information from another source.

Aug 23, 2012 2:05 PM in response to BobHarris

thanks a lot guys, you're saving me, because I'm in a *real* hurry...

can you help me launch this from an applescript?


****

#!/bin/bash



# {{{ ## setup

# DSSTORE_REEXECUTE: set after kill begaviour.

#

# 0 - no execution of DSStore after HUNG detected and killed

# 1 - execute new DSStore process after HUNG killed

#

DSSTORE_REEXECUTE=0



# 1 - yes

# 0 - no

KILL_DSSTORE=1



# callback script

HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK=/.hung.app

#

LOOP_SLEEP_TIME=1

#

R_STATE_LOOP_SLEEP_TIME=0.2

#

R_STATE_KILL_COUNTDOWN_START=100

#

DSSTORE_BINARY_FILE="/Users/DSStore"

# }}}

# {{{ MASQUE SIGINT

function keyboard_quit_signal_handler(){

# SIGINT

echo Keyboard C-c received.

rm -v $TMP1 $TMP2 2>/dev/null

exit 3;

}

trap keyboard_quit_signal_handler SIGINT

# }}}

# {{{ function: wait_for_DSStore

function wait_for_DSStore(){

while true ;

do

echo In function: wait_for_DSStore. 1>&2

x=$(ps ax| grep DSStore|grep -v grep)

PX=${x::6}

if [ "$PX" == "" ] ; then

sleep 0.5;

continue;

else

echo $PX

echo Process found. PID: $PX 1>&2

return 0;

fi

done

}

# }}}

# {{{ internal variables



export R_COUNTER_START=$R_STATE_KILL_COUNTDOWN_START

if [ $DSSTORE_REEXECUTE -eq 0 ] ; then

export DSSTORE_CMD_PREFIX=echo

fi

export BIN="$DSSTORE_CMD_PREFIX $DSSTORE_BINARY_FILE"

export TMP1=/tmp/$0.$USER.$$.1

export TMP2=/tmp/$0.$USER.$$.2

export R_COUNTER=$R_COUNTER_START

export R_STATE=0

export SLEEP_T=$LOOP_SLEEP_TIME

export STEPS=0



# }}}

# {{{ pre-loop commands (set PX (pid))

PX=$(wait_for_DSStore)

# }}}

# {{{ main loop (while true)

while true;

do

# {{{ while loop no. 2

while true;

do

# {{{ speed up/slow down : R_STATE= 0|1



if [ $R_STATE -eq 1 ] ;

then

export SLEEP_T=$R_STATE_LOOP_SLEEP_TIME ;

else

export SLEEP_T=$LOOP_SLEEP_TIME ;

fi



# }}}

# {{{ set PC (%cpu)

x=$(ps -p $PX -o,%cpu=)

export PC=${x/,[0-9]*/}

# }}}

# {{{ PROCESS GONE :: PC check : no value => PX set && PC set

if [ "$PC" == "" ] ; then

echo Process $PX is gone.

export PX=$(wait_for_DSStore)

x=$(ps -p $PX -o,%cpu=)

export PC=${x/,[0-9]*/}

fi

# }}}

# {{{ set PS (stat)

x=$(ps -p $PX -o,stat=)

PS=${x::1}

# }}}

# {{{ DEBUG output [kill:1]

echo "=+> $(date) :: PID:$PX PC:$PC PS:$PS: C:$R_COUNTER"

# }}}

# {{{ Check PS and PC (R && -gt 50)

if [ "$PS" == "R" ] && [ "$PC" -gt 50 ] ; then

# R STATE

export R_COUNTER=$[ $R_COUNTER - 1 ]

export R_STATE=1

if [ $R_COUNTER -eq 0 ] ; then

echo "=========================================="

echo " ERR DETECTED " $(date)

echo "=========================================="

echo kill -9 $PX

if [ $KILL_DSSTORE -ne 0 ] ; then

kill -9 $PX

fi

export PX=""

echo "=> EXECUTING CALLBACK SCRIPT: $HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK"

$HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK

echo "=< CALLBACK SCRIPT FINISHED WITH EXIT CODE: $?"

break ;

fi

else

export R_STATE=0

export R_COUNTER=$R_COUNTER_START

fi

# }}}

# {{{ loop 2 finish: sleep && STEPS++

sleep $SLEEP_T

export STEPS=$[ $STEPS + 1 ]

# }}}

done

# }}} while loop no. 2

# {{{ DSStore execution / PID detection / couter&state reset

echo executing new app process...

$BIN & # exec new app process

export PX=$(wait_for_DSStore)

export R_COUNTER=$R_COUNTER_START

export R_STATE=0

# }}}

done

# }}} end of main loop



# Local Variables:

# mode: shell-script

# mode: folding

# End:


***

Aug 23, 2012 2:58 PM in response to personal_username

Ok, a couple of things...


are you dunning this from within test.app? If so, then you want to use the path to resource command that you'll find in Standard Addition osax.


your shell command looks weird to me. it ought to be:


do shell script "/Users/test.app/Contents/.script_dog/watchdog.sh &> /dev/null &"


no?


There are a couple of questionable things in the script. For instance, you have a routine to catch keyboard input, but running from AppleScript it won't have a console and no possibility of keyboard input, so for all you know it's reading random noise. Also, I would want to test that the ps command is doing the same thing called from applescript that it does from terminal. Sometimes utilities like that present different output formats when they are not writing to a terminal.

Aug 23, 2012 3:18 PM in response to twtwtw

thanks twtwtw,


i tried to put it on the desktop, to be sure the problem was it being inside an app.

than I put the do shell script as you suggested... unfortunatley without results...


again, it worked if launched from the terminal, BUT it didn't if launched from the applescript.

If it may help the script is looking for an application not responding, if so it kills it.

Works on command line, doesn't if launched from applescript... very strange, isn't it?

Any other way to launch it?

Aug 23, 2012 3:55 PM in response to personal_username

ok. if I can't solve this I'd rather be ok with this other thing:


how do i call an applescript from here?

it was supposed to do so, but it is actually not working...

this is the line


***

HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK=/hung.app

***


where "hung.app" is the applescript application in the same folder of this sh

but it is actually not launching... any idea about this?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

running shell from applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.