running an AppleScript from a c-shell script

I am trying to run an AppleScript from a c-shell script (I have saved the Applecript as application). Well, the AppleScript is being run by the c-shell, however, the shell script doesn’t wait for the applescript to be terminated, and keeps performing the following commands.
In the meantime I have added the command “sleep(60)” immediately after the call to the AppleScript, and it works, but I am more than positive that there is a better way to do it. any suggestions?

Not sure if this helps, but could you call the remaining c-shell script from the applescript

Chris

There is a command line tool called osascript which will run a compiled AppleScript file. I’m pretty sure it waits for the compiled script to finish its process. I don’t know about in the case of a script application. Like any application, it may or may not be “stay open.”

Since AppleScript applications do not interact with stdin/stdout/stderr they detatch from the calling script and the shell script will continue before the AppleScript app finishes.

One option may be to use osascript and pass it the path to the compiled script, rather than launching the AppleScript application. You still don’t hook to stdin/stdout but I believe osascript will hang around until the script finishes:

#!/bin/sh
osascript /path/to/script.scpt
echo "done"

unfortunately the osascript command didn’t solve my problem.
the shell script doesn’t wait for the compiled script to finish its process.
Is there a way to use the pid of the appleScript file?

my mistake, it does work!!!
Thanks :lol:

Here I am trying to call an applescript function from a shell script.

Here is a shell script written by the author of the great SuperDuper cloning software:

if [ -e $HOME"/Volume Icons/$3.VolumeIcon.icns" ]; then
	cp $HOME"/Volume Icons/$3.VolumeIcon.icns" "$4/.VolumeIcon.icns"
fi

What I want to do is call my AppleScript function as with

copyAllFiles($3, $4)

which function is in another file named “set_icon_script.scpt”

from within a shell script so I can benefit from the passed $3 and $4 parameters.

Thanks for listening.

hi john,

have you tried using a “Here Document”?

here is an example of a bash script that will display the dialog, “Hello World”:

[code]#!/bin/bash

myVar=“Hello”
myOtherVar=" World"

/usr/bin/osascript <<EOF
tell application “Finder” to display dialog “$myVar” & “$myOtherVar”
EOF[/code]
cheers.

I hope I have found the place to answer my question about using AppleScript within a shell script. In a hopefully concise fashion, here is my much abbreviated shell script:

##################
#!/bin/sh
echo “Restoring icon for $3…”

usage: -e ‘copyAllFiles “iMac Internal HD” “iMac_Internal_DU”’

osascript <<EOT

on copyAllFiles (theSrcDisk, theDestDisk)

set srcDisk to “$1”
set destDisk to “$2”

set ICONS to “Volume Icons”
set theICON to “.VolumeIcon.icns”

try

tell application "Finder"
    -- stuff

    editIcon("c")
    -- stuff
    editIcon("v")

end tell

on error errorMsg
	doError(errorMsg)

end try

end copyAllFiles

to editIcon(withKey)

tell application "System Events"
    -- stuff
end tell

end editIcon

on doError(errorMsg)

activate me

-- stuff

display dialog (missingItem & " does not exist!") buttons {"Error"} default button "Error" with icon stop

end doError

end copyAllFiles

EOT

% osascript copyAllFiles “iMac Internal HD” “iMac_Internal_DU”

-e ‘copyAllFiles “iMac Internal HD” “iMac_Internal_DU”’
##################

Now, I have saved my most basic question to last; namely

IF the above is accurate, how do I call within Apple’s Terminal the above assuming that it’s saved in a file named “set_icon.pl”

hi john,

you are a bit confused. something called “set_icon.pl” should start with this:

#!/usr/bin/perl

and have actual perl code inside. your ‘here’ document is a shell script and should be called “set_icon.sh”. this is mainly because it starts with this

#!/bin/sh

now, name it correctly and call it by putting it in your path or like this:

$/path/to/set_icon.sh

cheers.

EDITED: because obviously i am confused too!

Hi waltr

Lay you odds I am MUCH more confused than all of us … but as you have said “The goal is in the journey” … so …

I’ve done the following with my “set_icon_shell_script.txt”:

  1. duped it (so I can retain the original .txt)
  2. chmod +x on the copy within Terminal
  3. changed the copy’s suffix to .command as in “set_icon_shell_script.command”

Then I double-clicked the result and it worked, as described below.

What I was trying to do initially was access within the shell script a handler in the AppleScript and that did not work for me.

Then I simply accessed the whole AppleScript from the shell script and within the AppleScript’s on run handler accessed the specific handler … and this worked.

Here’s the shell script:

#!/bin/sh
echo "Restoring icon ..."

osascript ~/Library/Application\ Support/SuperDuper\!/Saved\ Settings/set_icon_applescript_from_shell.scpt "iMac Internal HD" "iMac_Internal_DU"

Next, is the AppleScript code:

on run parms
	set theSrcDisk to item 1 of parms
	set theDestDisk to item 2 of parms
	
	copyAllFiles(theSrcDisk, theDestDisk)
end run

on copyAllFiles(srcDisk, destDisk)

-- code

end copyAllFiles

Anyway, it worked.

Just one thing is lacking … above I have what I call the long-way-around path for the AppleScript file within my osascript call …

Good grief, this AppleScript file is in the same Folder as my shell script, yet just using:

osascript set_icon_applescript_from_shell.scpt parm1 parm2

generates “no such file” in the Terminal. ???

John Love

hi john,

sorry to reply so late. unfortunately i’ve been sick.

one thing you could do to get the folder your script is in is ask the Finder:


tell application "Finder"
set myFolder to container of (path to me) as string
end tell

in Osascript you may need the POSIX path however:

[code]osascript <<EOF --or EOT, doesn’t matter. you could use DUFFBEER and it would work.
tell application “Finder”
set myFolder to container of (POSIX path to me) as string
end tell

EOF --or EOT or DUFFBEER, just use whichever you started with.[/code]
then just use the variable myFolder.

cheers.