How to Launch GUI Applications from the Terminal?

如何在終端機打開圖形界面的應用程式?

一般而言, 打開應用程式要透過 Finder 進到 Applications 慢慢找某特定的應用程式. 然後點兩下執行它. 雖然, 常用的應用程式大部份都會依個人偏好放在 Dock 上方便執行. 但是, 總是會有些程式放在 Dock 上. 所以透過 Finder 尋找應用程式然後執行. 某種程度而言算是有點瑣碎. 對有些重度使用 Terminal 的使用者會認為, 如果能用某指令就能打開圖形界面的應用程式那是最好不過的. 尤其透過遠端登入要打開某個程式.那更是方便. 因為遠端沒辦法直接對某應用程式直接執行.

至於, 該怎麼遠端或是透過終端機打開圖形界面應用程式? 簡單來說就使用 open 這個指令. 先大概看一下基本的說明如下 :

$ open
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b ] [-a ] [filenames] [--args arguments]
Help: Open opens files from a shell.
      By default, opens each file using the default application for that file.  
      If the file is in the form of a URL, the file will be opened as a URL.
Options: 
      -a                Opens with the specified application.
      -b                Opens with the specified application bundle identifier.
      -e                Opens with TextEdit.
      -t                Opens with default text editor.
      -f                Reads input from standard input and opens with TextEdit.
      -R, --reveal      Selects in the Finder instead of opening.
      -W, --wait-apps   Blocks until the used applications are closed (even if they were already running).
          --args        All remaining arguments are passed in argv to the application's main() function instead of opened.
      -n, --new         Open a new instance of the application even if one is already running.
      -g, --background  Does not bring the application to the foreground.
      -h, --header      Searches header file locations for headers matching the given filenames, and opens them.

上面的內容就稍微自己看一下吧 : )

就示範幾個範例, 讓你參考.
開啓計算機

open -a Calculator

開啓 photoshop

open -a Adobe\ Photoshop\ CS4

用 Smultron 打開桌面上的 README.txt 檔案

open -a Smultron ~/Desktop/README.txt

打開某個網址

open http://liho.tw/

打開某張圖

open sexy.jpg

當然你可以把這些指令編輯道 .profile 裡面. 以方便日後開啓程式.

alias photoshop="open -a Adobe\ Photoshop\ CS4"

之後打 photoshop 就可以開啓photoshop.

遠端開啓的話, 也是一樣的方式. 只是記得在 System Preferences 開啓 Remote Login 的選項. 讓你自己能從遠端登入開啓圖形應用程式.