Is there a way to package bash shell scripts with AppleScriptObjC app?

I have been trying to follow a series of instructions posed on StackOverflow that show how the user was able to completely package their bash shell script inside of an ASOC application built with Xcode.

I have tried to follow these instructions and asked in a comment for a post that could help: http://stackoverflow.com/questions/18362914/is-there-a-way-to-package-bash-shell-scripts-with-applescriptobjc-app-on-macosx

I wasn’t able to get a reply from the original poster, so I started my own thread on StackOverflow too: http://stackoverflow.com/questions/19739287/embed-a-bash-shell-script-in-an-applescriptobjc-application-with-xcode/19754006?noredirect=1#comment29372277_19754006

I have a copy of a raw AppDelegate.applescript file below. Can anyone please show me, what changes are needed to be made, so that it can reference into itself, to link to a bash file so that I can link it to a button with:

onButtonHandlerBUTTONNAME_(sender)
do shell script "/path/to/shell/myScript.sh"
endButtonHandlerBUTTONNAME

This in turn would open up myScript, but using the code blow, how would I edit it, so it can reference to the myScript.sh file that I would package into my application with Xcode. I know how to add the file to the bundle etc etc, I just get lost around his step #4, and I’m getting lots of helpful answers, but nobody is giving me a clear and concise reply. I just need a template that works, and that way I can go back over all the answers and see how it works. It’s been quite a few weeks since I had set out to get this to work. What should be relevantly simple - has turned out to be quite difficult.

I’m very new to AppleScriptObjC. Bash scripting on the other hand, I would say well versed. I would like to be able to present my script solutions for people in nice simple packages and not entirely rely on rewriting everything into AppleScript code.

[code]script AppDelegate
property parent : class “NSObject”

    on applicationWillFinishLaunching_(aNotification)
            -- Insert code here to initialize your application before any files are opened
    end applicationWillFinishLaunching_
   
    on applicationShouldTerminate_(sender)
            -- Insert code here to do any housekeeping before your application quits
            return current application's NSTerminateNow
    end applicationShouldTerminate_

end script[/code]

I just did 1 more try at this, and realised I am a complete n00b. I was just missing 1 line of code. I’m going to post the answer here, and I’ll also post it on StackOverflow to show everyone what the answer is for future searches.

This takes into consideration the following facts:
firstScript = variable name that points to a script called scriptNumberOne.sh
scriptNumberOne.sh = the script that I have embedded into my application to run
ButtonHandlerRunScript_ = the name of the Received Action in Xcode
pathToResources = variable that points to the internal Resources folder of my application, regardless of it’s current location

[code]script AppDelegate
property parent : class “NSObject”
property pathToResources : “NSString”

on applicationWillFinishLaunching_(aNotification)
set pathToResources to (current application’s class “NSBundle”'s mainBundle()'s resourcePath()) as string
end applicationWillFinishLaunching_

on ButtonHandlerRunScript_(sender)
set firstScript to pathToResources & “/scriptNumberOne.sh”
do shell script firstScript
end ButtonHandlerRunScript_

on applicationShouldTerminate_(sender)
return current application’s NSTerminateNow
end applicationShouldTerminate_

end script[/code]

You could set the variable with one line


set firstScript to (current application's NSBundle's mainBundle()'s bundlePath()'s stringByAppendingPathComponent_("Contents/Resources/scriptNumberOne.sh"))

I’ve found sometimes though that this doesn’t always work in a “do shell script” statement if there are spaces or special characters in the file path name, so I usually do the following to the variable before trying a “do shell script”


            set firstScript to firstScript as Unicode text
            set firstScript to quoted form of (POSIX path of (firstScript))

There must be some better way to run a shell script than with using ‘do shell script’.

NSTask can be a replacement in many cases from what I’ve seen.

Hi x74353,

Sounds interesting. I’ll look that up.

Edited: it is interesting! Looks like it has everything and more for running shell scirpts.

Thanks and have a good day,
kel

Yes, that’s the way to do it, provided you don’t need elevated privileges. FWIW, there’s a chapter on the topic with examples in my book.