2

I know this has been asked repeatedly, e.g., here, or here,

but neither using \" nor quote does seem to work for me. E.g., using

set msgDate to "05-06-2013"
set quotedmsgDate to "\"" & msgDate & "\"" as string
do shell script "echo " & quoted form of quotedmsgDate

returns

"\"05-06-2013\""

I don't get why, it would be great if you could enlighten me! I am using 10.8.5 and German localization if that matters...

EDIT:

This is the script I am trying to get to work

tell application "BibDesk (original)"
    set thePublications to selection of document 1
    repeat with thePub in thePublications
        tell thePub
            if value of field "Pages" is not "" then
                set the_pages to value of field "Pages"
                set quoted_pages to "\"" & the_pages & "\"" as string
                set the_script to "/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p " & quoted form of quoted_pages
                set a to do shell script the_script
                set value of field "Pages" to a
            end if
        end tell
    end repeat
end tell

I get an error because the_script is not set correctly, resulting in

do shell script "/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p '\"151-65\"'"

which cannot be executed correctly...

Community
  • 1
  • 1
user21932
  • 185
  • 1
  • 6

1 Answers1

1

What’s happening, I think, is that the Result pane in AppleScript Editor is showing you a variable as you would use it in AppleScript.

So when the result comes back as 05-06-2013 surrounded by quotes, it interprets this as a string. And because, if you were assigning a string with quotes in it to a variable you would have to backslash those quotes, it does so here as well.

You can get a better idea of what’s happening by adding some display dialogs to your script:

set msgDate to "05-06-2013"
set quotedmsgDate to "\"" & msgDate & "\"" as string
display dialog quotedmsgDate
set echoScript to "echo " & quoted form of quotedmsgDate
display dialog echoScript
do shell script echoScript
display dialog the result

When displayed as normal text, you’ll see that the date is surrounded by quotes.

For the new form, this might be useful for testing:

set the_pages to "151-65"
--set quoted_pages to "\"" & the_pages & "\"" as string
set quoted_pages to the_pages as string
set the_script to "/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p " & quoted form of quoted_pages
display dialog the_script
set a to do shell script the_script
display dialog a
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • That's true if I use your code, but see my extended example (that does not work) above... Thanks! – user21932 Oct 25 '13 at 00:30
  • Can you be more specific about the problem with that line? The "quoted form of" is seeing double quotes in it, and so surrounding it with single quotes. What would be the correct form of that line? That is, you’ve shown us the incorrect shell script; what’s the correct one? – Jerry Stratton Oct 25 '13 at 00:47
  • on the command line, it would be `/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p "151-65"`; I think that it is executed including the \s and that results in an error... thanks for your help! I will go to bed now (3am in Germany), so please do not wonder why I don't write back – user21932 Oct 25 '13 at 01:01
  • On the command line, at least for this case, single quotes and double quotes should be equivalent. That is, the shell will see 151-65 regardless of whether the command line says "151-65" or '151-65'. This means you should be able to remove the line “set quoted_pages to "\"" & the_pages & "\"" as string” and just let “quoted form of” surround it with single quotes. Try it on the command line and see how it works; then move up to the Applescript version without BibDesk (i.e., hard-coding what you expect to find in field Pages); and then move up to pulling the page ranges from BibDesk. – Jerry Stratton Oct 25 '13 at 01:14
  • You're right, using just `quoted form of` did the trick. Thank you very much! – user21932 Oct 25 '13 at 06:08