AppleScript脚本快速入门

/ 0评 / 0

最近按照bilibili的一个up主的教程,花了620买了一台小主机,捣腾起了黑苹果。

https://www.bilibili.com/video/BV1ZK411J7SC

但是使用起来有诸多不习惯,特别是快捷键方面十分别扭,便开始研究如何使用开机键帮助自己达成目的。

Mac os中就有一个applescript的东西,可以让我们使用很多mac os提供的诸多方法也是mac 上操作应用程序为数不多的途径之一。非常方便实现一些平常工作中重复工作的脚本化,提升工作效率,避免重复劳动。它可以帮助我们:

编写工具

脚本编辑器目前支持applescript和javascript 两种脚本语言

使用 command+r 可以快速运行

基本操作

简单运行示例

在编辑器中输入以下内容,按快捷键command+r 即可快速执行

set x to 25
set area to x * 10

查看运行结果

注意,脚本编辑器默认只显示最后一行结果。

当然,我们可以使用display alert做弹窗显示。

基本语法

数据类型

AppleScript有4种最基本的数据类型:number、string、list和record,分别对应编程概念中的数值、字符串、数组和字典。

它们都是使用set赋值,使用get取出。

number

number 有简单的运算符: +、-、*、/、^ 对应了 加、减、乘、除、乘方

set x to 2
get x
set y to 3
get y
set xy to x * y
set y3 to y ^ 3

string

set strX to "Hello "
set strY to "AppleScript"

字符串拼接

 set strXY to strX & strY & "str"

获取字符串长度

 set lengthOfStrXY to the length of strXY

分割成单个字符并组成一个新的列表

 set charList to every character of strXY

通过 AppleScript's text item delimiters 来指定分隔号,然后通过 every text item of 来实现分割

 set defaultDelimiters to AppleScript's text item delimiters
 set AppleScript's text item delimiters to " "
 set listAfterDelimiter to every text item of strXY
 set AppleScript's text item delimiters to defaultDelimiters

number 与 string 类型转换

 set numberToString to 100 as string
 set stringToNumber to "1234" as number
 set x to 100
 set y to x as string
 set x to y as numner

list 类型

set myList to { 100, 200.0, "300", -10 }

列表拼接(方法同字符串拼接)

set list1 to { 100, 200, 300}
set list2 to { 400, 500, 600}
set list3 to list1 & list2

获取和更改列表中的元素

获取元素,负数表示从列表尾端开始获取元素

set myList to { 100, 200, 300}
get item 2 of myList
get item -2 of myList

获取第一个元素

set myList to { 100, 200, 300}
get item 1 of myList
get first item of myList

获取最后一个元素

set myList to { 100, 200, 300}
get item -1 of myList
get last item of myList

替换列表中第三个为"3",倒数第2个同理:

set myList to { 100, 200, 300}
set item 3 of myList to "3" #{ 100, 200, "3"}
set item -2 of myList to "3" #{ 100, "3", "3"}

在列表中随机获取一个内容

set myList to { 100, 200, 300}
get some item of myList

倒转列表

set myList to { 100, 200, 300}
get reverse of myList

获取列表长度

set myList to { 100, 200, 300}
get the count of myList

将 string 转换为 list

set string1 to "Hello World"
set stringList to string1 as list
# {"Hello World"}

注释

-- 注释一行
#  注释一行
(*
注释多行
注释多行
*)

条件语句

我们把“if…then”指令叫做“条件语句”(conditional statement)。条件语句 还需要一个 end if 收尾。

set myHeight to 180
set standardHeight to 173
if myHeight > standardHeight then
    display alert "身高符合标准"
else
    display alert "身高不符合标准"
end if

循环语句

循环一百次

set sum to 0
repeat 100 times
    set i to i + 1
end repeat
get sum # 100

0到10 每次跳加2 counter作为参数

repeat with counter from 0 to 10 by 2
    display alert counter
end repeat

循环列表

set aList to { 1, 2, 8 }
repeat with anItem in aList
    display alert anItem as string
end repeat

函数

定义函数可以使我们反复调用同一处代码,大大减少代码量

# 获取列表所有数字总乘的函数
on getCount(numList)
	set oldNum to 1
	repeat with num in numList
		set oldNum to num * oldNum
	end repeat
	return oldNum
end getCount
# 调用函数
getCount({8, 9, 10})

错误处理

在程序当中,经常会遇到一些错误,这些错误有可能是程序错误导致的也有可能来自于用户的错误输入等等这些都会导致程序意外的终止

意外的终止必然是我们所不希望的 比如,你的脚本需要打开一个文件夹处理其中的文件, 但是这个文件夹已经被删除了,你会希望脚本允许用户选择其它合适的文件夹,而不是意外退出

你可以把这些可能引起运行错误的语句放入 try...end try 模块中

try
  set x to 1 / 0
on error the error_message number the error_number
  display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"} 
end try

弹出窗

通知框

display alert "只有一个'好'的选项"

确认框

有两个选项,取消和好,选择取消时会抛出异常,如果没有捕获异常,默认将终止程序

display dialog "是否同意用户协议?"

捕获异常

try
	display dialog "是否同意用户协议?"
	display alert "让我们继续愉快的玩耍"
on error
	display alert "呜呜呜,你不要我了"
end try

输入框

可以设置提示内容和输入框中的默认值

set returnedString to display dialog "Input a number here" default answer "10"
get text returned of returnedString

多选框

choose from list {"选项1", "选项2", "选项3"} with title "选择框" with prompt "请选择选项"

目录地址

如桌面的地址格式:

硬盘名称:Users:用户名称:Desktop

使用choose folder可以获取用户选取的目录地址

choose folder with prompt "指定提示信息"

使用choose file可以获取用户选取的文件地址

-- 选取文件名称Choose File Name
choose file name with prompt "指定提示信息"

限制类型

choose file name with prompt "指定提示信息" of type {"txt"}

文件读取和写入

在桌面创建文件函数:

on createMyTxt(name)
  tell application "Finder"   #调起应用程序
    try
      make new file of desktop with properties {name:name} #创建文件命令
    end try
  end tell
end createMyTxt

在桌面创建文件夹函数:

on createMyTxt(name)
  tell application "Finder"   #调起应用程序
    try
      #创建文件夹命令
      make new folder at desktop with properties {name:name} 
    end try
  end tell
end createMyTxt

文件读取写入:

#创建文件
createMyTxt("aFile.txt")

#获取文件
set aFile to alias "SSD:Users:mokevip:Desktop:aFile.txt"
#写入前必须用 open for access 打开文件
set fp to open for access aFile with write permission
#写入
write "Hello AppleScript" to fp
#关闭
close access fp

#读取文件内容
set myFile to alias "SSD:Users:mokevip:Desktop:aFile.txt"
read myFile

on createMyTxt(name)
	tell application "Finder"
		try
			make new file of desktop with properties {name:name}
		end try
	end tell
end createMyTxt

读取用户选取的文件内容:

set filep to choose file
set file1 to filep
read file1

案例

让电脑说话

say "good bye"
beep 2
say "good bye" using "fred"  #指定其他声音

打开终端

tell application "Terminal"
  reopen
  activate
end tell

调用mac通知

display notification "message" with title "title" subtitle "subtitle"

清空废纸篓

tell application "Finder"
  empty the trash
  beep
  -- 打开启动磁盘
  open the startup disk
end tell

挂载快捷键

很多时候我们写了一个脚本,可能会需要比较快捷的调用他,比如我会有需要使用快捷键来打开终端程序,我们可以:

1.使用自动操作新建一个快速操作

2.收到:没有输入

3.在实用工具中找到运行AppleScript,将它拖到窗口中

在这里我们就可以写入Apple Script脚本啦

比如我的打开终端

4.储存

5.可以在偏好设置->键盘->快捷键->服务->通用中找到,并为其设置快捷键

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注