×

Script: New Key-Combo Voice Control Command

Here is an AppleScript script tool (macOS) for generating the XML of a new Voice Control Key-Combo command. The script generates the values for all of the placeholders (shown in orange). If you choose to use a placeholder for the value for the CustomShortcutKeyCode element, you can look up on the key code afterwards on the Eastman Reference website. (Many thanks to Eastman Reference for making those key codes available!)

AppleScript Script: New Key-Combo Command XML


<key>COMMAND-ID</key> <dict> <key>CustomAppName</key> <string>TARGET-APP-NAME</string> <key>CustomCommands</key> <dict> <key>LOCALE-CODE</key> <array> <string>COMMAND-STRING</string> </array> </dict> <key>CustomModifyDate</key> <string>ISO-DATE-STRING</string> <key>CustomScope</key> <string>TARGET-APP-ID</string> <key>CustomShortcutKeyCode</key> <integer>KEY-CODE-GOES-HERE</integer> <key>CustomShortcutModifierFlags</key> <integer>MODIFIER-FLAGS</integer> <key>CustomType</key> <string>Shortcut</string> <key>Enabled</key> <true/> </dict>

* NOTE: If not modifier keys are indictated, the key CustomShortcutModifierFlags is omitted.

Property List Editor and Tools

To edit Voice Control commands files, you’ll need an XML editor application that accepts the creation of XML elements via the pasting of XML text from the clipboard. The examples and downloads provided on this website were created using PlistEdit Pro from Fat Cat Software.

DOWNLOAD SCRIPT

To install, place the AppleScript script file in the Home ▶︎ Library ▶︎ Scripts folder on your computer. If there is no Scripts folder, make a new one. Next activate the system-wide Script Menu from the General Preferences in the Script Editor application that comes with macOS (Application ▶︎ Utilities folder). Installed scripts will be available from the activated Script Menu in the top right menu bar.

AppleScript Script: New Key-Combo Voice Control Command


use AppleScript version "2.4" -- Yosemite (10.10) or later use framework "Foundation" use scripting additions -- classes, constants, and enums used property NSUserDefaults : a reference to current application's NSUserDefaults property NSNumberFormatter : a reference to current application's NSNumberFormatter property NSString : a reference to current application's NSString property scriptID : "com.nyhthawk-productions.script.vc-key-combo-command" property defaultTargetAppName : "Finder" property defaultTargetAppID : "com.apple.finder" global targetAppName, targetAppID ## USER DEFAULTS set theDefaults to NSUserDefaults's alloc()'s initWithSuiteName:scriptID theDefaults's registerDefaults:{targetAppName:defaultTargetAppName, targetAppID:defaultTargetAppID} set targetAppName to (theDefaults's stringForKey:"targetAppName") as string set targetAppID to (theDefaults's stringForKey:"targetAppID") as string ## USER PROMPT repeat set dialogMessage to "This script will generate the XML text for a new Key-Combo Voice Control command element." & return set dialogMessage to dialogMessage & return & "Target Application: " & targetAppName set dialogMessage to dialogMessage & return & "Application ID: " & targetAppID display dialog dialogMessage with title "Voice Control Command: Key-Combo" buttons {"Cancel", "Set App", "Continue"} default button 3 set actionChoice to the button returned of the result if actionChoice is "Continue" then exit repeat else if actionChoice is "Set App" then set targetApp to choose application with prompt "Select the application to use:" tell targetApp set targetAppID to the id of it set targetAppName to the name of it end tell theDefaults's setObject:targetAppID forKey:"targetAppID" theDefaults's setObject:targetAppName forKey:"targetAppName" end if end repeat ## GENERATE THE COMMAND ID set IDstring to ("Custom." & generateAbsoluteTimeID()) as string ## GENERATE THE CURRENT DATE IN ISO FORMAT set dateString to do shell script "date +'%Y-%m-%dT%H:%M:%SZ'" ## GET THE CURRENT LOCALE set localeString to user locale of (system info) ## PROMPT FOR COMMAND STRING repeat display dialog "Enter the command text or phrase:" default answer "" with title "Voice Control Command: Key-Combo" set commandString to the text returned of the result if commandString is not "" then exit repeat end repeat ## PROMPT FOR KEY set keyCode to keycodeForCharacter() if keyCode is false then error number -128 if keyCode is "X" then set keyCode to "REPLACE-WITH-KEY-CODE" end if ## PROMPT FOR MODIFIER KEYS set modiferFlags to modiferFlagsForKeyCombo() if modiferFlags is false then error number -128 ## INSERT VALUES INTO TEMPLATE if modiferFlags is "0" then -- no modifier keys set commandXML to " <key>" & IDstring & "</key> <dict> <key>CustomAppName</key> <string>" & targetAppName & "</string> <key>CustomCommands</key> <dict> <key>" & localeString & "</key> <array> <string>" & commandString & "</string> </array> </dict> <key>CustomModifyDate</key> <date>" & dateString & "</date> <key>CustomScope</key> <string>" & targetAppID & "</string> <key>CustomShortcutKeyCode</key> <integer>" & keyCode & "</integer> <key>CustomType</key> <string>Shortcut</string> <key>Enabled</key> <true/> </dict> " else set commandXML to " <key>" & IDstring & "</key> <dict> <key>CustomAppName</key> <string>" & targetAppName & "</string> <key>CustomCommands</key> <dict> <key>" & localeString & "</key> <array> <string>" & commandString & "</string> </array> </dict> <key>CustomModifyDate</key> <string>" & dateString & "</string> <key>CustomScope</key> <string>" & targetAppID & "</string> <key>CustomShortcutKeyCode</key> <integer>" & keyCode & "</integer> <key>CustomShortcutModifierFlags</key> <integer>" & modiferFlags & "</integer> <key>CustomType</key> <string>Shortcut</string> <key>Enabled</key> <true/> </dict> " end if ## PUT COMMAND XML ON CLIPBOARD set the clipboard to commandXML ## PROMPT THE USER display dialog "The XML text for a new Key-Combo Voice Control command is on the clipboard." buttons {"Lookup Key Code", "Done"} default button 2 with title "Voice Control Command: Key-Combo" if the button returned of the result is "Lookup Key Code" then ## VISIT THE EASTMAN REFERENCE WEBSITE open location "https://eastmanreference.com/complete-list-of-applescript-key-codes" end if on generateAbsoluteTimeID() ## 669490008.038141 set currentAbsoluteTime to current application's CFAbsoluteTimeGetCurrent() set fmtr to NSNumberFormatter's new() fmtr's setFormat:"#.#########" set exportDateString to NSString's stringWithFormat_("%@", fmtr's stringFromNumber:currentAbsoluteTime) as string if length of exportDateString is not 23 then repeat until length of exportDateString is 18 set exportDateString to exportDateString & (random number from 1 to 9) as string end repeat end if return exportDateString end generateAbsoluteTimeID on modiferFlagsForKeyCombo() set keyOptions to {"NO MODIFIER KEY(S): 0", ¬ "COMMAND (⌘): 1048576", ¬ "OPTION (⌥): 524288", ¬ "SHIFT (⇧): 131072", ¬ "CONTROL (⌃): 262144", ¬ "OPTION SHIFT (⌥⇧): 655360", ¬ "CONTROL OPTION (⌃⌥): 786432", ¬ "CONTROL SHIFT (⌃⇧): 393216", ¬ "CONTROL OPTION SHIFT (⌃⌥⇧): 917504", ¬ "OPTION COMMAND (⌥⌘): 1572864", ¬ "SHIFT COMMAND (⇧⌘): 1179648", ¬ "OPTION SHIFT COMMAND (⌥⇧⌘): 1703936", ¬ "CONTROL COMMAND (⌃⌘): 1310720", ¬ "CONTROL OPTION COMMAND (⌃⌥⌘): 1835008", ¬ "CONTROL SHIFT COMMAND (⌃⇧⌘): 1441792", ¬ "CONTROL OPTION SHIFT COMMAND (⌃⌥⇧⌘): 1966080"} set chosenItem to choose from list keyOptions with prompt "Choose the modifer key(s):" default items {item 1 of keyOptions} if chosenItem is false then return false set chosenItem to chosenItem as string set x to the offset of ":" in chosenItem set modifierFlags to text from (x + 2) to -1 of chosenItem return modifierFlags end modiferFlagsForKeyCombo on keycodeForCharacter() set USKeyCodes to {"USE PLACEHOLDER X", "A 0", "B 11", "C 8", "D 2", "E 14", "F 3", "G 5", "H 4", "I 34", "J 38", "K 40", "L 37", "M 46", "N 45", "O 31", "P 35", "Q 12", "R 15", "S 1", "T 17", "U 32", "V 9", "W 13", "X 7", "Y 16", "Z 6", "` 50", "1 18", "2 19", "3 20", "4 21", "5 23", "6 22", "7 26", "8 28", "9 25 ", "0 29", "- 27", "= 24", "⌫ 51", "~ 50", "! 18", "@ 19", "# 20", "$ 21", "% 23", "^ 22", "& 26", "* 28", "( 25", ") 29", "_ 27", "+ 24", "{ 33", "} 30", "[ 33", "] 30", "| 42", "\\ 42", "< 43", "> 47", ", 43", ". 47", "? 44", "/ 44", "⬆︎ 126", "⬇︎ 125", "⬅︎ 123", "➡︎ 124"} set keyCode to (choose from list USKeyCodes default items {item 1 of USKeyCodes} with prompt "Character Key Code. TIP: Type the key character") if keyCode is false then return false set x to offset of tab in (keyCode as string) set keyCode to text from (x + 1) to -1 of (keyCode as string) return keyCode end keycodeForCharacter

 

 

 

“The power of the computer should reside in the hands of the one using it. — Sal Soghoian”

LEGAL

Copyright 2022 Sal Soghoian (Nyhthawk Productions)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. VOICE-CONTROL.ORG assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. VOICE-CONTROL.ORG provides this only as a convenience to our users. VOICE-CONTROL.ORG has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and VOICE-CONTROL.ORG assumes no responsibility in this regard. Please understand that a third-party site is independent from VOICE-CONTROL.ORG and that VOICE-CONTROL.ORG has no control over the content on that website. Please contact the vendor for additional information.