Escape String Literals in a script

In scripting ScriptDebugger, is there an equivalent to the “Paste as String Literal” menu option.

Ideally I’d like to take a chunk of text with quotes and slashes and convert them to text with the quotes and slashes escaped.

If there’s not a scripting command that will do that, then I request it!

I think CMD-SHIFT-V will do what you want.

Thanks, but I want to script it. Not use the user interface.

It’s a fairly trivial scripting exercise:

set theText to the clipboard as text
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"\\"}}
set theText to text items of theText
set AppleScript's text item delimiters to {"\\\\"}
set theText to theText as text
set AppleScript's text item delimiters to {"\""}
set theText to text items of theText
set AppleScript's text item delimiters to {"\\\""}
set theText to theText as text
set AppleScript's text item delimiters to saveTID
tell application id "com.latenightsw.ScriptDebugger7" -- Script Debugger.app
	tell document 1
		set selection to theText
	end tell
end tell
1 Like

@ShaneStanley, how come you didn’t use the split handler in your RegexandStuff lib?
All that “TID” stuff makes my head swim. LOL

I love your lib:

set theText to split string theText ¬
  using delimiters "\\\\"

I suppose to emphasise how little’s involved. And I was getting rusty with TIDs. :slightly_smiling_face:

1 Like

That worked, thanks!

tell application "Script Debugger"
   set myText to the selection of window 1
end tell

set myList to paragraphs of myText

repeat with x from 1 to count of myList
   set thisGraph to item x of myList
   set item x of myList to EscapeQuotes(thisGraph)
end repeat

set AppleScript's text item delimiters to {", ¬" & return}
set newText to myList as text

set newText to return & "{ ¬" & return & newText & "}"
tell application "Script Debugger"
   set the selection of window 1 to newText
end tell

on EscapeQuotes(theText)
   set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"\\"}}
   set theText to text items of theText
   set AppleScript's text item delimiters to {"\\\\"}
   set theText to theText as text
   set AppleScript's text item delimiters to {"\""}
   set theText to text items of theText
   set AppleScript's text item delimiters to {"\\\""}
   set theText to theText as text
   set AppleScript's text item delimiters to saveTID
   return "\"" & theText & "\""
end EscapeQuotes