我正在尝试创建一个小程序,它使用 AppleScript 获取 Finder 中的选定项目并在剪贴板中创建 RTF 链接列表。
代码如下:
tell application "Finder" to set s to selection
set r to "{\\rtf1"
repeat with i in s
set t to "Download " & name of i
set p to POSIX path of (i as text)
set sed to "echo " & quoted form of p & " | sed s_/Users/0/Dropbox/Public_http://dl.dropbox.com/u/DBid_"
set l to (do shell script sed) & "?dl"
set r to r & "{\\field{\\*\\fldinst{HYPERLINK \"" & l & "\"}}{\\fldrslt " & t & "}}\\par "
end repeat
set r to r & "}"
set fc to "echo " & quoted form of r & " | pbcopy"
do shell script fc
一切运行良好,除了最后的“echo”到“pbcopy”。
反斜杠被破坏并被解释为转义代码而不是 RTF 代码!
我尝试复制 var 的最终值足球俱乐部手动,并将其粘贴到终端中,效果很好。
但是当它来自 AppleScript 时,“\” 将被发送为“\\”,从而破坏 RTF 代码。
谢谢你的帮助!
答案1
我在这个帖子中找到了解决方案:http://macscripter.net/viewtopic.php?id=26498
tell application "Finder" to set s to selection
set r to "\"{\\rtf1"
repeat with i in s
set t to "Download " & name of i
set p to POSIX path of (i as text)
set sed to "echo " & quoted form of p & " | sed s_/Users/0/Dropbox/Public_http://dl.dropbox.com/u/DBid_"
set l to (do shell script sed) & "?dl"
set r to r & "{\\field{\\*\\fldinst{HYPERLINK \"" & l & "\"}}{\\fldrslt \"" & t & "\"}}\\par "
end repeat
set r to r & "}\""
do shell script "/bin/bash -c 'echo " & r & " | pbcopy'"
答案2
您是否需要使用 pbcopy 将文本复制到剪贴板?AppleScript 本身具有执行此操作的功能,使用如下方法:
set the clipboard to r
答案3
因此问题在于sh
'secho
默认解释单引号内的转义序列。
(do shell script "echo " & quoted form of "\\t") is tab
→ 正确
以下任何一项都应保留文字反斜杠:
do shell script "shopt -u xpg_echo; echo " & quoted form of "\\t"
do shell script "/bin/echo " & quoted form of "\\t"
do shell script "bash -c \"echo " & quoted form of "\\t" & "\""
(echo -E
不可用)