Pergunta

I've run into a strange problem when trying to include quote marks ' ' in my osascript command.

If I try and escape a normal escapable character, it works fine. Example: osascript -e 'tell app "Finder" to display dialog "Te\\st"' A dialog box from Finder pops up with the text Test in it.

However, the problem occurs when I try and use apostrophes when I'm writing out full sentences. Example: osascript -e 'tell app "Finder" to display dialog "Te\'st"' When I run this, all I'm left with is no dialog box, and the text input in terminal looking like this:

>

From what I know, this should by all means work, however, it doesn't.

Foi útil?

Solução 2

You can do it this way:

osascript -e "tell app \"Finder\" to display dialog \"'Something' in quotes"\"

Outras dicas

Just to complement @Zero's helpful answer (which indeed does solve the problem):

Since you're using osascript, it is the shell's (bash's) quoting rules that apply first:

In bash (or any POSIX-compatible shell), you cannot include single quotes in a single-quoted string - not even with escaping.

What you CAN do, however, is to break your string into multiple pieces and simply splice in single quotes where needed (escaped outside a quoted string as \'):

osascript -e 'tell app "Finder" to display dialog "Te'\''st"'
  • 'tell app "Finder" to display dialog "Te', the first part, is followed by the escaped single quote \', followed by the remainder of the string 'st"'
  • By virtue of having NO spaces between the parts, bash creates a single string that does contain the spliced-in literal '.

It is generally easier to pass single-quoted strings to osascript, since double quotes are frequently used in AppleScript and therefore have to be escaped when enclosed in a double-quoted string (as in the accepted answer).

In the typically infrequent event that you must pass a single quote to AppleScript, you can use the technique described in this answer.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top