Running a process in the background

Thanks to help received to my earlier posts I am now nearly finished my first (and hopefully not last) AppleScript project. However, I have one problem outstanding.

My AppleScript Application is a GUI front end to a command line program which takes 40 minutes to execute. In order not to “freeze” my AppleScript Application I run it as background process. The code to invoke it is:

do shell script myScript >/dev/null 2>&1 & echo $!
set PID to the result

I save the PID so that I can kill the process if the user quits the AppleScript Application. Because myScript creates child processes of its own, I kill using the following:

do shell script "kill -9 -" & PID

The last dash ensures that all the processes in the same group as PID (i.e. all its children) are killed also.

My AppleScript Application actually needs to invoke myScript several times in sequence. Therefore, an ivocation of myScript triggers one of two Apple Events

  1. “doneIt” on success.

  2. “error” if there is an error.

In order for my AppleScript application to receive the above two events I have two hidden buttons in the main window.

The last thing I need to do is where I am having a problem. I need to send a result back from myScript with each event. I tried to tell the Apple Script application to set some properties but I can’t get this to work.

At the moment myCommand looks like:

[code]#!/bin/sh
function doneIt() {
/usr/bin/osascript <<EOT
tell application “$1”
tell button “done” of window “main” to perform action
end tell
EOT
}

function error() {
/usr/bin/osascript <<EOT
cat <<EOT
tell application “$1”
set errMsg to “$2”
tell button “error” of window “main” to perform action
end tell
EOT
}

theApp=“My AppleScript Application”
command=“$1 $2 $3 $4”
out=$command

if [ $? -eq 0 ]
then
doneIt $theApp
else
error $theApp $out
fi[/code]
What is wrong? How do tell “My AppleScript Application” to set properties from my shell script?

I can’t tell you precisely what to do because a) I’ve never done it before and b) I don’t have your code to see the rest of what you’re doing, but I can show you an example that works.

Save the following code as a stay-open application and launch it:

on run
    -- do nothing
end run

on doDone()
    beep
    display dialog "Done!"
end doDone

on doError()
    beep 2
    display dialog "Error!"
end doError

on idle
    return 60
end idle

The run the following shell script:

osascript -e 'tell application "myap" to doDone()'

Off hand I don’t see any significant difference between my code and your sample other than the fact my shell script runs a handler in the AppleScript app whereas you try to click a button, but it does prove it can be done. You’ll need to work through your shell script to find other differences, or maybe change your app to follow my example.
It might be as simple as changing your shell script to call the handler behind the buttons rather than trying to click the buttons themselves.

Well, the solution I use is to set the title of the button to the textual property you want to pass to it and then, in your script application, take the value from the title and do something with it:

[code]#!/bin/sh
function doneIt() {
/usr/bin/osascript <<EOT
tell application “$1”
tell button “done” of window “main” to perform action
end tell
EOT
}

function error() {
/usr/bin/osascript <<EOT
cat <<EOT
tell application “$1”
tell button “error” of window “main”
set title to “$2”
perform action
end tell
end tell
EOT
}

theApp=“My AppleScript Application”
command=“$1 $2 $3 $4”
out=$command

if [ $? -eq 0 ]
then
doneIt $theApp
else
error $theApp $out
fi[/code]
then, in your AppleScript Studio app, use code like this:

on clicked the_object
    set object_name to name of the_object as string
    if object_name = "done" then
        --do something
    else if object_name = "error" then
        set errMsg to title of the_object as string
        beep
        display alert "Error" as critical message errMsg default button "OK"
        return
    end if
end clicked

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]