首页 > AppleScript 快速基础学习

aaaforapple歌词,AppleScript 快速基础学习

互联网 2021-01-20 23:57:41
在线算命,八字测算命理 AppleScript的必须掌握的命令:tell someone do something

AppleScript的语法十分接近自然语言,想要操作一个对象执行某些操作,只需要使用"tell命令 +对象类型 + 对象名",在之后执行end tell,结束当前的回话

激活终端

tell application "Terminal"activate -- 告诉 终端,执行激活命令end tell

关闭终端

tell application "Terminal"quit -- 告诉 终端,执行退出命令end tell

System Events是系统应用,我们有时寄期望于在系统应用中找到某个正在执行的进程(Process)

tell application "System Events"tell process "Terminal"end tellend tell

在执行这段代码后,你会发现,其实什么都没有执行,不用慌,这是因为你并没有告诉"终端"要做些什么,那么我们通过entire contents命令获取Terminal内的所有UI元素

#执行下面代码前,先激活"终端"tell application "System Events"tell process "Terminal"entire contents --获取区域内所有的UI元素end tellend tell

执行后,会得到"终端"内的所有UI元素的完整描述

image.png

选择日志中的一条进行分析

button 1 of window "终端 — -zsh — 80×24" of application process "Terminal" of application "System Events"

其实这其中的从属关系已经十分明确了,翻译过来就是

应用"System Events"的应用进程"Terminal"的"窗口"的"按钮1"

模拟点击

button 1的描述,我们已经很清楚了,那button 1到底对应"终端"中的哪个按钮呢?其实单从名字上看,很难看出button 1对应的是哪个按钮,那么我们用一个很简单的方法,确定它的位置,让button 1执行点击操作,通过它的点击效果反推它的位置!

#执行下面代码前,先激活"终端"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 tellend tell

执行上述操作后,发现"终端"被关闭了,原来button 1对应的就是"终端"左上角的关闭按钮

请注意:并不是所有的button 1对应的都是应用的关闭按钮,还要具体情况具体分析

模拟输入

我们打开了终端,想看当前目录下存有哪些文件,该如何操作呢?想一下这个问题,我们都知道在终端中查看目录下文件的命令是ls,那么就可以将这个问题拆分为键入'ls'和点击回车两个步骤在AppleScript中,输入字符串的命令是keystroke,你也可以用key code实现单键点击我们完善一下代码:

tell application "Terminal"activateend telltell application "System Events"tell process "Terminal"keystroke "ls"delay 1 -- 延时一秒后执行key code 36 -- 回车的键位码为36end tellend tell

完整的键位码传送门☞键位码

我们有时需要通过组合按键的方式执行某一操作,通过在键入命令后添加using {按键A down, 按键B down}的方式,如:

#强制关闭应用快捷键key code 53 using {command down, option down} -- Esc的键位码为53执行shell命令

在终端中通过执行shell命令的方式可以达到和模拟键入的同样的效果

tell application "Terminal"activatedo script "ls" end tell

当然也可打开某路径下的文件,这里我们尝试打开"系统偏好设置"中的"键盘偏好设置"

tell application "Terminal"activate#打开键盘偏好设置do script "open .'/System/Library/PreferencePanes/Keyboard.prefPane'"end tell

我们简单回顾一下,现在我们已经了解了:

如何激活应用,并在当前的系统进程中找到它

如何获取某一区域内的全部UI控件,并尝试点击其中的某一个单独控件

如何实现模拟字符串输入、单键输入和组合输入

如何关闭应用

此外,我们再多了解两个帮助我们编写代码的两个小知识点:

1.注释

AppleScript中的注释分为单行注释和多行注释单行注释:#、--多行注释 (* 中间的部分都是注释 *)(上文中三种注释均有用到)

2.输出日志

log命令,可以通过打印辅助信息,来帮助我们编写代码至此,你已经掌握了实现恢复系统默认修饰键的全部基础概念,小伙伴们完全可以自己尝试写下代码核心代码:

#激活tell application "Terminal"activate#打开键盘偏好设置do script "open .'/System/Library/PreferencePanes/Keyboard.prefPane'"end telltell 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 tellend tellAppleScript基础知识1.基本模块语法#符号'¬'(option+'L') 用来将语句延续到第二行display dialog "This is just a test." buttons {"Great", "OK"} ¬default button "OK" giving up after 3 --result:调出弹窗,默认键是OK,3秒后消失变量和属性#变量赋值set myName to "John"copy 22 to myAge#局部变量local windowCountlocal agentName,agentNumber,agentHireDate#全局变量global gAgentCountglobal gStatementDate,gNextAgentNumber#属性property defaultClientName : "Mary Smith"#字符串中引用变量set aPerson to "GCS"display dialog "Hello " & aPerson & "!"类型转换set myText to 2 as text运算符3 * 7 - "1" --result 20List(数组)#初始化数组set myList to {1, "what", 3} --result: {1, "what", 3}set beginning of myList to 0 --首位设置为0set end of myList to "four" --末位设置为"four"set item 2 of myList to 4 --第二位设置为4myList --result: {0, 4, "what", 3, "four"}Record(键值对)#存值set myFullName to {firstName:"John", lastName:"Chapman"}#取值set myLastName to lastName of myFullName --result "Chapman"2.控制语句considering/ignoring"Hello Bob" = "HelloBob" --result: falseignoring white space --忽略空格"Hello Bob" = "HelloBob" --result: trueend ignoring"BOB" = "bob" --result: trueconsidering case --考虑大小写"BOB" = "bob" --result: falseend consideringtry-errortryword 5 of "one two three"on errorerror "There are not enough words."end tryifset currentTemp to 10if currentTemp < 60 thenset response to "It's a little chilly today."else if currentTemp > 80 thenset response to "It's getting hotter today."elseset response to "It's a nice day today."end ifdisplay dialog responserepeat-exitset num to 0repeat-- perform operationsif num < 5 thenset num to num + 1elsedisplay dialog numexit repeatend ifend repeatrepeat (number) timesset x to 3set power to 3set returnVal to xrepeat power - 1 timesset returnVal to returnVal * xend repeatreturn returnValrepeat while(当型循环)set userNotDone to truerepeat while userNotDoneset userNotDone to enterDataRecord()end repeaton enterDataRecord()delay 3falseend enterDataRecordrepeat with loopVariable (数值循环)set n to 3set returnVal to 0repeat with i from 0 to nset returnVal to returnVal + Iend repeatreturn returnVal(数组遍历)set peopleList to {"Chris", "David", "Sal", "Ben"}#方法一repeat with aPerson in peopleListdisplay dialog "Hello " & aPerson & "!"end repeat#方法二repeat with i from 1 to (number of items in peopleList)display dialog "Hello " & item i of peopleList & "!"end repeatString to Listset wordList to words in "Where is the hammer?"repeat with currentWord in wordListlog currentWordif (contents of currentWord) is equal to "hammer" thendisplay dialog "I found the hammer!"end ifend repeatrecord遍历#在文档中没有找到遍历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:testRecordset allKeys to objCDictionary's allKeys()repeat with theKey in allKeyslog theKey as textlog (objCDictionary's valueForKey:theKey) as textend repeat3.函数构造on greetClient(nameOfClient)display dialog ("Hello " & nameOfClient & "!")end greetClientgreetClient("HelloWorld")4.脚本对象定义/执行脚本script Johnproperty HowManyTimes : 0to sayHello to someoneset HowManyTimes to HowManyTimes + 1return "Hello " & someoneend sayHelloend scripttell John to sayHello to "Herb"#John's sayHello to "Jake" --result: "Hello Jake"#sayHello of John to "Jake" --result: "Hello Jake"初始化脚本on makePoint(x, y)script thePointproperty xCoordinate:xproperty yCoordinate:yend scriptreturn thePointend makePoint set myPoint to makePoint(10,20)get xCoordinate of myPoint--result: 10get yCoordinate of myPoint--result: 20继承script Alexon sayHello()return "Hello, " & getName()end sayHelloon getName()return "Alex"end getNameend script script AlexJuniorproperty parent : Alexon getName()return "Alex Jr"end getNameend 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"写在结尾

本文记录的只是AppleScript中的一小部分内容,笔者在网上暂时还没有找到十分详细的中文文档和介绍,所以想要更加深入的了解和学习AppleScript还是查看官方文档!

一手资料☞AppleScript Language Guide

参考资料:手把手教你用 AppleScript 模拟鼠标键盘操作,实现 macOS 系统的自动化操作

AppleScript中的保留字

image.png
免责声明:非本网注明原创的信息,皆为程序自动获取自互联网,目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责;如此页面有侵犯到您的权益,请给站长发送邮件,并提供相关证明(版权证明、身份证正反面、侵权链接),站长将在收到邮件24小时内删除。