Embed a bash shell script in an AppleScriptObjC application with Xcode

。_饼干妹妹 提交于 2019-12-23 05:46:27

问题


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.


回答1:


I think you should use the command path to resource <specifiedResource>.

See Standard Additions, path to resource.

You could set it by set myVariableName to path to resource "myBashScript.sh" or just use the command instead of your property so it points always to the right place (a user could move your app while running... lol).


ADDITION:

I did it that way in my AppleScript-Application:

on run_scriptfile(this_scriptfile)
    try
        set the script_file to path to resource this_scriptfile
        return (run script script_file)
    end try
    return false
end run_scriptfile

Whenever I want to run a script that is bundled within my app I do this:

if my run_scriptfile("TestScript.scpt") is false then error number -128

run_scriptfile(this_scriptfile) returns true when everything worked.




回答2:


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


来源:https://stackoverflow.com/questions/19739287/embed-a-bash-shell-script-in-an-applescriptobjc-application-with-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!