总结一下工作中常用到的一些可提升效率的一些AppleScript的操作
iTerm2
新建窗口并执行shell指令
tell application "iTerm"
set myterm to create window with default profile
tell myterm
activate current session
launch session "Default Session"
tell the current session
write text "echo hello"
write text "clear;"
write text "date && cal" without newline
end tell
write (sessions of current tab) text linefeed
end tell
end tell
新建tab, 并执行shell命令
tell application "iTerm"
tell current window
create tab with default profile
tell current session
write text "echo 'hello world~ ' "
end tell
end tell
end tell
配合自动操作选中Podfile
文件增加快速操作菜单,创建新窗口并执行pod install
#新建窗口,选中Podfile 右键快速操作找到pod install这个菜单执行即可
PodfilePath="${1%/*}"
osascript <<EOF
tell application "iTerm"
set myterm to create window with default profile
tell myterm
activate current session
launch session "Default Session"
tell the current session
write text "cd ${PodfilePath}"
write text "clear;"
write text "pod install" without newline
end tell
write (sessions of current tab) text linefeed
end tell
end tell
EOF
# 或者新建tab
PodfilePath="${1%/*}"
osascript <<EOF
tell application "iTerm"
tell current window
create tab with default profile
tell current session
write text "cd ${PodfilePath}"
write text "clear;"
write text "pod install" without newline
end tell
end tell
end tell
EOF
Safari
使用Safari打开指定URL
function openWebLink(){
osascript <<EOF
tell application "Safari"
activate
tell window 1
set current tab to (make new tab with properties {URL:"${1}"})
end tell
end tell
EOF
}
Safari当前标签页执行一段js
//CSDN或360Doc解锁文本
tell application "Safari" to do JavaScript "javascript:window.oncontextmenu=document.oncontextmenu=document.oncopy=null; [...document.querySelectorAll('body')].forEach(dom => dom.outerHTML = dom.outerHTML); [...document.querySelectorAll('body, body *')].forEach(dom => {['onselect', 'onselectstart', 'onselectend', 'ondragstart', 'ondragend', 'oncontextmenu', 'oncopy'].forEach(ev => dom.removeAttribute(ev)); dom.style['user-select']='auto'; dom.style['-webkit-touch-callout']='auto'; dom.style['-webkit-user-select']='auto'; dom.style['-ms-user-select']='auto';dom.style['-khtml-user-select']='auto'; dom.style['-moz-user-select']='auto';});" in document 1
Xcode
Xcode打开/编译/运行项目的shell工作流
#! /bin/bash
WorkspaceName="xxx.xcworkspace" #workspace文件名
FilePath="/xxx/xxx" #项目根目录路径
#打开项目
function LoadProject() {
osascript <<SCRIPT
tell application "Xcode"
# 判断已经打开就关闭的代码 觉着用不上就注释了
# open "$FilePath"
# set workspaceDocument to workspace document "${WorkspaceName}"
#
# repeat 120 times
# if loaded of workspaceDocument is true then
# close workspaceDocument
# exit repeat
# end if
# end repeat
open "$FilePath"
set workspaceDocument_new to workspace document "${WorkspaceName}"
set loadTime_Begin to (current date)
repeat 1200 times
if loaded of workspaceDocument_new is true then
set loadTime_End to (current date)
exit repeat
end if
end repeat
end tell
SCRIPT
}
# 编译项目
function BuildProject() {
osascript <<SCRIPT
tell application "Xcode"
set actionResult to build workspace document 1
set buildTime_Begin to (current date)
repeat
if completed of actionResult is true then
set buildTime_End to (current date)
exit repeat
end if
end repeat
log "Build 开始时间" & (time string of (buildTime_Begin))
log "Build 开始结束" & (time string of (buildTime_End))
log "Build 总共耗时 :" & (buildTime_End - buildTime_Begin) & "秒"
end tell
SCRIPT
}
# 运行项目
function RunProject() {
osascript <<SCRIPT
tell application "Xcode"
set actionResult to run workspace document 1
set runTime_Begin to (current date)
repeat
if status of actionResult is running then
set runTime_End to (current date)
exit repeat
end if
end repeat
log "Run 开始时间" & (time string of (runTime_Begin))
log "Run 开始结束" & (time string of (runTime_End))
log "Run 总共耗时 :" & (runTime_End - runTime_Begin) & "秒"
end tell
SCRIPT
}
LoadProject
BuildProject
RunProject