Home > Blockchain >  Add " " character in between every character in AppleScript List
Add " " character in between every character in AppleScript List

Time:10-09

I'm trying to edit a list of shortcuts and separate keys with a " " character.

So "⌃⌥B" becomes "⌃ ⌥ B" and so on

So far I've been able to extract my shortcuts into a list named "hotkeyShortcutList".

My example hotkeyShortcutList 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"}

My script for processing so far:

set processedShortcutList to {}
repeat with i from 1 to length of hotkeyShortcutList
set theCurrentListItem to item i of hotkeyShortcutList
set processedListItem to (do shell script ("<<<" & theCurrentListItem & " sed -E 's/.{1}/& /g ; s/-$//'"))
set processedListItem to characters 1 thru -2 of processedListItem as text
set end of processedShortcutList to processedListItem
end repeat
return processedShortcutList

However I've got 3 issues:

  1. The shell script seems to work on regular characters, but bugs out when trying to handle special characters like "⇧", "⌃" and "⌥". I have very limited understanding of shell scripts, but it seemed to be the way to go with text handling from what I could Google...

  2. I need to replace special characters with text after the " "'s have been added. ⇧ replaced with "shift", ⌃ replaced with "control", ⌥ replaced with "command", ⌥ replaced with "command" etc.

  3. I'd like to add some edge case protection to keys that have multiple letters like "space" for spacebar and "F1" for F-keys, so I don't end up with "s p a c e" and "F 1".

I know AppleScript might not be the easiest way to do it, but I'll be using the values inside of an AppleScript, so I feel like it would be nice to keep it all together. Any suggestions?

CodePudding user response:

Some AppleScriptObjC (OK, just about everything) might be faster, but even doing it the long way with regular AppleScript winds up being way faster without using the shell script, since the overhead of calling the shell script multiple times is expensive. On my machine the shell script events take up 94% of the time, and your script still has more to do.

The long way involves stepping through the characters of each hotkey item, replacing special characters with their description as it goes, and adding the separators. The edge cases are taken care of while going through the characters by using a regular space in the hotkey item and replacing it the same as the other modifier characters, and by just appending the rest of the hotkey string once it finds an F (modifier keys typically come first).

Note that the symbol for control is different than the caret ^ (shift 6):

set hotkeyShortcutList to {"$", "U", "J", "G", "R", "⇧R", "⇧Y", "⇧G", "⇧B", "⇧P", "⇧⌫", "⌃M", "⌃W", "⌃^", "⌃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", "⇧⌃)", "⇧⌃?", "⌃ ", "⌘ ", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "⌘F1", "⌃F12"}
set replacements to {{" ", "space"}, {"⇧", "shift"}, {"⌃", "control"}, {"⌥", "option"}, {"⌘", "command"}, {"↵", "enter"}, {"↩", "return"}, {"⌫", "backspace"}} -- key symbols to replace

set processedShortcutList to {}

repeat with anItem in hotkeyShortcutList
    set processedItem to ""
    set anItem to contents of anItem
    repeat with here from 1 to (count anItem)
        set char to item here of anItem
        repeat with replacement in replacements -- replace key symbol with its description
            if first item of replacement is char then
                set char to second item of replacement
                exit repeat
            end if
        end repeat
        considering case and diacriticals -- tighten the comparison a little
            if char is "F" then -- can be function key, so just append the rest
                set processedItem to processedItem & text here thru -1 of anItem & " "
                exit repeat
            end if
        end considering
        set processedItem to processedItem & char & " "
    end repeat
    set end of processedShortcutList to text 1 thru -2 of processedItem -- strip last
end repeat

return processedShortcutList

CodePudding user response:

How about just replacing the special characters with their descriptive word and appending a plus sign — all in one go? As you have a modest number of odd characters, this has the benefit of obviousness as well as making it easy to add or edit substitutions.

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 newList to {}
repeat with hotkey in hotkeyShortcutList
    do shell script "echo '" & hotkey & "' | sed -e 's/⌃/control /g' -e 's/⌥/option /g' -e 's/⇧/shift /g' -e 's/⌫/backspace /g'"
    set end of newList to result
end repeat
newList

NB You have some odd characters in your source text. For example, your 'control' character is actually an 'up arrowhead' (U 2303). If your text ever has the more conventional 'circumflex accent' (U 005E) then you will need to edit the sed parameter.

The above generates the following result for me with your source list. Let me know if this is the desired output.

{"$", "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"}

CodePudding user response:

Here is a method that is faster than the other answers as it does not need to run in a repeat loop as it coerces the list to linefeed separated text to process it with a single do shell script command and sed, then coerces the result of the do shell script command back to a list.

In testing in Script Debugger the following example AppleScript code took 0.01 second, while the code in the answer from red_menace took 0.05 seconds, and the code in the answer from Mockman took 0.93 second.

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 AppleScript's text item delimiters to linefeed
set foo to hotkeyShortcutList as text
set AppleScript's text item delimiters to {}

set processedShortcutList to paragraphs of (do shell script "sed -e 's/⌃/control /g' -e 's/⌥/option /g' -e 's/⇧/shift /g' -e 's/⌫/backspace /g' <<< " & foo's quoted form)

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