是否可以从其当前光标位置获取Pages应用程序的文本? 我的要求是,当用户在“页面”中键入内容时,我必须为他们输入的单词显示建议。
所以我想从“ Pages”应用中找到当前光标位置附近的当前或最后一个单词。 使用AppleScript还是可访问性? 未选择文本。 我也没有在寻找“服务”。 对于除“页面”以外的应用程序,我使用了辅助功能和appleScript。但是对于页面我什么也找不到。
我也尝试使用AppleScript,但是出于某种原因,它可以在“脚本编辑器”中完美地工作,但是当我在代码中使用它时,它将进入无限循环。
tell application "Pages"
activate
end tell
tell application "System Events"
tell application "System Events"
key code 123 using {shift down, command down} -- shift-command-left
end tell
tell process "Pages"
keystroke "c" using {command down}
delay 1
tell application "System Events"
key code 124 -- shift-command-left
end tell
set myData to (the clipboard) as text
return myData
end tell
end tell
如果我在应用中运行此AppleScript,它只会冻结我的Mac,我必须强制退出Mac才能停止它。
答案 0 :(得分:2)
这对于使用最新版本的macOS Mojave和Pages的我来说有效
property theApp : "Pages" -- change value to name of any other application (TextEdit)
tell application theApp to activate
delay 3
tell application "System Events"
tell application process theApp
-- Move the insertion point to the beginning of the previous word.
key code 123 using {option down} -- left arrow key while holding option down
delay 0.2
-- Move the insertion point to the end of the next word. (selects the word)
key code 124 using {shift down, option down} -- right arrow key while holding option and shift down
delay 0.2
keystroke "c" using {command down} -- copies selected wprd
delay 0.2
-- Next 2 key code commands attempt to restore cursor location
key code 124 using {option down} -- right arrow key while holding option down
delay 0.2
key code 123 using {option down} -- left arrow key while holding option down
tell current application to set myData to (the clipboard) as text
delay 4
return myData
end tell
end tell