将Shell脚本转换为* .app文件 [英] Converting a Shell Script Into a *.app File

查看:152
本文介绍了将Shell脚本转换为* .app文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Automator.app和Platypus.app,我可以捆绑我创建的简单外壳脚本,以便在MacBook Pro上重启wi-fi.两种最终的应用程序都可以正常运行,但是有一个明显的问题需要纠正:这些应用程序从程序外部引用了shell脚本.如何嵌入外壳脚本并从应用程序的资源中引用它,以便即使移动原始源文件也可以运行该应用程序?

Using Automator.app and Platypus.app, I have been able to bundle a simple shell script I created to power cycle wi-fi on my MacBook Pro. Both resulting apps run properly, but have one glaring issue that I want to correct: The apps reference the shell script from outside of the program. How can I embed the shell script and reference it from the app's resources so that the app can run even if the original source file is moved?

推荐答案

仅提及它,如果您在脚本中获取信息,则可以将其设置为在终端中打开.双击该脚本将运行该脚本.

Just to mention it, if you Get Info on a script, you can set it to be opened with the Terminal. This will run the script when you double-click it.

否则,将脚本打包到.app捆绑包中是微不足道的.Mac OS X会愉快地运行任何被标识为应用程序可执行文件的脚本.

Otherwise, packaging a script in a .app bundle is trivial. Mac OS X will happily run any script identified as the application's executable.

至少,您需要遵循以下结构:

At a minimum, you need to following structure in place:

  • (名称).app
    • 内容
      • MacOS
        • (名称)

        其中名为(name)的文件是您的脚本(必须必须是可执行文件,而必须必须有shebang行).(名称)在.app目录和脚本文件中必须相同:例如,如果您的应用程序目录名为"My Shell Script.app",则MacOS目录中的文件必须名为"My Shell Script",并带有无扩展名.

        Where the file called (name) is your script (which must be executable, and must have a shebang line). (name) must be identical in the .app directory and the script file: for instance, if your app directory is called "My Shell Script.app", then the file inside the MacOS directory must be called "My Shell Script", with no extension.

        如果这样做不方便,则可以使用Info.plist文件指定备用可执行文件名称.Info.plist进入目录目录:

        If this is inconvenient, it's possible to use an Info.plist file to specify an alternate executable name. The Info.plist goes in the Contents directory:

        • Wrapper.app
          • 内容
            • Info.plist
            • MacOS
              • MyScript

              如果在属性列表中将MyScript指定为 CFBundleExecutable ,则此结构(包装中名为Wrapper.app的MyScript可执行文件)将起作用:

              This structure (a MyScript executable in a wrapper called Wrapper.app) works if you specify MyScript as the CFBundleExecutable in the property list:

              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
              <plist version="1.0">
              <dict>
                  <key>CFBundleExecutable</key>
                  <string>MyScript</string>
              </dict>
              </plist>
              

              使用Info.plist文件可能是更可取的选择,因为这将使您可以重命名包装器而不会破坏它.

              Using an Info.plist file is probably preferable, as that will allow you to rename your wrapper without breaking it.

              这是一个使用/bin/sh 作为解释器的示例脚本,但是您实际上可以拥有任何内容(#!/usr/bin/swift #!/usr/bin/python 等).

              Here's one example script that uses /bin/sh as the interpreter, but you really could have anything (#!/usr/bin/swift, #!/usr/bin/python, etc).

              #!/bin/sh
              open -a Calculator
              

              该脚本将在您双击应用程序捆绑包时运行.

              The script will run as you double-click the app bundle.

              您可以将脚本中需要的其他任何内容捆绑在 Contents 目录中.如果您愿意,可以使用 Resources 目录和类似的东西来复制标准的可执行文件包布局.

              You can bundle anything else that you need with your script within the Contents directory. If you feel fancy, you can reproduce the standard executable bundle layout with a Resources directory and things like that.

              这篇关于将Shell脚本转换为* .app文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆