1. 程式人生 > 實用技巧 >藉助 AppleScript 一鍵開啟工作空間

藉助 AppleScript 一鍵開啟工作空間

我有個小毛病:同時只能在一個工程裡工作。

假如讓我開四五個 Webstorm,在工程裡 A 改個Bug,然後又到工程 B 里加個需求,再去工程 C 發個版,切來切去一會兒就懵了。

於是有了這個專案:my-applescript

使用方式

  1. 第一步:滑鼠滑動到左側底部喚起桌面(我設定的觸發角);
  2. 第二步:雙擊 “open-saas-admin”(是個指令碼,會初始化公司 saas-admin 專案的工作空間);
  3. 第三步:泡個茶或者發個呆,指令碼負責開啟 Webstorm、iTerm、Chrome、執行開發命令...
  4. 第四步:幹活兒。

如何實現

思路很簡單:用 AppleScript 控制 MacOS 中的應用程式

下邊是一段示例程式碼:

set dirPath to "/Users/rmlzy/Documents/homedo/saas-admin"

on quitApplication(name)
	tell application name
		quit
	end tell
end quitApplication

on runCmdInNewTab(cmdStrs)
	tell application "iTerm"
		activate
		delay 1
		tell current window
			create tab with default profile
			tell current session
				repeat with cmdStr in cmdStrs
					write text cmdStr
				end repeat
			end tell
		end tell
	end tell
end runCmdInNewTab

on openChrome(URL)
	tell application "Google Chrome"
		activate
		open location URL
	end tell
end openChrome

# Fire
quitApplication("iTerm")
delay 1
quitApplication("Webstorm")
delay 1
runCmdInNewTab({"pwd", "cd " & dirPath, "git pull", "npm run dev"})
runCmdInNewTab({"webstorm " & dirPath})
openChrome("http://localhost:10445")

AppleScript 的語法很簡單,套娃式的邏輯結構。

指令碼實現以後,可以在 MacOS 中的 “Script Editor” 中測試一下。

測試完畢後,點選目錄中的 “檔案”,選擇 “匯出”,匯出成應用程式到桌面,勾選“僅執行” 即可。

完工。