【问题标题】:Embed a bash shell script in an AppleScriptObjC application with Xcode使用 Xcode 在 AppleScriptObjC 应用程序中嵌入 bash shell 脚本
【发布时间】:2013-11-05 17:24:00
【问题描述】:

我已尝试按照 this post 上的说明进行操作,但我无法理解某些海报说明的工作原理。

我希望能够使用预先编写的 bash 脚本打包应用程序,然后执行它,但不要从 步骤 4 开始。


帖子写道:

4.同样在您的 AppleScriptObjC 脚本中,在适当的地方添加以下内容:

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

5.在适当的情况下,还在您的 AppleScriptObjC 脚本中添加以下内容:

    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.顺便说一句,您可以使用

打开文件进行读取
    tell application "Finder"
        open yourScriptPath
    end tell

问题:

  1. 我在哪里添加行:

    property pathToResources : "NSString"
    
  2. 我要添加以下哪一项,在哪里添加?

    set yourScript to pathToResources & "/yourScriptFile.sh" 
    

    set yourScriptPath to (((yourScript as text) as POSIX file) as alias)
    
  3. 如何执行脚本本身?提及As an aside, you could then open the file for read using仅涵盖Apple样式路径,不涵盖使用上述样式。


谁能帮我解释一下,或者发布一个AppDelegate.applescript 文件的静态副本,显示原始海报如何要求使用基本代码?我已经尝试了他的方法,并在过去 3 周内浏览了互联网,但无济于事。我不想将特定工具的所有代码从 bash 脚本转换为 AppleScript,因为这需要大量工作。

我只需要知道如何在我的应用程序中引用脚本文件(例如myBashScript.sh),该脚本文件将驻留在应用程序中并在编译时被 Xcode 包含。

【问题讨论】:

    标签: xcode bash applescript applescript-objc


    【解决方案1】:

    我认为你应该使用命令path to resource <specifiedResource>

    请参阅标准添加,path to resource

    您可以通过set myVariableName to path to resource "myBashScript.sh" 设置它,或者只使用命令而不是您的属性,以便它始终指向正确的位置(用户可以在运行时移动您的应用程序......哈哈)。


    补充:

    我在 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
    

    每当我想运行捆绑在我的应用程序中的脚本时,我都会这样做:

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

    run_scriptfile(this_scriptfile) 在一切正常时返回true

    【讨论】:

    • 你能给我举个例子吗?这是一个空白应用程序的粘贴箱:pastebin.com/6BZr5e8L
    • 打开 AppleScript 编辑器,按 shift-command-l 查看库,打开“标准添加”,搜索“资源路径”。阅读该信息。
    • or just use the command instead of your property 是什么意思?
    • 顺便说一句:我使用 AppleScript Editor 来创建我的应用程序,所以这里可能有所不同。但是你会看到它是否有效。
    【解决方案2】:

    我最终将所有信息汇总在一起,现在有了解决方案。


    这考虑了以下事实:

    firstScript = 指向名为 scriptNumberOne.sh 的脚本的变量名称
    scriptNumberOne.sh = 我嵌入到应用程序中以运行的脚本
    ButtonHandlerRunScript_ = Xcode 中Received Action 的名称
    pathToResources = 指向我的应用程序内部资源文件夹的变量,无论它的当前位置是什么 p>

    使用此信息,下面是我的 AppleScriptObjC Xcode 项目中的原版 AppDelegate.applescript 的副本:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-08-24
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 2012-02-10
      • 2011-05-14
      相关资源
      最近更新 更多