open 是 macOS 獨有的萬能啟動器,能啟動 .app、以預設程式開啟檔案、在 Finder 顯示目錄、在瀏覽器開啟網址、甚至指定應用程式開檔。本文整理最常用語法與實戰範例。
open [選項] 路徑或網址...
| 需求 | 指令 | 說明 |
|---|---|---|
| 啟動 App | open -a "App Name" | 不需完整路徑,Spotlight 索引即可找到 |
| 啟動 App (完整路徑) | open /Applications/iTunes.app | 直接指定 bundle 路徑 |
| 以指定 App 開啟檔案 | open -a "TextEdit" ~/notes.txt | 覆蓋預設關聯 |
| 以預設編輯器開啟文字檔 | open -e ~/notes.txt | 等同 open -a TextEdit |
| 在 Finder 顯示資料夾 | open /Applications | 或 open . 顯示當前目錄 |
| 在預設瀏覽器開啟網址 | open https://apple.com | 支援 http、https、mailto、file 等 scheme |
| 以指定瀏覽器開啟網址 | open -a "Google Chrome" https://apple.com | |
| 開啟檔案並定位 Finder | open -R /path/to/file | Reveal in Finder |
| 新建檔案並用預設程式開啟 | open -n -a TextEdit | -n 強制開新執行個體 |
| 隱藏啟動 (背景) | open -g -a "App Name" | 不帶到前景 |
| 等待 App 結束再返回 | open -W -a "App Name" | 腳本同步用 |
open -a "Google Chrome" --args --incognito https://example.com (雙減號後參數透傳給 App)。open *.pdf (依預設程式逐一開啟)。if [ -d "/Applications/Visual Studio Code.app" ]; then open -a "Visual Studio Code"; fi。osascript -e 'id of app "App Name"' (用於 open -b com.microsoft.VSCode)。xdg-open (通用)、gnome-open、kde-open。-a 指定應用,需直接呼叫執行檔或用 mimeopen。LSOpenURLsWithRole() failed with error -10810:App 簽名損壞或架構不符 (Apple Silicon 需 Rosetta 2)。duti 工具設定預設處理器 (如 duti -s com.apple.TextEdit .txt all)。熟練 open,終端機瞬間變身高效啟動器。