如需转载请注明出处:让你的 MAC自动化起来(自动启动项目和iTerm 相关 AppleScript语句)
简介
如果你是一名程序员每天早上到公司启动开发项目要有多少步骤操作?
以我自己为例。我每天到公司要在 iTerm 中启动5个项目操作如下:
- 启动 mysql
mysql.server start
- 进入 python 工作环境
workon SPC
- 进入到项目根目录
cd /Users/machao/Desktop/Projects
- 进入到开发分支
git checkout develop
- 获取最新的代码
git pull origin develop
- 如果有数据库变化更新数据库
python manage.py migrate
- 启动项目
python manage.py runserver 8088
以上是我启动了一个项目,在不算 mysql 启动命令,我还要在启动4个项目重复上面2~7的步骤,一共要在操作24次,其中还不算给 iTerm 切分窗口的操作。以上这一坨操作大概要花掉5分钟左右。
为了让你节约这五分钟,而且可以在你同事面前显呗一下吼吼。
OK先看效果:
看到了吧。AppleScript 可以把5分钟的操作简化为5秒中。
下面进入正题。
基本语句
首先我先将本章会用到的 iTerm 的 AppleScript 语句写出来。如果不懂 AppleScript 语法和基本流程语句请参考《AppleScript 基础一》和《AppleScript 基础二》
iTerm的 AppleScript 基本语句
下面也是一个执行顺序
语句 | 说明 |
---|---|
create window with default profile | 创建一个新的窗口 |
set win to current window | 获取到当前的窗口 |
create tab with default profile | 创建一个新的 tab 页 |
current tab of current window | 获取当前窗口下的 tab |
set columns to sessions | 获取当前tab下所有分栏(返回一个 list) |
tell second session | 告诉第几个分栏做什么( second 随便替换) |
split horizontally with default profile | 在当前tab中水平切分一个分栏 |
split vertically with default profile | 在当前tab中垂直切分一个分栏 |
set name to "name" | 设置 iTerm窗口名 |
write text "ls" | 在 iTerm中写入命令 |
关于 窗口-tab-分栏
,看下图:下图就是一个窗口,窗口里面两个 tab,当前 tab 中有3个分栏;
iTerm AppleScript 语句用法说明
1. 创建一个新的窗口 create window with default profile
tell application "iTerm"
create window with default profile
end tell
2. 在窗口中创建 tab create tab with default profile
创建 tab的前提是你要告诉 iTerm 在那个窗口创建所,以需要 tell current window
tell application "iTerm"
tell current window
create tab with default profile
end tell
end tell
3. 获取当前 tab 中分栏数量set sessionCount to the count of sessions
tell application "iTerm"
tell current tab of current window
set sessionCount to the count of sessions
get sessionCount
end tell
end tell
>>> 3
4. 在当前 tab 中水平切分一个分栏 split horizontally with default profile
创建分栏你要告诉 iTerm 是在哪个窗口的哪个 tab 中的哪个分栏创建新的分栏
tell application "iTerm"
-- 当前窗口的当前 tab
tell current tab of current window
-- 第一个分栏中
tell first session
-- 水平切分一个新的分栏
split horizontally with default profile
end tell
end tell
end tell
5. 垂直分类和水平分栏创建过程相同 split vertically with default profile
tell application "iTerm"
tell current tab of current window
tell first session
split vertically with default profile
end tell
end tell
end tell
6. 在 iTerm 中输入命令 write text ...
同样也是需要告诉 iTerm 命令是在哪个窗口的哪个 tab 中的哪个分栏执行
tell application "iTerm"
tell current tab of current window
tell first session
write text "ls"
end tell
end tell
end tell
7. 设置 iTerm 分栏名称 set name to ...
在 Build 3.3.0beta1 版本突然不能用了,不知道为什么。
tell application "iTerm"
tell current tab of current window
tell first session
set name to "AA"
end tell
end tell
end tell
以上是在我的自动化程序中最常用的命令;
iTerm 官方 AppleScript 语句点击这里查看
下面是我启动项目脚本的源码
on selectItem()
-- 定义按钮
set itemButtons to {"Gateway", "Raven", "Cancel"}
set temp to display dialog "Select the item to launch:" buttons itemButtons
set itemName to button returned of temp
if itemName is "Gateway" then
runGateway()
else if itemName is "Raven" then
runRaven()
else
end if
end selectItem
-- public
on closeTab(windowSession, maxTabs)
get windowSession
tell application "iTerm"
tell windowSession
set tabCount to the count of sessions
if tabCount ≥ maxTabs then
set itermTabs to sessions
set a to 1
-- 循环tab中分栏字典
repeat tabCount - 1 times
-- 获取字典里面单个分栏元素
set value to item a of itermTabs
-- 索引+1
set a to a + 1
-- 告诉tab中单个分栏关闭
tell value
close
end tell
end repeat
end if
end tell
end tell
end closeTab
-- 检测 iterm 是否启动
on checkiTermActive()
tell application "iTerm"
-- 激活 iterm
tell application "iTerm" to activate
try
-- 检测 iterm 是否有 window 没有报错
tell current tab of current window
set tabCount to the count of sessions
end tell
on error the errorMessage number the errorNumber
-- 创建一个 window
create window with default profile
end try
end tell
return true
end checkiTermActive
-- end public
-- Gateway
on initGatewayItem()
if checkiTermActive() then
tell application "iTerm"
-- 获取 iterm window 中第一个 Tab
set tabRecord to tab of current window
set firstTab to item 1 of tabRecord
-- 关闭多余的 分栏(session)
closeTab(firstTab, 2) of me
tell firstTab
set tabCount to the count of sessions
-- 检测tab中分栏数量是否为1
if tabCount = 1 then
tell first session
-- 在tab中创建新的分栏
-- vertically垂直切分
-- horizontally 水平切分
split vertically with default profile
split horizontally with default profile
runItem() of me
end tell
end if
end tell
end tell
end if
end initGatewayItem
on runItem()
tell application "iTerm"
set tabRecord to tab of current window
set firstTab to item 1 of tabRecord
tell firstTab
set tabCount to the count of sessions
if tabCount = 3 then
-- 告诉第一个分栏
tell first session
-- 设置分栏名
set name to "Nexus(staging)"
-- 在分栏中执行下面的命令
write text "mysql.server start"
write text "workon SC"
write text "Nexus"
write text "gck staging"
write text "gpullo staging"
write text "pym migrate"
write text "runnexus"
end tell
tell second session
set name to "Arbiter(develop)"
write text "Arbiter"
write text "workon SC"
write text "gck develop"
write text "gpullo develop"
write text "pym migrate"
write text "runarbiter"
end tell
tell third session
set name to "Gateway(newui)"
write text "Gateway"
write text "workon SC"
write text "gck newui"
write text "gpullo newui"
write text "pym migrate"
write text "rungateway"
end tell
else
initGatewayItem() of me
end if
end tell
end tell
end runItem
on runGateway()
initGatewayItem()
end runGateway
-- end Gateway
-- Raven
on initRaveniTerm()
if checkiTermActive() then
tell application "iTerm"
tell application "iTerm" to activate
set tabRecord to tab of current window
set tabCount to the count of tabRecord
if tabCount is 1 then
tell current window
create tab with default profile
end tell
end if
set tabRecord to tab of current window
set secondTab to item 2 of tabRecord
closeTab(secondTab, 2) of me
end tell
end if
end initRaveniTerm
on runRaven()
initRaveniTerm()
tell application "iTerm"
-- 获取当前窗口 tab 列表和数量
set tabRecord to tab of current window
set tabCount to the count of tabRecord
if tabCount ≥ 2 then
-- 获取窗口中第二个 tab
set secondTab to item 2 of tabRecord
-- 告诉第二个 tab
tell secondTab
-- 告诉第二个tab 的第一个分栏
tell first session
split vertically with default profile
set name to "Raven"
write text "workon SC-py3"
write text "Raven"
write text "gck develop"
write text "gpullo develop"
write text "runraven"
end tell
tell second session
set name to "Raven UI"
write text "workon SC-py3"
write text "Raven"
write text "gck develop"
write text "gpullo develop"
write text "cd ui"
write text "npm start"
end tell
end tell
end if
end tell
end runRaven
-- end Raven
selectItem()
里面有很多我启动项目的命令缩写;mac在~/.bash_profile
中记录,继续向下看
alias gadd='git add'
alias gb='git branch'
alias gc='git commit'
alias gs='git status'
alias gpusho='git push origin'
alias gpu='git push upstream'
alias gpull='git pull'
alias gpullo='git pull origin'
alias gf='git fetch'
alias gfo='git fetch origin'
alias gck='git checkout'
alias gr='git rm'
alias gm='git mv'
alias gcp='git cherry-pick'
alias gdiff='git diff'
alias ll='ls -al'
alias runnexus='cd /Users/machao/Desktop/Projects/Nexus/ && python manage.py runserver 127.0.0.1:8080'
alias runngnexus='cd /Users/machao/Desktop/Projects/NgNexus/ && python manage.py runserver 127.0.0.1:8080'
alias rungateway='cd /Users/machao/Desktop/Projects/Gateway/ && python manage.py runserver 127.0.0.1:9000'
alias runarbiter='cd /Users/machao/Desktop/Projects/Arbiter/ && python manage.py runserver 127.0.0.1:8000'
alias runcommander='cd /Users/machao/Desktop/Projects/Commander/ && python manage.py runserver 127.0.0.1:8090'
alias runraven='cd /Users/machao/Desktop/Projects/Raven/ && python manage.py runserver 127.0.0.1:8090'
模拟 MAC 快捷键操作
关于按键的编码点击这里查看
- 单键点击
key code
模拟按键必须在System Events
这里面进行
tell application "System Events"
key code 49
end tell
- 多个键点击
keystroke ".." using {... ,...}
--粘贴操作
tell application "System Events"
keystroke "v" using {command down}
end tell
:这里的 v 是小写
如果把 v变成大写 V 系统会认为你要操作的是 command + shift + v
- 有些可以直接输入名字即可
tell application "System Events"
keystroke return
keystroke space
keystroke tab
end tell