which is:
“Return” & space & return & “Before” & space & return & “Taxes” & tab
without manually having to key it. Seems like there would be a way to get A/S to take the current selection in Q and return the code, maybe to the clipboard.
I’m not sure what you’re asking. I think you want a string representation of the applescript commands so you can paste it into some other application??? I do not have QX but I made this work for TextEdit. You just have to adapt it for QX. It’s really just multiple find/replace commands. It worked for your example but you’ll have to check it for other cases.
-- put the selected text on the cliboard so we can get it into applescript
tell application "TextEdit" to activate
delay 0.75
tell application "System Events" to tell process "TextEdit"
keystroke "c" using command down
end tell
set selectedText to the clipboard
-- first you have to add quote marks to all of the words
set theWords to words of selectedText
repeat with i from 1 to count of theWords
set selectedText to findAndReplace(selectedText, item i of theWords, "\"" & item i of theWords & "\"")
end repeat
-- next we replace space, return, and tab with their equivalent words surrounded by "&" characters
set selectedText to findAndReplace(selectedText, space, "& space &")
set selectedText to findAndReplace(selectedText, return, "& return &")
set selectedText to findAndReplace(selectedText, " ", "& tab &")
-- now we add spaces between the quoted words and the "&" characters
set selectedText to findAndReplace(selectedText, "\"&", "\" &")
set selectedText to findAndReplace(selectedText, "&\"", "& \"")
-- next we take care of multiple && instances
repeat while selectedText contains "&&"
set selectedText to findAndReplace(selectedText, "&&", "&")
end repeat
-- next we take care of multiple quote mark instances
repeat while selectedText contains "\"\""
set selectedText to findAndReplace(selectedText, "\"\"", "\"")
end repeat
-- put quoted words next to each other back together
repeat while selectedText contains "\" & space & \""
set selectedText to findAndReplace(selectedText, "\" & space & \"", space)
end repeat
-- finally we clean up the beginning and ending of the final selectedText string
if selectedText begins with "& " then set selectedText to text 3 thru -1 of selectedText
if selectedText ends with " &" then set selectedText to text 1 thru -3 of selectedText
-- put the result back onto the clipboard
set the clipboard to selectedText
(*================ SUBROUTINES ==================*)
on findAndReplace(thetext, findText, replaceText)
set {tids, text item delimiters} to {text item delimiters, findText}
set a to text items of thetext
set text item delimiters to replaceText
set b to a as text
set text item delimiters to tids
return b
end findAndReplace
I was playing with the script I posted and I wanted it to work on the following selected text in TextEdit. Note that this is just gibberish text as I was only trying to give it a tough example.
My previous script failed miserably on that text. It seems as soon as I added punctuation the script failed. Also, if I had a word like “A” and another word like “bake” which contained an “a” that the script would also fail. In that case “bake” would turn into “b"A"ke”… not too good. So I had to rethink how to handle these cases. It came down to how I was using “words” to quote the words of the text. “Words” doesn’t account for punctuation so punctuation was being left out of the quotes. To handle the second problem I had to add “considering case”.
So the solution was to just quote every character and then iterate through the quoted text and remove any quotes that didn’t belong. As such I came up with this and it seems to work well.
property standardReplaceCharacters : {space, return, character id 9, character id 10}
property wordsForStandardReplaceCharacters : {"space", "return", "tab", "return"}
-- put the selected text on the clipboard so we can get it into applescript
tell application "TextEdit" to activate
delay 0.5
tell application "System Events" to tell process "TextEdit"
keystroke "c" using command down
end tell
delay 0.5
set selectedText to the clipboard
-- quote all the characters
set myChars to characters of selectedText
set didChar to {}
repeat with i from 1 to count of myChars
set thisChar to item i of myChars
considering case
if thisChar is not in standardReplaceCharacters and thisChar is not in didChar then
set end of didChar to thisChar
set selectedText to findAndReplace(selectedText, thisChar, "\"" & thisChar & "\"")
end if
end considering
end repeat
-- clean up the quotes
set selectedText to cleanUpQuotes(selectedText)
-- next we replace space, return, and tab with their equivalent words surrounded by "&" characters
repeat with i from 1 to count of standardReplaceCharacters
set findChar to item i of standardReplaceCharacters
set replaceChar to item i of wordsForStandardReplaceCharacters
set selectedText to findAndReplace(selectedText, findChar, "& " & replaceChar & " &")
end repeat
-- now we add spaces between the quoted words and the "&" characters
set selectedText to findAndReplace(selectedText, "\"&", "\" &")
set selectedText to findAndReplace(selectedText, "&\"", "& \"")
-- next we take care of multiple && instances
repeat while selectedText contains "&&"
set selectedText to findAndReplace(selectedText, "&&", "&")
end repeat
-- put quoted words next to each other back together
repeat while selectedText contains "\" & space & \""
set selectedText to findAndReplace(selectedText, "\" & space & \"", space)
end repeat
-- finally we clean up the beginning and ending of the final selectedText string
if selectedText begins with "& " then set selectedText to text 3 thru -1 of selectedText
if selectedText ends with " &" then set selectedText to text 1 thru -3 of selectedText
-- put the result back onto the clipboard
set the clipboard to selectedText
(*================ SUBROUTINES ==================*)
on cleanUpQuotes(thetext)
set textCount to count of thetext
if textCount is 1 then return thetext
-- determine which quote characters need removal
set quotesToRemove to {}
repeat with i from 2 to (textCount - 1)
if character i of thetext is "\"" then
if character (i + 1) of thetext is not in standardReplaceCharacters and character (i - 1) of thetext is not in standardReplaceCharacters then
set end of quotesToRemove to i
end if
end if
end repeat
-- iterate through the quotesToRemove list in reverse order so we maintain the proper character positions
if quotesToRemove is not {} then
repeat with i from (count of quotesToRemove) to 1 by -1
set thisNumber to item i of quotesToRemove
set thetext to text 1 thru (thisNumber - 1) of thetext & text (thisNumber + 1) thru -1 of thetext
end repeat
end if
return thetext
end cleanUpQuotes
on findAndReplace(thetext, findText, replaceText)
set {tids, text item delimiters} to {text item delimiters, findText}
considering case
set a to text items of thetext
end considering
set text item delimiters to replaceText
set b to a as text
set text item delimiters to tids
return b
end findAndReplace
So all-in-all it probably could use a little more refinement, but it’s working pretty well. I started helping with this question just for fun but I think now I’ll get some use out of the script too.
Glad you both like it. I think it’s working well and I’ve used it already myself. AutoFetishist, I guess you must be using 10.4 because I believe that “character id” was implemented in 10.5. Anyway, good luck.