tell application "System Events" tell process "Terminal" (* 注意:由于环境的差异性,直接复制这段代码到你的脚本编辑器,可能得不到想要的效果 这里一定要输入你上面entire contents后,打印的"button 1"的完整结果 *) click button 1 of window "终端 — -zsh — 80×24" of application process "Terminal" of application "System Events" end tell end tell
#激活 tell application "Terminal" activate #打开键盘偏好设置 do script "open . '/System/Library/PreferencePanes/Keyboard.prefPane'" end tell
tell application "System Events" tell process "系统偏好设置" #点击修饰键 click button "修饰键…" of tab group 1 of window "键盘" -- of application process "System Preferences" of application "System Events" #更改为默认状态 click button "修饰键…" of tab group 1 of window "键盘" -- of application process "System Preferences" of application "System Events" click button "恢复成默认" of sheet 1 of window "键盘" -- of application process "System Preferences" of application "System Events" click button "好" of sheet 1 of window "键盘" -- of application process "System Preferences" of ®application "System Events" end tell end tell
#符号'¬'(option+'L') 用来将语句延续到第二行 display dialog "This is just a test." buttons {"Great", "OK"} ¬ default button "OK" giving up after 3 --result:调出弹窗,默认键是OK,3秒后消失
变量和属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#变量赋值 set myName to "John" copy 22 to myAge
#局部变量 local windowCount local agentName,agentNumber,agentHireDate
#全局变量 global gAgentCount global gStatementDate,gNextAgentNumber
#属性 property defaultClientName : "Mary Smith"
#字符串中引用变量 set aPerson to "GCS" display dialog "Hello " & aPerson & "!"
类型转换
1
set myText to 2 as text
运算符
1
3 * 7 - "1" --result 20
List(数组)
1 2 3 4 5 6
#初始化数组 set myList to {1, "what", 3} --result: {1, "what", 3} set beginning of myList to 0 --首位设置为0 set end of myList to "four" --末位设置为"four" set item 2 of myList to 4 --第二位设置为4 myList --result: {0, 4, "what", 3, "four"}
Record(键值对)
1 2 3 4
#存值 set myFullName to {firstName:"John", lastName:"Chapman"} #取值 set myLastName to lastName of myFullName --result "Chapman"
2.控制语句
considering/ignoring
1 2 3 4 5 6 7 8 9
"Hello Bob" = "HelloBob" --result: false ignoring white space --忽略空格 "Hello Bob" = "HelloBob" --result: true end ignoring
"BOB" = "bob" --result: true considering case --考虑大小写 "BOB" = "bob" --result: false end considering
try-error
1 2 3 4 5
try word 5 of "one two three" on error error "There are not enough words." end try
if
1 2 3 4 5 6 7 8 9
set currentTemp to 10 if currentTemp < 60 then set response to "It's a little chilly today." else if currentTemp > 80 then set response to "It's getting hotter today." else set response to "It's a nice day today." end if display dialog response
repeat-exit
1 2 3 4 5 6 7 8 9 10
set num to 0 repeat -- perform operations if num < 5 then set num to num + 1 else display dialog num exit repeat end if end repeat
repeat (number) times
1 2 3 4 5 6 7 8
set x to 3 set power to 3
set returnVal to x repeat power - 1 times set returnVal to returnVal * x end repeat return returnVal
repeat while(当型循环)
1 2 3 4 5 6 7 8 9
set userNotDone to true repeat while userNotDone set userNotDone to enterDataRecord() end repeat
on enterDataRecord() delay 3 false end enterDataRecord
数值循环
1 2 3 4 5 6
set n to 3 set returnVal to 0 repeat with i from 0 to n set returnVal to returnVal + I end repeat return returnVal
(数组遍历)
1 2 3 4 5 6 7 8 9 10 11
set peopleList to {"Chris", "David", "Sal", "Ben"}
#方法一 repeat with aPerson in peopleList display dialog "Hello " & aPerson & "!" end repeat
#方法二 repeat with i from 1 to (number of items in peopleList) display dialog "Hello " & item i of peopleList & "!" end repeat
String to List
1 2 3 4 5 6 7
set wordList to words in "Where is the hammer?" repeat with currentWord in wordList log currentWord if (contents of currentWord) is equal to "hammer" then display dialog "I found the hammer!" end if end repeat
record遍历
1 2 3 4 5 6 7 8 9 10 11 12
#在文档中没有找到遍历record的方法,不知道是不是不小心遗漏了 #但是在Stack Overflow中看有如下方法,引入了OC中的Foundation库,实现了record的遍历 use framework "Foundation" set testRecord to {a:"aaa", b:"bbb", c:"ccc"}
set objCDictionary to current application's NSDictionary's dictionaryWithDictionary:testRecord set allKeys to objCDictionary's allKeys()
repeat with theKey in allKeys log theKey as text log (objCDictionary's valueForKey:theKey) as text end repeat
3.函数构造
1 2 3 4 5
on greetClient(nameOfClient) display dialog ("Hello " & nameOfClient & "!") end greetClient
greetClient("GCS_DEVELOPER")
4.脚本对象
定义/执行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13
script John property HowManyTimes : 0 to sayHello to someone set HowManyTimes to HowManyTimes + 1 return "Hello " & someone end sayHello end script
tell John to sayHello to "Herb" #John's sayHello to "Jake" --result: "Hello Jake" #sayHello of John to "Jake" --result: "Hello Jake"
初始化脚本
1 2 3 4 5 6 7 8 9 10 11
on makePoint(x, y) script thePoint property xCoordinate:x property yCoordinate:y end script return thePoint end makePoint set myPoint to makePoint(10,20) get xCoordinate of myPoint --result: 10 get yCoordinate of myPoint --result: 20
script Alex on sayHello() return "Hello, " & getName() end sayHello on getName() return "Alex" end getName end script script AlexJunior property parent : Alex on getName() return "Alex Jr" end getName end script -- Sample calls to handlers in the script objects: tell Alex to sayHello() --result: "Hello, Alex" tell AlexJunior to sayHello() --result: "Hello, Alex Jr." tell Alex to getName() --result: "Alex" tell AlexJunior to getName() --result: "Alex Jr"