A post on another category reminded me that there’s a feature I’d like to see: Straighten quotes. It would turn smart quotes into dumb quotes, work on selected text and if no text was selected work on the entire document.
Something like this:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application id "com.latenightsw.ScriptDebugger7" -- Script Debugger.app
tell document 1
set selRange to character range of selection
if item 2 of selRange > 0 then
set theText to contents of selection
set theText to my swapQuotes(theText)
set contents of selection to theText
else
set theText to source text
set theText to my swapQuotes(theText)
set source text to theText
end if
set character range of selection to selRange
end tell
end tell
on swapQuotes(theText)
set saveTID to AppleScript's text item delimiters
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 swapQuotes
Wow. So there is still a place for TIDs in ASObjC land?
Of course — as long as you use at least two at a time
How about three at a time?
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application id "com.latenightsw.ScriptDebugger7" -- Script Debugger.app
tell document 1
set selRange to character range of selection
if item 2 of selRange > 0 then
set theText to contents of selection
set theText to my swapQuotes(theText)
set contents of selection to theText
else
set theText to source text
set theText to my swapQuotes(theText)
set source text to theText
end if
set character range of selection to selRange
end tell
end tell
on swapQuotes(theText)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"\"", "”", "“"}
set theText to (text items of theText) as text
set AppleScript's text item delimiters to saveTID
return theText
end swapQuotes
The more I look at this the more I see it wouldn’t be a good idea to make it a built in feature the way BB Edit does.
Any smart quotes inside text strings get converted, and that throws off compiling. I don’t know if it would even be possible to do this without converting quotes in text strings.
Second, if you’re going to have a feature to straighten quotes, you probably want one to “educate quotes” the way BB Edit does. And that would be even more difficult.
I saved the above script to my script folder, but its limited use would only be to fix scripts posted with curly quotes, which doesn’t happen that often.
Consider the request withdrawn!
I was half wondering if you’d conclude that after playing with it for a while.