我正在尝试编写AppleScript服务,以将选定的超链接添加到Safari阅读列表中。对于裸URL,这很容易。例如,下面是我的服务,它接收选定的URL(输入是“仅URL”):
on run {theUrl}
using terms from application "Safari"
tell application "Safari"
add reading list item theUrl as string
end tell
end using terms from
return theUrl
end run
但是,如果所选文本是超链接(但不是裸URL),这显然不起作用,例如StackOverflow。有没有办法使服务也适用于超链接文本?感谢。
答案 0 :(得分:2)
可悲的是,没有好的解决方案,但你可以尝试以下方法(粗略概述):
text
作为输入 - 即使选择了超链接,它也会启用它(尽管它会有效地启用任何文本)rich text
的服务,也只会将 plain 文本传递给服务处理程序。)要从剪贴板获取RTF或HTML数据,需要进行一些黑客攻击(谢谢,https://stackoverflow.com/a/2548429/45375):
set html to do shell script "osascript -e '«class HTML» of (the clipboard as record)' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))'"
set rtf to do shell script "osascript -e '«class RTF » of (the clipboard as record)' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))'"
您可以使用正则表达式解析结果以提取URL。
请注意,有些应用只将RTF放在剪贴板(Safari)上,其他应用放置了HTML和RTF(Chrome)。
如果您愿意解决这个问题,请告诉我您是否需要特定步骤的帮助。祝你好运。
更新:根据要求,用于调用最前面的应用程序的copy-to-clipboard命令的代码:
tell application "System Events" to keystroke "c" using command down
唯一需要注意的是,辅助设备的访问必须通过System Preferences
启用,最多10.8,这是一个单一的全局切换,这可以从脚本启动(带有允许使用管理员密码;从10.9开始,不幸的是,每个使用GUI脚本的个人应用程序必须经过授权,这需要最终用户进行相当多的人工干预 - 这是一次性的痛苦,虽然)。
my copyToClipboard()
(*
# Copies the frontmost application's current selection to the clipboard
# using GUI scripting rather than keyboard shortcuts so as to avoid conflicts
# (with keys held down by the user while the script is sending keystrokes).
# CAVEAT: While this subroutine IS portable across *languages*, it does make an
# assumption that will hopefully hold for all applications: that the "Edit" menu is
# the *4th* menu from the left (Apple menu, app menu, File, Edit).
*)
on copyToClipboard()
tell application "System Events"
tell menu 1 of menu bar item 4 of menu bar 1 of (first process whose frontmost is true)
-- Find the menu item whose keyboard shortcut is Cmd-C
set miCopy to first menu item whose value of attribute "AXMenuItemCmdChar" is "C" and value of attribute "AXMenuItemCmdModifiers" is 0
click miCopy
end tell
end tell
end copyToClipboard
最后,这是一个子程序,用于确保启用辅助设备的访问;适用于10.9和之前:
System preferences
窗格 - 其余部分取决于用户。但是,即使用户设法授权应用程序,也必须重新启动才能开始工作。 # Example use.
try
my ensureAssistiveAccess()
on error
# Exit quietly, relying on the prompt to have provided
# sufficient information.
return
end try
# Tries to ensure that access for assistive devices is turned on so as to enable GUI scripting.
# - Up to 10.8.x, access can be turned on programmatically, on demand - via an admin authorization prompt.
# - From 10.9, the best we can do is display a GUI prompt, then open System Preferences to the relevant pane, then exit, telling the user to try again after interactively enabling access.
# Returns:
# Only returns if access is already enabled; throws an error otherwise.
# Example:
# try
# my ensureAssistiveAccess()
# on error
# # Enabling failed (10.8-: user didn't provide admin password; 10.9: user may or may not have authorized
# # this script's app as instructed by the prompt, but either way the script must be restarted.
# # Exit quietly, relying on the prompt to have provided sufficient information.
# return
# end try
on ensureAssistiveAccess()
local ok, isPreMavericks, verOs, verMajor, verMinor, appName, errMsg
# Determine if access is currently enabled.
tell application "System Events" to set ok to UI elements enabled
if not ok then
# See if we're running 10.8 or below
set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"."}}
set verOs to system version of (system info)
set verMajor to first text item of verOs as number
set verMinor to second text item of verOs as number
set AppleScript's text item delimiters to orgTIDs
set isPreMavericks to verMajor ≤ 10 and verMinor < 9
if isPreMavericks then # 10.8-: we can try to turn it on ourselves, which will prompt for authorization
try
# Try to turn it on - will prompt for authorization via admin credentials.
tell application "System Events"
set UI elements enabled to true
set ok to UI elements enabled # Check if the user actually provided the authorization.
end tell
end try
else # 10.9+: we cannot turn it on ourselves, it has to be enabled *interactively*, *per application*.
# Try a dummy GUI scripting operation - which we know will fail - in the hope that this will
# get the app at hand registered in System Preferences > Security & Privacy > Privacy > Accessibility.
# ?? Does this work?
try
tell application "System Events" to windows of process "SystemUIServer"
end try
set appName to name of current application
if appName = "osascript" then set appName to "Terminal" # ?? how can we deal with other apps that invoke `osascript`, such as Alfred?
set errMsg to "You must turn on ACCESS FOR ASSISTIVE DEVICES for application '" & appName & "' (System Preferences > Security & Privacy > Privacy > Accessibility) first, then retry."
try
display dialog errMsg & linefeed & linefeed & "Press OK to open System Preferences now; unlock, if necessary, then locate the application in the list and check it." with icon caution
# We only get here if the user didn't cancel.
# Open System Preferences and show the appropriate pane. (This is the best we can do in guiding the user - further guidance would require the very kind of assistive access we're trying to turn on.)
tell application "System Preferences"
activate
tell pane id "com.apple.preference.security"
reveal anchor "Privacy_Assistive"
end tell
end tell
end try
end if
end if
if not ok then
if isPreMavericks then # This indicates that the authorization prompt was aborted; for 10.9+, errMsg was set above.
set errMsg to "You must turn on ACCESS FOR ASSISTIVE DEVICES first, via System Preferences > Accessibility > Enable access for assistive devices"
end if
error errMsg
end if
end ensureAssistiveAccess
答案 1 :(得分:0)
令人讨厌的是,这是不可能的,但您是否尝试过在Safari中右键单击(按住Control键单击)上下文菜单项“添加链接到阅读列表” ?我猜你想把服务添加到任何应用程序,在这种情况下,AFAIK,你运气不好。