前陣子說過,發現了 Mac 調度中心的正確用法,在三屏顯示器下很酷,後來又實現了讓 mac 和外聯的顯示器一塊兒隨機成同一個壁紙,真是賞心悅目。html
然而,如何在桌面空間裏爲一堆項目窗口進行自動化佈局這件事一直讓我念念不忘,好在如今有了收穫。web
此處只分享腳本和用法,具體組裝須要各人自行領會,菜端上來了,怎麼吃,請自行動筷。shell
個人工做環境裏,主要涉及 Chrome
Sublime
iTerm
等軟件,如下代碼供參考。npm
方法:api
on newWindow(name, openStr, newWindowStr)
tell application "Dock"
activate
end tell
tell application "System Events"
tell process "Dock"
set frontmost to true
activate
tell list 1
tell UI element name
perform action "AXShowMenu"
delay 1
key code 126 -- up arrow
tell menu name
try
tell menu item openStr
perform action "AXPress"
-- 第一次打開程序 須要多等幾秒
delay 5
end tell
on error errMsg
tell menu item newWindowStr
perform action "AXPress"
end tell
end try
end tell
end tell
end tell
end tell
end tell
end newWindow
複製代碼
用法示例:瀏覽器
newWindow("Google Chrome", "打開", "打開新的窗口")
複製代碼
說明:bash
google 瀏覽器
窗口,這裏有兩個參數,第一個參數是當前沒有 google 瀏覽器進程存在的命令,第二個參數是若是有 google 瀏覽器進程存在的命令。方法:session
on moveBounds(name, topLeftX, topLeftY, bottomRightX, bottomRightY)
tell application name
set bounds of front window to {topLeftX, topLeftY, bottomRightX, bottomRightY}
end tell
end moveBounds
on sizePosition(name, topLeftX, topLeftY, width, height)
tell application "System Events" to tell application process name
tell window 1
set {position, size} to {{topLeftX, topLeftY}, {width, height}}
end tell
end tell
end sizePosition
複製代碼
用法示例:app
sizePosition("Google Chrome", 40, 15, 1828, 1057)
moveBounds("Google Chrome", 1519, 116, 1919, 874)
複製代碼
說明:svn
方法:
on getFrontWindow()
tell application "System Events"
repeat with theapp in (every application process whose visible is true and frontmost is true)
repeat with ew in (every window of theapp)
return ew
end repeat
end repeat
end tell
end getFrontWindow
on openChromeDevTool()
tell application "Google Chrome"
activate
end tell
tell application "System Events"
key code 53 --esc
-- 打開開發者工具
delay 1
key code 34 using {option down, command down}
end tell
-- 切換工具窗口
delay 3
set frontWindowName to name of getFrontWindow() as string
if {frontWindowName does not start with "DevTools"} then
--display dialog frontWindowName
tell application "System Events"
key code 2 using {shift down, command down}
delay 2
end tell
end if
-- 移動開發者工具
moveBounds("Google Chrome", 1920, -36, 3410, 1043)
end openChromeDevTool
複製代碼
用法示例:
openChromeDevTool()
複製代碼
說明:
方法:
on sublOpenWorkspace(name)
do shell script "\"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl\" -n --project \"/Users/AsinW/Library/Application Support/Sublime Text 3/Packages/User/Projects/" & name & ".sublime-workspace\""
end sublOpenWorkspace
複製代碼
用法示例:
delay 1
newWindow("Sublime Text", "打開", "New Window")
delay 1
sizePosition("Sublime Text", 0, 15, 1514, 1080)
delay 1
sublOpenWorkspace("jiyingshou_api3")
delay 3
sublOpenWorkspace("jiyingshou_admin")
複製代碼
說明:
Sublime Text.app
的絕對路徑,請你們自行更改成本身電腦中的對應路徑。方法:
-- 在當前面板執行命令
on iTermCmdCurrentTab(cmdStrs)
tell application "iTerm"
activate
tell current window
tell current session
repeat with cmdStr in cmdStrs
write text cmdStr
end repeat
end tell
end tell
end tell
end iTermCmdCurrentTab
-- 打開一個新標籤,並執行命令
on iTermCmdNewTab(cmdStrs)
tell application "iTerm"
activate
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 iTermCmdNewTab
-- 在當前界面橫向再切一個面板出來並執行命令
on iTermCmdInPane(cmdStrs)
tell application "iTerm"
activate
tell current window
tell current tab
tell current session
split horizontally with same profile
end tell
repeat with ss in sessions
select ss
end repeat
tell current session
repeat with cmdStr in cmdStrs
write text cmdStr
end repeat
end tell
end tell
end tell
end tell
end iTermCmdInPane
-- 指定面板高度
on iTermSetRows(num)
tell application "iTerm"
tell current session of current window
set rows to num
end tell
end tell
end iTermSetRows
-- 激活指定面板
on iTermActiveSession(num)
tell application "iTerm"
tell current tab of current window
select item num in sessions
end tell
end tell
end iTermActiveSession
複製代碼
用法示例:
delay 1
newWindow("iTerm", "打開", "New Window (Default Profile)")
delay 1
iTermCmdCurrentTab({"cd /Users/AsinW/svnBox/jiyingshou/web/pc"})
iTermCmdInPane({"cd /Users/AsinW/svnBox/jiyingshou/web/pc", "npm run dev"})
iTermSetRows(3)
iTermActiveSession(0)
delay 1
iTermCmdNewTab({"cd /Users/AsinW/svnBox/jiyingshou/web/mobile4"})
iTermCmdInPane({"cd /Users/AsinW/svnBox/jiyingshou/web/mobile4", "npm run dev"})
iTermSetRows(3)
iTermActiveSession(0)
delay 1
moveBounds("iTerm", -1187, 136, -1, 935)
複製代碼
說明:
工具是死的,人是活的。
有不少你們耳熟能詳的工具或方法,只是聽過,沒有用過,由於你沒有想象到本身使用的場景,等到頓悟的那一天,會發現,哦,能夠這樣那樣的用。
以此記錄,供你們參考或收藏或路過。
原文來自阿星的博客:wanyaxing.com/blog/201808…