Search code examples
xcodebashapplescriptapplescript-objc

Embed a bash shell script in an AppleScriptObjC application with Xcode


I have attempted to follow the instructions on this post but I am falling short of understanding how some of the posters instructions work.

I want to be able to package the app with a prewritten bash script and then execute it, but don't follow from Step 4 onwards.


Post writes:

4. Also in your AppleScriptObjC script, add the following where appropriate:

    property pathToResources : "NSString"  -- works if added before script command

5. Where appropriate, also add the following in your AppleScriptObjC script:

    set yourScript to pathToResources & "/yourScriptFile.sh" 
    -- gives the complete unix path
    -- if needed, you can convert this to the Apple style path:
    set yourScriptPath to (((yourScript as text) as POSIX file) as alias)`

6. As an aside, you could then open the file for read using

    tell application "Finder"
        open yourScriptPath
    end tell

Questions:

  1. Where do I add the line:

    property pathToResources : "NSString"
    
  2. Do I add which of the following, and where?

    set yourScript to pathToResources & "/yourScriptFile.sh" 
    

    OR

    set yourScriptPath to (((yourScript as text) as POSIX file) as alias)
    
  3. How is it possible to execute the script itself? The mention As an aside, you could then open the file for read using only covers the Apple style path, it does not cover using the aforementioned style.


Can anyone shed a bit more light on this for me, or post a static copy of a AppDelegate.applescript file that shows how the original poster required the base code to be used? I have tried his method and looked across the internet for the past 3 weeks to no avail. I don't want to have to convert all my code for specific tools from bash scripts into AppleScript, as this would take a lot of work.

I only need to know how to reference to the script file (for example myBashScript.sh) in my app, which would reside in the application and be included by Xcode at time of compilation.


Solution

  • I ended up bringing all the information together and now have a solution.


    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

    Using this information, below is a copy of a vanilla AppDelegate.applescript in my AppleScriptObjC Xcode project:

    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)
        -- Insert code here to do any housekeeping before your application quits 
        return current application's NSTerminateNow
    end applicationShouldTerminate_
    
    end script