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

你可能感兴趣的:(AppleScript实用命令)