AppleScript实用命令

AppleScript实用命令

个人整理,命令都经过了验证有效

AppleScript保留字

AppleScript保留字,按字母顺序列出

1 2 3 4 5
about above after against and
apart from around as aside from at
back before beginning behind below
beneath beside between but by
considering contain contains contains continue
copy div does eighth else
end equal equals error every
exit false fifth first for
fourth from front get given
global if ignoring in instead of
into is it its last
local me middle mod my
ninth not of on onto
or out of over prop property
put ref reference repeat return
returning script second set seventh
since sixth some tell tenth
that the then third through
thru timeout times to transaction
true try until where while
whose with without

注释

单行注释
--这是注释

多行注释

(*
这是第一行注释
这是第二行注释
*)

声明
#!/usr/bin/osascript

弹窗

为了易于阅读,可用¬(opt+l)进行换行展示

display dialog "This is just a test." buttons {"Great", "OK"} ¬
default button "OK" giving up after 3   --默认选中OK按钮,3s后弹窗消失

列表

{1,7,"name",4.5}

数字

3.1415

字典

{name:"bob",age:"18"}

字符串

"A basic string"

变量赋值

两种方式

set myName to "John"    -- set objectVar to "string"
copy 33 to myAge    -- copy inVar to "outVar"

表达式

3 * 7 --result: 21

声明 Statements

Finder关闭当前窗口

tell application "Finder"
    set savedName to name of front window   --将当前窗口finder的名称赋值给savename
    close window savedName  --关闭上一步保存的窗口名
end tell 

获取Finder已经打开的窗口标题

get name of front window of application "Finder" --当Finder没有激活时会报错


函数的使用

property defaultClientName : "Mary Smith"   --property:属性
 
 --需要传值的函数
on greetClient(nameOfClient)
    display dialog ("Hello " & nameOfClient & "!")
end greetClient
 
 -- 不需要传值的函数
script testGreet
    greetClient(defaultClientName)
end script
 
run testGreet --result: "Hello Mary Smith!"
greetClient("Joe Jones") --result: "Hello Joe Jones!"

向列表添加元素的方法

set myList to {1, "what", 3} --result: {1, "what", 3}
set beginning of myList to 0
set end of myList to "four"
myList --result: {0, 1, "what", 3, "four"}

获取文本编辑器的内容

tell application "TextEdit"
    paragraph 1 of document 1   --获得文本编辑中的第一个文档的第一行(paragraph 1)
end tell

获取文本编辑器窗口名称

tell application "TextEdit"
    first window's name --获得文本编辑应用中最前面的窗口名称
end tell

转换类型

set myText to 2 as text --将数字型转换字符串类型

在Finder中激活文件夹中创建副本

duplicate last file of window 1 of application "Finder"
    --在Finder已激活的窗口的文件夹目录下的第一个文件创建副本

等价写法

tell last file of window 1 of application "Finder"
    duplicate
end tell

复制第1行文本到第4行前

tell front document of application "TextEdit"
    duplicate paragraph 1 to before paragraph 4
        --将第一行的文本复制到第4行前面
end tell

获取应用的名称

tell application "Finder" -- sets target
    me --result: «script» (still the top-level script object)
    it --result: application "Finder" (target of the tell statement)
end tell

Debug

display dialog "In factorial routine; x = "
--弹窗选中取消时,就会停止执行命令

变量和属性(Variables and Properties)

获取本地时间

set currentDate to current date
--date 2020年11月3日 星期二 20:32:04

获取文本编辑器的打开窗口的第一个字

如果是word时,会将文本拆成单个词组,如:你, 吗, 本书, 解压, 密码

--获得文本编辑器的第一个文字
tell application "TextEdit"
    set word1 to word 1 of front document --result: some word
end tell

定义全局变量(global)

set currentCount to 10
on increment()
    global currentCount
    set currentCount to currentCount + 2
end increment
 
increment() --result: 12
currentCount --result: 12

脚本对象(Script Objects)

定义脚本

script John
    property HowManyTimes:0

    to sayHello to someone
        set HowManyTimes to HowManyTimes + 1
        say "Hello " & someone
        return "Hello " & someone
    end sayHello

end script

tell John to sayHello to "Herb"

获取函数的属性值

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

处理程序(About Handlers)

定义函数on,类似python的def

on helloWorld()
    display dialog "Hello World"
end
 
helloWorld() -- Call the handler

比大小

on minimumValue(x, y)
    if x < y then
        return x
    else
        return y
    end if
end minimumValue
 
-- To call minimumValue:
minimumValue(5, 105) --result: 5

use 语句1

Safari 后台搜索网页

use application "Safari"
search the web for "AppleScript"

use 语句2

显示弹窗

use scripting additions
display dialog "hello world"

try 语句

代码内的语句程序不会报错,会继续执行下去

使用的基本功能是要求在脚本开始执行之前资源已经存在。如果不能满足要求,脚本将无法运行。USE语句还可以指定所需资源的最低版本,例如应用程序的最低兼容版本。

try
    say i --该语句有错误,但是程序会继续执行下去
end try

say "hello"

use 定义safari并获得当前窗口标题

use Safari : application "Safari"
get the name of Safari's front window

现时关闭文本编辑器的窗口

-- 关闭文本编辑器的窗口
tell application "TextEdit"
    with timeout of 20 seconds  --20s没有执行,则视为超时
        close document 1 saving ask
    end timeout
end tell

处理程序参考(HandlerReference)

continue

on beep numTimes
    set x to display dialog "Start beeping?" buttons {"Yes", "No"}
    if button returned of x is "Yes" then ¬
        continue beep numTimes -- Let AppleScript handle the beep.
        -- In this example, nothing to do after returning from the continue.
end beep
 
beep 3 --result: local beep handler invoked; shows dialog before beeping
tell my parent to beep 3 -- result: AppleScript beep command invoked


文件夹操作(Folder Actions Reference)

支持的文件夹操作:

  • Enable or disable Folder Actions.

  • View the folders that currently have associated scripts

  • View and edit the script associated with a folder.

  • Add folders to or remove folders from the list of folders.

  • Associate one or more scripts with a folder.

  • Enable or disable all scripts associated with a folder.

  • Enable or disable individual scripts associated with a folder.

  • Remove scripts associated with a folder.

eg:用预览打开文件

tell application "Preview"
    open POSIX file "/Users/luomingyang/Desktop/test/1.jpg" --只使用此命令,文件会打开,但是不会出行窗口
    activate    
end tell

预览桌面文件

set myFile to "/Users/luomingyang/Desktop/test.md"
do shell script "qlmanage -p " & quoted form of myFile & ">& /dev/null"

将预览的两个窗口平铺

set bnds to ""
tell application "Finder" to set bnds to (get bounds of window of desktop)

set FullWinWidth to (item 3 of bnds)
set halfWinWidth to (item 3 of bnds) / 2
set winHeight to (item 4 of bnds)

tell application "Preview"
    if (count of windows) > 1 then
        activate
        try
            set the bounds of the first window to {0, 0, halfWinWidth, winHeight}
            set the bounds of the second window to {halfWinWidth, 0, FullWinWidth, winHeight}
        end try
    end if
end tell

获取当前文件的路径

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

推荐阅读更多精彩内容