Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to create an applet which takes the selected items in the Finder and creates a ist of RTF links in the clipboard, using AppleScript.

The code is :

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

Everything works great, except the final "echo" to "pbcopy".

The backslashes get mangled and interpreted as escape codes instead of RTF codes!

I tried copying the final value of var fc manually, and pasting it in Terminal, and that worked great.

But when it comes from AppleScript, the "\" get sent as "\\" and it breaks the RTF codes.

Thanks for any help!

share|improve this question
    
Just filter it through another sed substitution? –  Daniel Beck Feb 15 '11 at 20:02
add comment

3 Answers

up vote 3 down vote accepted

I found the solution in this thread: 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'"
share|improve this answer
    
This is even undocumented. According to man pbcopy, the -Prefer parameter is only supported by pbpaste... –  Daniel Beck Feb 15 '11 at 20:46
    
actually, you don't need the -prefer rtf –  Ze'ev Feb 17 '11 at 17:09
add comment

Do you need to use pbcopy to copy your text to the clipboard? AppleScript has the capability to do that by itself, using something like this:

set the clipboard to r
share|improve this answer
    
I tried that, but then the clipboard was plaintext, not richtext! –  Ze'ev Feb 15 '11 at 20:10
add comment

So the problem was that sh's echo interprets escape sequences within single quotes by default.

(do shell script "echo " & quoted form of "\\t") is tab → true

Any of these should preserve the literal backslashes:

  • 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 isn't available)

share|improve this answer
1  
If you start bash as sh, it is equivalent to bash --posix (POSIX compliance mode), in which echo defaults to -e option evaluating escape sequences and ignoring all arguments — so echo -E is useless here), unless shopt -s xpg_echo is set. –  Daniel Beck Nov 10 '11 at 20:46
    
@DanielBeck I edited the answer to mention unsetting xpg_echo. –  Lri Nov 10 '11 at 21:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.