0%

AppleScript使用

早年因为没有记录的习惯,导致实现过一个自动生成 markdown link 的 popclip 插件找不到了,其中就用的 AppleScript 来实现的。
那就从新出发。

Mac OS 上打开 Automater.app 然后新建”快捷操作”→”资源库下的实用工具”→”运行AppleScript”,就可以填入 AppleScript 脚本进行测试了。
然后测试好的脚本可以放在 popclip 也可以放在 alfred 里做成任务。

  1. 基本操作
    1. 打开一个safari新页签
    2. safari当前选中文字加上连接,制作成 markdown link
  2. popclip
    1. plist文件
    2. AppleScript文件
    3. .popclipext 文件夹重名
  3. alfred
    1. 使用 alfed 来实现切换 siri的语音选项

基本操作

打开一个safari新页签

-- 打开一个safari新页签
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
-- safari当前选中文字加上连接,制作成 markdown link

tell application "Safari"

if not running then
run
delay 0.5
end if
activate

-- Back up clipboard contents:
set savedClipboard to the clipboard

tell application "System Events" to keystroke "c" using {command down}
delay 0.5 -- Without this, the clipboard may have stale data.
set nameText to the clipboard

tell application "System Events" to keystroke "l" using {command down}
delay 0.5 -- Without this, the clipboard may have stale data.
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
delay 0.5 -- Without this, the clipboard may have stale data.

set theSelectedText to the clipboard

-- display dialog "[" & name & "](" & theSelectedText & ")"

set the clipboard to "[" & nameText & "](" & theSelectedText & ")"
end tell

popclip

制作一个 popclip 插件,如果用的 AppleScript 脚本的话,需要三个步骤。

  1. plist文件,Config.plist
  2. AppleScript文件
  3. 把上面两个文件放在一个文件夹下,并重命名文件夹为 .popclipext

下面是一个浏览器直接提取 markdown 链接的例子,支持 firefox 和 safari,你也可以添加上 chrome,我的chrome卸载了所以两个就够了。

plist文件

这里是配置文件,放一些配置信息,默认应该是 Config.plist。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Actions</key>
<array>
<dict>
<key>AppleScript File</key>
<string>mdLink.applescript</string>
<key>Image File</key>
<string>sirisay.png</string>
<key>Long Running</key>
<true/>
<key>Title</key>
<string>mdLink</string>
</dict>
</array>
<key>Extension Description</key>
<string>siri say.</string>
<key>Extension Identifier</key>
<string>com.sseen.popclip.extension.mdLink</string>
<key>Extension Name</key>
<string>mdLink</string>
<key>Required Software Version</key>
<integer>701</integer>
</dict>
</plist>

AppleScript文件

这就是执行的脚本,注意文件名和 plist 里的对应。
这里注意一点就是 AppleScript 里的 tell application 后面不能跟一个 variable,必须是一个显示的字符串才行。

global frontApp, frontAppName, windowTitle

set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp as string
-- display dialog frontAppName
if (frontAppName = "Firefox") then
tell application "Firefox"
activate
-- Back up clipboard contents:
set savedClipboard to the clipboard

tell application "System Events" to keystroke "c" using {command down}
delay 0.2 -- Without this, the clipboard may have stale data.
set nameText to the clipboard

tell application "System Events" to keystroke "l" using {command down}
delay 0.2 -- Without this, the clipboard may have stale data.
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
delay 0.2 -- Without this, the clipboard may have stale data.

set theSelectedText to the clipboard

-- display dialog "[" & name & "](" & theSelectedText & ")"

set the clipboard to "[" & nameText & "](" & theSelectedText & ")"
end tell
else
tell application "Safari"
activate
-- Back up clipboard contents:
set savedClipboard to the clipboard

tell application "System Events" to keystroke "c" using {command down}
delay 0.2 -- Without this, the clipboard may have stale data.
set nameText to the clipboard

tell application "System Events" to keystroke "l" using {command down}
delay 0.2 -- Without this, the clipboard may have stale data.
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
delay 0.2 -- Without this, the clipboard may have stale data.

set theSelectedText to the clipboard

-- display dialog "[" & name & "](" & theSelectedText & ")"

set the clipboard to "[" & nameText & "](" & theSelectedText & ") "
end tell
end if

tell application "System Events"
key code 53
end tell

end tell

return {frontAppName, windowTitle}

.popclipext 文件夹重名

放在一个文件夹下,重名如上后缀,双击就能添加到 popclipe。

alfred

使用 alfed 来实现切换 siri的语音选项

-- us voice setting use sript
-- 使用 alfed 来实现切换 siri的语音选项

on alfred_script(q)
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
get the name of every anchor of current pane
reveal anchor "TextToSpeech" of pane "com.apple.preference.universalaccess"
end tell
tell application "System Events"
tell process "System Preferences"
set visible to true
-- return every UI element of front window
-- return name of every UI element of front window
-- return every UI element of window 1
-- get name of every button of window 1
tell group 1 of window 1
delay 0.1
tell pop up button 1
click
delay 0.1
tell menu 1
click menu item 6
end tell
delay 0.1
-- click menu item "系统声音" of menu 1
end tell
end tell
end tell
end tell
end alfred_script

-- jp voice setting use sript
-- 使用 alfed 来实现切换 siri的语音选项,使用 jp 语音
on alfred_script(q)
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
get the name of every anchor of current pane
reveal anchor "TextToSpeech" of pane "com.apple.preference.universalaccess"
end tell
tell application "System Events"
tell process "System Preferences"
set visible to true
-- return every UI element of front window
-- return name of every UI element of front window
-- return every UI element of window 1
-- get name of every button of window 1
tell group 1 of window 1
delay 0.1
tell pop up button 1
click
delay 0.1
tell menu 1
click menu item 3
end tell
delay 0.1
-- click menu item "系统声音" of menu 1
end tell
end tell
end tell
end tell
end alfred_script