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 TOOL (macOS)
- To install the script file, DOWNLOAD and run the “Setup Script Menu” applet. This script applet will turn on the system-wide “Script Menu,” and open the macOS “Scripts” folder on the desktop. This process may require your approval of security dialogs, granting this script applet access to the “AppleScript Utility” and “Finder” applications.
- Unpack the downloaded ZIP archive containing the script tool, and place the script file into the “Scripts” folder now open on the Desktop. Once added, the script will be available from the system-wide “Script Menu” located at the top right of the menu bar.
Here is the script code written in AppleScriptObj-C:
AppleScript Script: New Key-Combo Voice Control Command
use AppleScript version "2.4" -- Yosemite (10.10) or lateruse framework "Foundation"use scripting additions-- classes, constants, and enums usedproperty NSUserDefaults : a reference to current application's NSUserDefaultsproperty NSNumberFormatter : a reference to current application's NSNumberFormatterproperty NSString : a reference to current application's NSStringproperty scriptID : "com.nyhthawk-productions.script.vc-key-combo-command"property defaultTargetAppName : "Finder"property defaultTargetAppID : "com.apple.finder"global targetAppName, targetAppID## USER DEFAULTSset theDefaults to NSUserDefaults's alloc()'s initWithSuiteName:scriptIDtheDefaults's registerDefaults:{targetAppName:defaultTargetAppName, targetAppID:defaultTargetAppID}set targetAppName to (theDefaults's stringForKey:"targetAppName") as stringset targetAppID to (theDefaults's stringForKey:"targetAppID") as string## USER PROMPTrepeatset dialogMessage to "This script will generate the XML text for a new Key-Combo Voice Control command element." & returnset dialogMessage to dialogMessage & return & "Target Application: " & targetAppNameset dialogMessage to dialogMessage & return & "Application ID: " & targetAppIDdisplay dialog dialogMessage with title "Voice Control Command: Key-Combo" buttons {"Cancel", "Set App", "Continue"} default button 3set actionChoice to the button returned of the resultif actionChoice is "Continue" thenexit repeatelse if actionChoice is "Set App" thenset targetApp to choose application with prompt "Select the application to use:"tell targetAppset targetAppID to the id of itset targetAppName to the name of itend telltheDefaults's setObject:targetAppID forKey:"targetAppID"theDefaults's setObject:targetAppName forKey:"targetAppName"end ifend repeat## GENERATE THE COMMAND IDset IDstring to ("Custom." & generateAbsoluteTimeID()) as string## GENERATE THE CURRENT DATE IN ISO FORMATset dateString to do shell script "date +'%Y-%m-%dT%H:%M:%SZ'"## GET THE CURRENT LOCALEset localeString to user locale of (system info)## PROMPT FOR COMMAND STRINGrepeatdisplay dialog "Enter the command text or phrase:" default answer "" with title "Voice Control Command: Key-Combo"set commandString to the text returned of the resultif commandString is not "" then exit repeatend repeat## PROMPT FOR KEYset keyCode to keycodeForCharacter()if keyCode is false then error number -128if keyCode is "X" thenset keyCode to "REPLACE-WITH-KEY-CODE"end if## PROMPT FOR MODIFIER KEYSset modiferFlags to modiferFlagsForKeyCombo()if modiferFlags is false then error number -128## INSERT VALUES INTO TEMPLATEif modiferFlags is "0" then -- no modifier keysset 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>"elseset 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 CLIPBOARDset the clipboard to commandXML## PROMPT THE USERdisplay 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 WEBSITEopen location "https://eastmanreference.com/complete-list-of-applescript-key-codes"end ifon generateAbsoluteTimeID()## 669490008.038141set 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 stringif length of exportDateString is not 23 thenrepeat until length of exportDateString is 18set exportDateString to exportDateString & (random number from 1 to 9) as stringend repeatend ifreturn exportDateStringend generateAbsoluteTimeIDon 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 falseset chosenItem to chosenItem as stringset x to the offset of ":" in chosenItemset modifierFlags to text from (x + 2) to -1 of chosenItemreturn modifierFlagsend modiferFlagsForKeyComboon 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 falseset x to offset of tab in (keyCode as string)set keyCode to text from (x + 1) to -1 of (keyCode as string)return keyCodeend keycodeForCharacter
LEGAL
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. OMNI-AUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. OMNI-AUTOMATION.COM provides this only as a convenience to our users. OMNI-AUTOMATION.COM 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 OMNI-AUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from OMNI-AUTOMATION.COM and that OMNI-AUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.