◎软件环境:
macOS Sierra 10.12.5
Alfred 3
iterm2
◎功能描述:
开机正常联网后使用alfred输入start回车,即可一键完成身份认证,登陆中控机,打开QQ、WeChat、自定义网页标签等,如需打开特定IDE或其它App,请根据个人工作环境修改,添加即可。
◎实现思路:
脚本 + Alfred workflow + App把工作流打通,实现想要的功能。
workflow使用shell调用osascript脚本的方式实现,身份认证使用密码加token脚本实现,登陆中控机中间有一层relay跳板使用了expect跳过去。执行结果,异常信息等没有捕捉,有需要可自行完善。
# One-click start office software by anzhihe 2019-02-20
# 使用密码+token登陆relay认证,请修改为自己的信息
/usr/local/Cellar/python/3.7.1/bin/python3 /Users/anzhihe/relay.py -u anzhihe -p password -s token
# 启动办公软件
exec osascript <<EOF
-- 使用iterm2登陆中控机
tell application "System Events"
-- some versions might identify as "iTerm2" instead of "iTerm"
set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
end tell
tell application "iTerm"
activate
set hasNoWindows to ((count of windows) is 0)
if isRunning and hasNoWindows then
create window with default profile
end if
select first window
tell the first window
if isRunning and hasNoWindows is false then
create tab with default profile
end if
-- 使用组合键让iTerm2最大化
tell application "System Events"
key code 36 using {command down}
end tell
tell current session to write text "/usr/local/bin/con 中控机名称或IP"
end tell
end tell
-- 启动QQ
tell application "QQ"
activate --激活应用
-- 引入系统事件 用户触发键盘
tell application "System Events"
tell application process "QQ"
activate
set frontmost to true
-- 在 qq 有保存 qq 号,密码的前提下,调用回车来触发 qq 登录界面的登录按钮
key code {36}
end tell
end tell
end tell
-- 启动WeChat
tell application "WeChat"
activate
tell application "System Events"
tell application process "WeChat"
activate
key code {36}
end tell
end tell
end tell
-- 启动文档编辑器
tell application "Atom"
activate
tell application "System Events"
tell application process "Atom"
activate
key code {36}
end tell
end tell
end tell
-- Chrome打开自定义页面标签
set urlTickets to "https://chegva.com/ticket-manager/op-tickets"
set urlAlarm to "https://chegva.com/alarm-center/alarm-event-cases"
tell application "Google Chrome"
-- 新建一个 chrome 窗口
set window1 to make new window
tell window1
-- 当前标签页加载 tickets 页面
set currTab to active tab of window1
set URL of currTab to urlTickets
-- 打开报警页面,多个页面直接依次定义添加即可
make new tab with properties {URL:urlAlarm}
-- 将焦点由最后一个打开的标签页还给首个标签页
set active tab index of window1 to 1
end tell
end tell
-- 需打开IDE,音乐等app依次添加即可。
-- 横幅显示通知消息
tell application "System Events"
activate
display notification "新的一天新的开始,加油!" with title "服务已启动完毕,工作愉快:)"
end tell
EOF◎效果展示:
启动workflow:
完成后横幅提示:
不到10秒钟,办公环境就已经准备好啦,点点鼠标,敲敲键盘愉快的工作吧!
◎下载地址:https://github.com/anzhihe/Efficient-office/tree/master/one-click-start-software
预告:Mac高效办公第二弹——PopClip + Alfred + Iterm2实现一键登陆服务器
参考:

