前阵子说过,发现了 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…