美文网首页
AppleScript相关

AppleScript相关

作者: 笑破天 | 来源:发表于2022-06-21 15:14 被阅读0次

AppleScript官方指南
AppleScript 入门:探索 macOS 自动化
appleScript快速入门教程
appleScript 快速入门
初识AppleScript

一、使用说明

AppleScript 字典是一个AppleScript语言的Mac支持的多款app的使用说明文档。
位于:“AppleScript 编辑器”中,选取“窗口”>“资源库”。
主要包含:术语组(S)、命令(C)、类(C)、属性(P)、元素(E)。
使用:将应用直接拖到 Dock 上的脚本编辑器图标,然后就会显示扩展的词典
操作其他程序的界面:借助entire contents和Xcode 自带的工具 Accessibility Inspect

二、示例

1、注释和调试

display alert "This is an alert" #弹窗示例

2、点击关于计算器

tell application "System Events"
    #entire contents
    tell process "Calculator"
        click menu item "关于计算器" of menu "计算器" of menu bar item "计算器" of menu bar 1
    end tell
end tell

3、新建一个终端tab并执行命令

tell application "Terminal"
    activate
    tell application "System Events"
        keystroke "t" using {command down}
    end tell
    do script "say '哈哈'"
end tell
// tab 1 of window id 791 of application "Terminal"

多次执行window id会变化,符合视图结构,Terminal有多个window,每个window有多个tab

4、Go2Shell_Term

tell application "Finder"
     #获取Finder顶层窗口的文件夹路径并转换为替身
    set pathList to (quoted form of POSIX path of (folder of the front window as alias))
end tell

tell application "System Events"
    do shell script "open -a Terminal " & pathList
end tell

5、其他例子
ssh快速登录、多屏登录、alfred、让你的MAC自动化起来

三、自己动手写一个Finder小工具

cmd+space-自动操作 - 新建文稿 - 应用程序,左侧列表选择实用工具下的“运行AppleScript”,粘贴以下代码,文件-存储为应用程序。打开Finder,cmd+拖拽该app到Finder工具栏即可。点击图标会弹窗提示当前文件夹的路径。

tell application "Finder"
    set pathList to (quoted form of POSIX path of (folder of the front window as alias))
end tell
display alert pathList

四、其他好玩的

1、让 Mac 唱生日快乐歌

set currentUser to long user name of (get system info)
set firstName to first word of currentUser
set lastName to last word of currentUser

set noteValues to {56, 56, 58, 56, 61, 60, 50, 56, 56, 58, 56, 63, 61, 50, 56, 56, 68, 65, 61, 60, 58, 50, 66, 66, 65, 61, 63, 61} -- F
-- set noteValues to {65, 65, 67, 65, 70, 69, 60, 65, 65, 67, 65, 72, 70, 60, 65, 65, 77, 74, 70, 69, 67, 60, 75, 75, 74, 70, 72, 70} -- D
-- set noteValues to {55, 55, 57, 55, 60, 59, 50, 55, 55, 57, 55, 62, 60, 50, 55, 55, 67, 64, 60, 59, 57, 50, 65, 65, 64, 60, 62, 60} -- E

set syllables to {"hap", "p", "birth", "day", "to", "you", "[[slnc 500]]", "hap", "p", "birth", "day", "to", "you", "[[slnc 500]]", "hap", "p", "birth", "day", "dear", firstName, lastName, "[[slnc 500]]", "hap", "p", "birth", "day", "to", "you"}

repeat with i from 1 to the length of noteValues
    set thisValue to item i of noteValues
    set thisSyllable to item i of syllables
    if length of thisSyllable is less than 3 then
        set speakingRate to 220
    else
        set speakingRate to 100
    end if
    say thisSyllable pitch thisValue using "Victoria" speaking rate speakingRate modulation 0
end repeat

相关文章

网友评论

      本文标题:AppleScript相关

      本文链接:https://www.haomeiwen.com/subject/evtuvrtx.html