建立一個應用程式,並自動執行多個相同Mac應用程式或不同Mac應用程式的多個例項,並將它們放置在螢幕上你想要的任何位置。
這是我最近為一位朋友建立的一個簡單應用程式,他需要定期開啟同一應用程式的多個例項,並將它們放置在便於檢視的位置。雖然手動操作並不困難,但如果每天都要重複這一過程,就會變得枯燥乏味。
這個應用程式可以自動完成任務,每次執行時都能準確地將應用程式定位到我想要的位置。
請觀看下面的視訊(加速 5 倍),瞭解該程式執行時的具體操作。
如果你也想建立類似的設定,無論是開啟同一 app 的多個例項,還是開啟不同 app 的組合,本文將指導你完成整個過程。我將提供所使用的 AppleScript,並解釋如何根據自己的需要進行定製,以便自動開啟和排列應用程式。
前提條件/要求
以下是實現這一功能所需的一切:
應用程式
- Automator– 這個內建的 macOS 應用程式允許你建立自定義工作流程,並將其轉化為可執行的應用程式。
- Magnet– 通過這款應用,你可以使用鍵盤快捷鍵將 Mac 桌面上開啟的視窗安排到不同的位置和大小。
- 程式碼編輯器– 編輯稍後將新增到 Automator app 的 AppleScript 需要程式碼編輯器。如果沒有安裝程式碼編輯器,Notes app 也可以使用。
AppleScript
下面是我使用的 AppleScript 的完整版本,稍後我會詳細介紹。
tell application "Cricut Design Space" to activate delay 15 -- Wait 15 seconds for the app to fully launch tell application "System Events" -- Step 2: Simulate Control + Option + U key code 32 using {control down, option down} -- U key has key code 32 delay 5 -- Short delay after keypress -- Step 3: Open first new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 5 -- Wait 2 seconds for the new window to open -- Step 4: Simulate Control + Option + I key code 34 using {control down, option down} -- I key has key code 34 delay 5 -- Short delay after keypress -- Step 5: Open second new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 5 -- Wait 2 seconds for the new window to open -- Step 6: Simulate Control + Option + J key code 38 using {control down, option down} -- J key has key code 38 delay 5 -- Short delay after keypress -- Step 7: Open third new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 5 -- Wait 2 seconds for the new window to open -- Step 8: Simulate Control + Option + K key code 40 using {control down, option down} -- K key has key code 40 end tell
1. 啟動 Automator 應用程式
啟動 Automator 應用程式,選擇應用程式,然後點選 Choose。
在操作選項卡下搜尋 Run AppleScript,然後將其選中並拖動到右側面板。
2. 編輯 AppleScript
現在,參照上文提供的 AppleScript,你需要對其進行編輯,以滿足你的要求。
讓我為您詳細介紹一下需要修改的內容,並附帶說明。以粗體標出的程式碼就是你需要編輯以符合要求的程式碼 。
Step 1
tell application "Cricut Design Space" to activate delay 15 -- Wait 15 seconds for the app to fully launch
注:Cricut Design Space 和 15 需要根據自己的實際情況進行修改。
首先,將 Cricut Design Space 替換為您要使用的應用程式名稱。
如何找到應用程式的準確名稱?很簡單。進入Applications/資料夾,複製應用程式名稱(不含 .app 副檔名),然後替換指令碼中的名稱。
delay 15 表示指令碼將等待 15
秒,讓應用程式完全載入。如果應用程式較輕,載入速度較快(如 Google Chrome 瀏覽器、Safari),可以減少延遲時間;如果應用程式較重(如 Adobe Photoshop、Adobe Premiere),可以增加延遲時間。
到目前為止,我們已指示指令碼啟動我們想要的應用程式,並新增了 15 秒的延遲,以確保在執行其餘命令之前有足夠的時間完全載入。
接下來,我們用步驟編號標記了剩餘的操作,並將它們括在指令碼中:
tell application "System Events"
和
end tell
下面解釋了每個步驟的作用,以及你需要編輯的部分,以滿足你的需要。
注:有關 AppleScript 關鍵程式碼的完整列表,請參閱本頁末尾的表格。
Step 2
-- Step 3: Open first new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 2 -- Wait 2 seconds for the new window to open
模擬按鍵 Control + Option + U,使用 Magnet 應用將第一個開啟的應用定位在螢幕右上角。
Step 3
-- Step 3: Open first new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 2 -- Wait 2 seconds for the new window to open
模擬點選應用程式的導航選單檔案 > 新視窗,開啟應用程式的第二個副本,然後等待 2
秒鐘載入。
Step 4
-- Step 4: Simulate Control + Option + I key code 34 using {control down, option down} -- I key has key code 34 delay 2 -- Short delay after keypress
模擬按鍵 Control + Option + I,將第二個開啟的應用程式定位在螢幕左上方,然後等待 2
秒鐘,確保動作完全執行。
Step 5
-- Step 5: Open second new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 2 -- Wait 2 seconds for the new window to open
模擬點選應用程式的導航選單檔案 > 新視窗,開啟應用程式的第三個副本,然後等待 2
秒鐘載入。
Step 6
-- Step 6: Simulate Control + Option + J key code 38 using {control down, option down} -- J key has key code 38 delay 5 -- Short delay after keypress
模擬按鍵 Control + Option + J,將第三個開啟的應用程式定位在螢幕左下方,然後等待 5
秒鐘讓操作完全執行。
Step 7
-- Step 7: Open third new window via File > New Window tell process "Cricut Design Space" click menu item "New Window" of menu "File" of menu bar 1 end tell delay 2 -- Wait 2 seconds for the new window to open
模擬點選應用程式的導航選單檔案 > 新視窗,開啟應用程式的第四個也是最後一個副本,然後等待 2
秒鐘載入。
Step 8
-- Step 8: Simulate Control + Option + K key code 40 using {control down, option down} -- K key has key code 40
模擬按鍵 Control + Option + K,將第四個開啟的應用程式置於螢幕右下方。
3. 替換和測試指令碼
將編輯好的指令碼貼上到 Run AppleScript 操作中,替換原有的指令碼。直接點選頂部的 Run 按鈕或螢幕右上角的 Run 按鈕,測試是否正常執行。如果不行,請除錯指令碼並再次測試。
一旦按預期成功執行,進入頂部選單欄,單擊檔案 >儲存。確保檔案格式設定為應用程式,以便儲存為可執行程式。
4. 編輯隱私和安全設定
由於該程式會在 Mac 上執行自動按鍵操作,因此 macOS 預設會阻止它。為確保其正常工作,你需要授予該應用必要的許可權。
開啟系統設定,導航至隱私與安全 > 輔助功能。啟用該 app,如果尚未列出,可點選底部的 “+”按鈕新增。
AppleScript 按鍵程式碼參考
按鍵 | 按鍵程式碼 | 按鍵 | 按鍵程式碼 |
---|---|---|---|
A | 0 | B | 11 |
C | 8 | D | 2 |
E | 14 | F | 3 |
G | 5 | H | 4 |
I | 34 | J | 38 |
K | 40 | L | 37 |
M | 46 | N | 45 |
O | 31 | P | 35 |
Q | 12 | R | 15 |
S | 1 | T | 17 |
U | 32 | V | 9 |
W | 13 | X | 7 |
Y | 16 | Z | 6 |
1 | 18 | 2 | 19 |
3 | 20 | 4 | 21 |
5 | 23 | 6 | 22 |
7 | 26 | 8 | 28 |
9 | 25 | 0 | 29 |
等號 (=) | 24 | 減號 (-) | 27 |
右中括號 (]) | 30 | 左中括號 ([) | 33 |
引號 (‘) | 39 | 分號 (;) | 41 |
逗號 (,) | 43 | 句號 (.) | 47 |
斜杆 (/) | 44 | 反斜槓 (\) | 42 |
製表鍵 | 48 | 空格 | 49 |
回車鍵(Enter) | 36 | 退出鍵(Esc) | 53 |
刪除鍵 | 51 | 向前刪除鍵 | 117 |
Home鍵 | 115 | 結束鍵 | 119 |
上一頁 | 116 | 下一頁 | 121 |
左方向鍵 | 123 | 右方向鍵 | 124 |
下方向鍵 | 125 | 上方向鍵 | 126 |
評論留言