AppleScript 基础(一)基本语法和数据类型

AppleScript 基础

一、基本语法

  1. 在 AppleScript 中所有值都需要使用双引号引起来。
  2. 变量命名字母或下划线开头。官方建议使用驼峰的方式。
  3. 语句结束使用换行
    例如:定义变量variable值为Hello Word
set variable to "Hello Word"

二、常用数据类型

  1. number 数字
  2. string 字符串
  3. boolean 布尔
  4. list 列表类型(\color{red}{注意}:list 中的内容成为元素 item)�
  5. record 记录(\color{red}{注意}:record 中的内容成为属性property)

幸运的是 AppleScript 变量不用强制定义类型

三、字符串介绍和常用操作

1. 定义一个字符串 "set ... to ..."
    set myName to "elephant"
2. 字符串连接符 "&"
    set myName to "elephant"
    set age to 20
    -- get类似 php、python 的 echo、print
    get "name: " & myName & "; age: " & age
    >>> "name: elephant; age: 20"
3.查看字符串长度 "to the length/count of"
    set myName to "elephant"
    -- 这里 length 替换成 count 可以可以的
    set nameLength to the length of myName
    >>> 8
4.将字符串按照每个字母切割成 list "to every character of"
    set myName to "elephant"
    set myList to every character of myName
    get myList
    >>> {"e", "l", "e", "p", "h", "a", "n", "t"}

当然这种切割基本上没啥用,我们希望的是使用特定的符号切割字符串。继续向下看

5.按照特定的字符切割字符串 "AppleScript's text item delimiters"
    set myName to "Very fat elephant"
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to " "
    set myList to every text item of myName
    set AppleScript's text item delimiters to oldDelimiters
    get myList
    >>> {"Very", "fat", "elephant"}
    
    -- 如果只是按照空格切分字符串可以使用下面的方法
    set myList to words of myName
    get myList
    >>> {"Very", "fat", "elephant"}
    
    -- 还可以限制数量哦
    set myList to words 1 thru 2 of myName
    get myList
    >>> {"Very", "fat"}

说明:
第3行:设置要切分的特殊字符
第2,5行:因为系统默认的AppleScript's text item delimiters这个东西是{""}所以在切割完字符串后需要在修改回来
第4行:"to every text item of"每个文本元素;而"to every character of"每个字符

6. 获取指定范围的字符串 "text from word ... to word ... of ..."
set shortList to text from word 1 to word 2 of "We're all in this together"
>>> "We're all"
6. 字符串比较运算符
begins with (or, starts with) 以……开头
ends with 以……结尾
comes before / comes after 在……之前
is in 在……之中
contains 包含
does not contain 不包含
does not comes before 不在....之前
does not start with 不以……开头
does not contain 不以……结尾
is not in 不在……之内

用法示例:

    set myName to "Very fat elephant"
    set bool to myName is equal to "Very fat elephant"
    get bool
    >>> true
    
    set myName to "Very fat elephant"
    set bool to myName does not contains "Very"
    get bool
    >>> false
    
    set myName to "Very fat elephant"
    set bool to myName comes before "Very"
    get bool
    >>> false
    
    set myName to "Very fat elephant"
    set bool to "V" comes before myName
    get bool
    >>> true

四. 列表(list)介绍和常用操作

1.定义一个列表
 set myList to {"a", "b", "c"}
2. 替换list 中元素的内容 "set item ... of ... to ..."

\color{red}{注意}:在 AppleScript中 list 是从1开始计数的

    set myList to {"a", "b"}
    set item 2 of myList to "B"
    get myList 
    >>> {"a", "B"}
3.获取 list 中最后一个元素或第一个 "set ... to first/last item of ..."
    set myList to {"a", "b", "c", "d", "e", "f"}
    set value to last item of myList
    >>> "f"
4.获取指定值 "set ... to item ... of ..."
    set myList to {"a", "b", "c", "d", "e", "f"}
    set value to item -2 of myList
    >>> "e"
5.获取指定范围的数据

获取从 N到M 所有的值

    set myList to {"a", "b", "c", "d", "e", "f"}
    set shortlist to items 2 through 5 of myList
    get shortlist
    >>> {"b", "c", "d", "e"}

获取范围数据时,根据数据类型筛选

    set myList to {1, "a", 3, "b", 4, 5}
    set shortlist to integers 1 thru 3 of myList
    get shortlist
    >>> {1, 3, 4}
6.排序 "set ... to reverse of ..."
    set myList to {"a", "b", "c", "d", "e", "f"}
    set reverseList to reverse of myList
    get reverseList
7.获取 list 中的数量,和字符串一样 "set ... to the length/count of..."
    set myList to {"a", "b", "c", "d", "e", "f"}
    set listCount to the length of myList
    >>> 6
8.随机获取 list 中一个数据 "set ... to some item of..."
    set myList to {"a", "b", "c", "d", "e", "f"}
    set a to some item of myList
    get a
9.给 list 末尾添加一个元素 "set the end of ... to ..."
     set myList to {"a", "b", "c", "d", "e", "f"}
     set the end of myList to "zzz"
     get myList
     >>> {"a", "b", "c", "d", "e", "f", "zzz"}
10.将 list 组成字符串
    set myList to {"a", "b", "c", "d", "e", "f", "ads"}
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "-"
    set myList to myList as string
    set AppleScript's text item delimiters to oldDelimiters
    get myList
    >>> "a-b-c-d-e-f-ads"
11.比较运算符和字符串类似

五、记录(record)

1. 创建一个记录
    set myRecord to {name: "elephant", age: 20}
2. 获取记录的值 "set ... to ... of ..."
    set myRecord to {name: "elephant", age: 20}
    set value to name of myRecord
    get value
    >>> "elephant"
3. 修改记录的值
    set myRecord to {name: "elephant", age: 20}
    set age of myRecord to 30
    get myRecord
    >>> {name:"elephant", age:30}
4. 获取数量 "set ... to the length/count of ..."
    set myRecord to {name:"elephant", age:20}
    set recordCount to the length of myRecord
    get recordCount
    >>> recordCount

六、类型转换 "... as ..."

set a to "15" as number
get a
>>> 15

set b to 123 as string
get b
>>> "123"

set c to "asdf" as list
get c
>>> {"asdf"}

set d to 0 as boolean
get d
>>> false
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355