Cannot use osascript within a shell script

Ultimately, I would like to use launchd to call AppleScripts, but it appears that although osascript works famously from the Terminal, when saved as a shell script, it fails. For example, I am using a short test AppleScript, saved as 2.scpt on the Desktop:

on run
    tell application "Finder"
        display dialog "Here is the second script"
    end tell
end run

I used a Finder tell for the display dialog, otherwise osascript could not call it at all, even from Terminal. I used the run handler in the hopes that osascript could call it from this shell script:

[code]#!/bin/sh

this should run the Second Script on the desktop

osascript ‘~/Desktop/2.scpt’[/code]
So, anyway, this command from Terminal works fine:

osascript '~/Desktop/2.scpt'

However, when I try to run it from the shell script, still using the command line in Terminal:

secondscript.sh

I get this error:
osascript: ~/Desktop/2.scpt: No such file or directory

When I incorporate the shell script into a launchd plist, this is what happens when called:

launchd[701]: RunSecondScript: execve(): No such file or directory

I am not a trained UNIX programmer, but I can fashion basic shell scripts. All I want to do is to use launchd to automate the execution of a few scripts on my machine, but all the scripts I want to launch are AS, and launchd does not seem to run those in its native form. I also tried saving my test script as an application, and launchd still did not recognize it.

Any advice or instruction is welcome and appreciated.

hi craig,

it seems that you are leaving a lot out of your description of the shell scripts. for example, how are they called? this is important because the user that calls them is directly related to this code:

~

this means, ‘in my home directory’. if you wrote a scirpt, for example, and put it on jimbob’s desktop, and then called your script as root, you would get an error similar to what you describe. this is because there is no script residing on root’s desktop, it’s on jimbob’s.

why not try to give an ‘absolute’ path rather than a ‘relative’ one from the root of the filesystem tree and see if that doesn’t work better. in other words, a POSIX path like:

/usr/bin/osascript /Users/jimbob/Desktop/2.scpt

i think you may have more than one problem going on here, so post your results & i’ll follow up w/ you tomorrow.

thanks waltr, I believe you are correct, and is boiling down to a permissions issue, or a group of permissions issues.

You were correct that by providing the full path to both the shell script and launchd .plist files, everything worked perfectly. So, I decided to point launchd to the applescript file, bypassing the shell script altogether (and providing the full path). Here is the error received in the Terminal:

TCHs-Computer:~/library/launchdaemons casdvm$ launchd[853]: RunSecondScript: execve(): Permission denied
launchd[857]: RunSecondScript: execvp(“/Users/casdvm/desktop/2.scpt”, …): Permission denied
Workaround Bonjour: Unknown error: 0

I admit that I do not understand the complexities of UNIX permissions, so I did the chmod command to both the applescript and the .plist files, using the 755 code:

chmod 755 /Users/casdvm/library/launchdaemons/SecondScript.plist chmod 755 /Users/casdvm/desktop/2.scpt
That is the same code I use for all my shell scripts when they are created and saved in the ~/bin directory.

The interesting part came when I loaded the .plist file into the launchd system after the chmod:

TCHs-Computer:~/library/launchdaemons casdvm$ /Users/casdvm/desktop/2.scpt: /Users/casdvm/desktop/2.scpt: cannot execute binary file
launchd[937]: RunSecondScript: exited with exit code: 126
launchd[937]: RunSecondScript: 9 more failures without living at least 60 seconds will cause job removal

It did indeed try 9 more times before yielding

launchd[937]: RunSecondScript: too many failures in succession

So, does this mean that my initial assumption that launchd cannot execute native AppleScripts is correct? It seems that as long as I always supply direct paths in both the shell scripts and the launchd .plist files, I can get my AS scripts to run on a schedule, so I really don’t mind that.

I also don’t believe it is a big problem, although I have become accustomed to the ever-so-nifty AS [path to] command to make the scripts portable in the first place.

Thank you for your advice, and any other input you may have.