MacOS系统自带的Stickies便笺应用很好用,不仅可以固定在桌面 作为提醒,而且也支持粘贴图片等,但是默认支持的全局快捷键必须得先选中一段文本后才能触发,所以便写了一段AppleScript实现全局一键创建空白便笺,通过快捷指令绑定全局快捷键,很好用。
-- Stickies:用快捷键新建便笺(优先 ⌘N),失败自动回退并校验结果
property RETRY_DELAY : 0.12
-- 激活并等待进程就绪
tell application id "com.apple.Stickies"
launch
activate
end tell
tell application "System Events"
repeat until exists process "Stickies"
delay 0.05
end repeat
tell process "Stickies"
set beforeCount to (count of windows)
end tell
end tell
-- ① 优先:keystroke(对键盘布局友好)
try
tell application "System Events" to keystroke "n" using {command down} -- ⌘N
end try
delay RETRY_DELAY
if my notCreated(beforeCount) then
-- ② 回退:key code(对输入法更稳,物理键位 45=“N”)
tell application "System Events" to key code 45 using {command down}
delay RETRY_DELAY
end if
-- ③ 最后兜底:菜单点击(多语言支持)
if my notCreated(beforeCount) then
tell application "System Events"
tell process "Stickies"
set theMenuBar to menu bar 1
-- 找 File/文件/檔案(多语言)
set fileBarItem to missing value
repeat with nm in {"File", "文件", "檔案"}
try
set fileBarItem to menu bar item (nm as text) of theMenuBar
exit repeat
end try
end repeat
if fileBarItem is missing value then set fileBarItem to menu bar item 2 of theMenuBar
-- 打开菜单并点击 New Note/新建便笺/新增便條
perform action "AXPress" of fileBarItem
delay 0.05
set fileMenu to menu 1 of fileBarItem
set newItem to missing value
repeat with nm in {"New Note", "新建便笺", "新增便條", "新增便箋"}
try
set newItem to menu item (nm as text) of fileMenu
exit repeat
end try
end repeat
if newItem is missing value then set newItem to menu item 1 of fileMenu
perform action "AXPress" of newItem
end tell
end tell
end if
on notCreated(beforeCount)
try
tell application "System Events" to tell process "Stickies" to set afterCount to (count of windows)
return not (afterCount > beforeCount)
on error
return true
end try
end notCreated