Finding and Replacing Text for every item in a list Applescript

Viewed 77

I've got an Applescript list of shortcuts and I want to replace some special characters with text.

My current list looks like this:

set hotkeyShortcutList to  {"$", "U", "J", "G", "R", "⇧+R", "⇧+Y", "⇧+G", "⇧+B", "⇧+P", "⇧+⌫", "⌃+M", "⌃+W", "⌃+S", "⌃+X", "⌃+C", "⌃+V", "⌃+N", "⇧+⌃+N", "⌃+U", "⌃+B", "⇧+⌃+A", "⌃+A", "⌥+I", "⌥+O", "⇧+⌥+I", "⇧+⌥+O", "⌥+B", "⌥+D", "⌥+S", "⌃+⌥+M", "⌃+⌥+B", "⌃+⌥+X", "⇧+⌃+G", "⇧+⌃+⌥+R", "⇧+⌃+⌥+L", "⌃+Å", "⌃+]", "⇧+⌃+Å", "⇧+⌃+}", "⇧+⌃+M", "⇧+⌃+⌥+!", "⇧+⌃+⌥+@", "⇧+⌃+⌥+£", "⇧+⌃+⌥+$", "⇧+⌃+⌥+%", "⇧+⌃+⌥+^", "⌃+1", "⌃+2", "⌃+3", "⌃+4", "⌃+5", "⌃+6", "⇧+⌃+!", "⇧+⌃+\"", "⇧+⌃+#", "⇧+⌃+€", "⇧+⌃+%", "⇧+⌃+&", "K", "⌃+K", "⌃+V", "⇧+⌃+⌥+K", "A", "Y", "Z", "⇧+⌃+*", "⇧+⌃+⌥+*", "X", "⌃+,", "⌃+.", "⇧+⌃+;", "⇧+⌃+:", "⌃+P", "⇧+⌃+)", "⇧+⌃+?", "⌃++", "Space", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "F"}

I would like to search every item of the list for occurrences of certain characters and replace them in each item.

"⇧" replaced with "shift"
"⌃" replaced with "control"
"⌥" replaced with "option"
"⌘" replaced with "command" 
"⌫" replaced with "backspace"
"→" replaced with "arrow right"

and so on,

I've tried to modify the following code repeat with each item of the list, but I can only seem to get it to work as long as I'm working with a String.

on findAndReplaceInText(theText, theSearchString, theReplacementString)
            set AppleScript's text item delimiters to theSearchString
            set theTextItems to every text item of theText
            set AppleScript's text item delimiters to theReplacementString
            set theText to theTextItems as string
            set AppleScript's text item delimiters to ""
            return theText
end findAndReplaceInText

How do I find and replace for every item of the list?

1 Answers

Try the following example AppleScript code:

set hotkeyShortcutList to {"$", "U", "J", "G", "R", "⇧+R", "⇧+Y", "⇧+G", "⇧+B", "⇧+P", "⇧+⌫", "⌃+M", "⌃+W", "⌃+S", "⌃+X", "⌃+C", "⌃+V", "⌃+N", "⇧+⌃+N", "⌃+U", "⌃+B", "⇧+⌃+A", "⌃+A", "⌥+I", "⌥+O", "⇧+⌥+I", "⇧+⌥+O", "⌥+B", "⌥+D", "⌥+S", "⌃+⌥+M", "⌃+⌥+B", "⌃+⌥+X", "⇧+⌃+G", "⇧+⌃+⌥+R", "⇧+⌃+⌥+L", "⌃+Å", "⌃+]", "⇧+⌃+Å", "⇧+⌃+}", "⇧+⌃+M", "⇧+⌃+⌥+!", "⇧+⌃+⌥+@", "⇧+⌃+⌥+£", "⇧+⌃+⌥+$", "⇧+⌃+⌥+%", "⇧+⌃+⌥+^", "⌃+1", "⌃+2", "⌃+3", "⌃+4", "⌃+5", "⌃+6", "⇧+⌃+!", "⇧+⌃+\"", "⇧+⌃+#", "⇧+⌃+€", "⇧+⌃+%", "⇧+⌃+&", "K", "⌃+K", "⌃+V", "⇧+⌃+⌥+K", "A", "Y", "Z", "⇧+⌃+*", "⇧+⌃+⌥+*", "X", "⌃+,", "⌃+.", "⇧+⌃+;", "⇧+⌃+:", "⌃+P", "⇧+⌃+)", "⇧+⌃+?", "⌃++", "Space", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "F"}

set processedShortcutList to {}
repeat with thisShortcut in hotkeyShortcutList
    set thisShortcut to my findAndReplaceInText(thisShortcut, "⇧", "shift")
    set thisShortcut to my findAndReplaceInText(thisShortcut, "⌃", "control")
    set thisShortcut to my findAndReplaceInText(thisShortcut, "⌥", "option")
    set thisShortcut to my findAndReplaceInText(thisShortcut, "⌘", "command")
    set thisShortcut to my findAndReplaceInText(thisShortcut, "⌫", "backspace")
    set thisShortcut to my findAndReplaceInText(thisShortcut, "→", "arrow right")
    set end of processedShortcutList to thisShortcut
end repeat

return processedShortcutList

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    log theTextItems
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

Result: {"$", "U", "J", "G", "R", "shift+R", "shift+Y", "shift+G", "shift+B", "shift+P", "shift+backspace", "control+M", "control+W", "control+S", "control+X", "control+C", "control+V", "control+N", "shift+control+N", "control+U", "control+B", "shift+control+A", "control+A", "option+I", "option+O", "shift+option+I", "shift+option+O", "option+B", "option+D", "option+S", "control+option+M", "control+option+B", "control+option+X", "shift+control+G", "shift+control+option+R", "shift+control+option+L", "control+Å", "control+]", "shift+control+Å", "shift+control+}", "shift+control+M", "shift+control+option+!", "shift+control+option+@", "shift+control+option+£", "shift+control+option+$", "shift+control+option+%", "shift+control+option+^", "control+1", "control+2", "control+3", "control+4", "control+5", "control+6", "shift+control+!", "shift+control+\"", "shift+control+#", "shift+control+€", "shift+control+%", "shift+control+&", "K", "control+K", "control+V", "shift+control+option+K", "A", "Y", "Z", "shift+control+*", "shift+control+option+*", "X", "control+,", "control+.", "shift+control+;", "shift+control+:", "control+P", "shift+control+)", "shift+control+?", "control++", "Space", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "F"}

Related