總結一下工作中常用到的一些可提升效率的一些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