Mac系统脚本语言 AppleScript 的使用
AppleScript 是开发 Mac 系统脚本文件使用的语言。
编写好的代码有两种执行方式,一种保存为脚本,去执行这个脚本,另一种是直接执行脚本的命令。
编写完成后的文件保存为 .scpt 格式,然后可以在终端使用 osascript 执行。
或者也可以直接使用终端执行脚本的命令。
保存为脚本再执行:
比如下面这段代码,功能是调用系统弹框,展示 1234 几个数子:
display dialog 1234
可以放到 ~/Desktop/dialog.scpt 文件中,然后终端执行:
osascript ~/Desktop/dialog.scpt
直接使用终端执行指令:
另一种是直接在终端中执行脚本命令,执行结果一样:
osascript -e "display dialog 1234"
建议还是保存为脚本文件后再执行。
因为脚本文件可以编写复杂的逻辑,便于开发和管理。
新建文件 openFinder.scpt,保存到桌面:
-- 打开新的访达窗口,但只是在后台打开了
tell application "Finder" to make new Finder window
-- 把访达软件设为当前前台运行的项目,也就是激活窗口到第一层
tell application "Finder" to activate
打开终端,输入以下并回车:
osascript ~/Desktop/openFinder.scpt
第一次执行时,系统可能会弹出提示,同意即可。
然后就可以看到一个新的访达窗口出现了。
可以在执行脚本文件时传入参数,比如有以下文件 readArg.scpt。
on run arg
set firstArg to item 1 of arg
log firstArg
return item 2 of arg
end run
执行:
osascript ./readArg.scpt "第一个参数" "第二个参数"
则脚本文件执行后,会打印第一个参数,返回第二个参数
这部分展示单独运行的一些指令,只实现简单功能,可以用这些功能组合成复杂功能。
以下示例,都需要把代码保存成 .scpt 格式的文件,再用 osascript 执行。
log 1
log "2"
下面是打印了一下数据类型
log Class of "abc"
log Class of 1
注意:只有当 Sublime Text 是完全关闭的状态,使用这个脚本才会启动软件并自动打开一个窗口。
如果这个软件本身是启动状态,只是关闭了所有窗口,那么这个脚本执行后,只是把这个程序放在前台,但不会自动打开新窗口。
tell application "Sublime Text" to activate
这个指令不好用,需要软件已启动,只是没有在最前台,使用指令后,软件界面会跑到最前面来。
但如果软件未启动,直接会报错。
同理,下面指令中的 true 改为 false,可以隐藏软件界面。
tell application "System Events"
set visible of application process "Sublime Text" to true
end tell
用这个指令启动、把软件设为最前台最好使了,不过这个指令其实压根不需要使用苹果脚本了,直接在终端执行指令就OK了。
执行以下脚本,它将使用终端执行 open "/Applications/Sublime Text.app"
这条指令:
do Shell Script "open \"/Applications/Sublime Text.app\""
调用系统弹框展示文本
display dialog "text"
display dialog 1234
display dialog true
声明方法使用 on ... end,下面是一个加法的示例
on add(numA, numb)
return numA + numB
end add
display dialog add(1, 2)
使用 set [变量名] to [字面值或一个方法的返回值],复制时可以使用 as 修改变量类型。
下面的 add(1, 2)
就是一个方法。
set a to "text"
set c to 3 as string
set d to as add(1, 2) as string
下面是 return 值,结果会大致到控制台。
set a to 10
set b to 20
if a > b then
return "a > b"
else if a < b then
return "a < b"
else
return "a == b"
end if
以下一行,会让程序等待 3 秒再继续:
delay 3
判断一个字符串,是否包含另一个字符串。
set langText to "hello world ha"
set shortText to "world"
set isContains to langText contains shortText
-- 会弹出 true,因为 langText 包含 shortText
display dialog isContains
这部分是能完成一些相对复杂的需求的脚本。
以下示例,都需要把代码保存成 .scpt 格式的文件,再用 osascript 执行。
-- 通过进程名称获取软件名称
on GetApplicationCorrespondingToProcess(process_name)
tell application "System Events"
set process_bid to get the bundle identifier of process process_name
set application_name to file of (application processes where bundle identifier is process_bid)
end tell
return application_name
end GetApplicationCorrespondingToProcess
-- 通过软件名称获取进程名称
on GetProcessCorrespondingToApplication(application_name)
tell application "System Events"
set application_id to (get the id of application application_name as string)
set process_name to name of (application processes where bundle identifier is application_id)
end tell
return process_name
end GetProcessCorrespondingToApplication
-- 获取当前前台运行的项目的进程名称
on GetfrontProcess()
tell application "System Events"
set theName to name of the first process whose frontmost is true
end tell
return theName
end GetfrontProcess
-- 开始执行
set processName to GetfrontProcess()
set AppName to GetApplicationCorrespondingToProcess(processName)
return AppName