再论Mac OS下如何将shell可执行文件转换成直接运行的APP?

今天看见一个更加直接的转换方法,转载到这里来备份下,后续有需要可以实践一下,不过看其方法是比较完整和实用的。

fan'yi

是否曾经想到过让应用程序直接运行而不是多个shell命令?

仅仅将您的Shell脚本转换为Mac应用程序并像其他任何Mac应用程序一样使用它会不会很酷?

因此,让我们今天开始学习如何使用Shell脚本创建Mac应用程序。

在开始创建应用程序之前,我们将快速了解Mac中应用程序文件夹的结构:

对于Mac中的所有应用程序,最基本的文件夹结构如下所示。

Ever thought of having an application to run it directly instead of multiple shell commands?

And won’t it be cool to just convert your shell script into a Mac application and use it just like any other mac applications?

So let’s learn today to create mac app from a shell script.

Before jumping into the application creation, we will quickly see how an application folder structure in Mac looks like:

The bare minimum folder structure is something like below that you will find for any applications in Mac.

(app-name).app
├── Contents
│   └── MacOS
│   └── (app-name)
└── Icon

现在开始吧。 我们的应用程序主要需要三件事:

可执行的Shell脚本,它将使您的Shell脚本成为应用程序。
您要使它作为应用程序的实际shell脚本。
将图标添加到您的应用程序。 (不是强制性的,但看起来像真正的应用程序一样酷)
让我们一步一步地完成这些步骤:

一个可执行的shell脚本,它将使您的shell脚本成为一个应用程序:

So now let’s get started. We need mainly three things for our application:

  1. An executable shell script which will make your shell script as an application.
  2. Your actual shell script to make it as an application.
  3. Add an icon to your application. (Not Mandatory but looks cool like real app)

Let’s walk through these steps one by one:

  1. An executable shell script which will make your shell script as an application:
     

    JavaScript

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    #!/usr/bin/env bash

     

    APPNAME=${2:-$(basename "${1}" '.sh')};

    DIR="${APPNAME}.app/Contents/MacOS";

     

    if [ -a "${APPNAME}.app" ]; then

      echo "${PWD}/${APPNAME}.app already exists. Provide some unique name if you want to create it anyway. ";

      exit 1;

    fi;

     

    mkdir -p "${DIR}";

    cp "${1}" "${DIR}/${APPNAME}";

    chmod +x "${DIR}/${APPNAME}";

     

    echo "${PWD}/$APPNAME.app";

    使用上述脚本创建一个文件并将其保存在/ usr / local / bin中。
    然后使用命令行/终端中的命令将上述文件设置为可执行文件:sudo chmod + x / usr / local / bin / appify
    注意:


    您可以给它指定任何名称,并且文件不需要扩展名。
    作为参考,我们将其命名为“ appify”
    我们会将文件保存在/ usr / local / bin中,以便我们可以直接调用“ appify”脚本。如果您想保存在用户名目录之类的其他位置,则必须使用完整路径(如“ / User /您的用户名/ appify”)调用脚本
    使其成为应用程序的实际shell脚本:
    现在,准备好具有.sh扩展名的脚本文件,并运行以下命令以使您将Shell脚本作为Mac应用程序使用。

    • Create a file with the above script and save it in /usr/local/bin .
    • Then make the above file as executable with the command from command line/Terminal: sudo chmod +x /usr/local/bin/appify
      Note:

       

      • You can give it any name and no extension is required for the file.
      • For reference, we will give it a name as ‘appify’
      • We are saving the file at /usr/local/bin  so that we can directly call ‘appify’ script. If you want to save at any other place like your username directory then you will have to call the script with its full path like ‘/User/your-username/appify’
  2. Your actual shell script to make it as an application:
    • Now have your script file with .sh extension ready and run the below command to make you shell script as a Mac application.

      JavaScript

       

      1

      appify your-shell-script.sh "Your App Name"

  3. Add an icon to your application:
    This one is very simple.

     

    • Just copy any image you want as the app icon.
    • Right-click your new application select “Get Info”.
    • Select the app icon on the top left corner by clicking it just once.
    • and then either hit ⌘ + V to paste it or go to ‘Edit’ from menu link and select paste.
    • With this, it will overwrite the default icon with your new icon.

Now your application is ready to use just like any other application.

 

I hope this post will help you learn how to create mac app from a shell script.

References:

  • https://mathiasbynens.be/notes/shell-script-mac-apps
  • https://gist.github.com/mathiasbynens/674099
  • https://stackoverflow.com/questions/30792569/converting-a-shell-script-into-a-app-file

 

转自:https://blog.csdn.net/julius_lee/article/details/104693979

原文链接:https://techtalkbook.com/create-mac-app-from-a-shell-script/

你可能感兴趣的:(Shell)