再論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://techtalkbook.com/create-mac-app-from-a-shell-script/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章