×

Script: New Run Shortcut Voice Control Command

Here is an AppleScript script tool (macOS) for generating the XML of a new Voice Control Run Shortcut command. The script will generate the values for all of the placeholders (shown in orange) and place the resulting XML on the clipboard.

New Run Shortcut Command XML (system-wide scope)


<key>COMMAND-ID</key> <dict> <key>CustomCommands</key> <dict> <key>LOCALE-CODE</key> <array> <string>MENU-ITEM-TITLE</string> </array> </dict> <key>CustomModifyDate</key> <date>ISO-DATE-STRING</date> <key>CustomScope</key> <string>com.apple.speech.SystemWideScope</string> <key>CustomShortcutsWorkflow</key> <string>SHORTCUT-ID</string> <key>CustomType</key> <string>RunShortcutsWorkflow</string> <key>Enabled</key> <true/> </dict>
New Run Shortcut Command XML (app scope)


<key>COMMAND-ID</key> <dict> <key>CustomAppName</key> <string>TARGET-APP-NAME</string> <key>CustomCommands</key> <dict> <key>LOCALE-CODE</key> <array> <string>MENU-ITEM-TITLE</string> </array> </dict> <key>CustomModifyDate</key> <date>ISO-DATE-STRING</date> <key>CustomScope</key> <string>TARGET-APP-ID</string> <key>CustomShortcutsWorkflow</key> <string>SHORTCUT-ID</string> <key>CustomType</key> <string>RunShortcutsWorkflow</string> <key>Enabled</key> <true/> </dict>

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)

  1. 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.
  2. 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 Run Shortcut 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-run-shortcut-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 Run Shortcuts 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 ## PROMPT USER FOR SCOPE display dialog "Should this Voice Control command be available only in " & (targetAppName as string) & " or System-Wide?" with title "Availability of Voice Control Command" buttons {"Cancel", "System-Wide", targetAppName} default button 3 set chosenScope to the button returned of the result ## SHORTCUTS COMMUNICATION tell application "Shortcuts Events" set shortcutTitles to the name of every shortcut end tell ## PROMPT USER TO SELECT SHORTCUT set shortcutTitle to choose from list shortcutTitles with prompt "Select the shortcut for the command:" if shortcutTitle is false then error number -128 set shortcutTitle to shortcutTitle as string ## GET ID OF CHOSEN SHORTCUT tell application "Shortcuts Events" set shortcutID to the id of shortcut shortcutTitle end tell ## 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) ## GENERATE CORRESPONDING XML if chosenScope is "System-Wide" then set commandXML to " <key>" & IDstring & "</key> <dict> <key>CustomCommands</key> <dict> <key>" & localeString & "</key> <array> <string>" & shortcutTitle & "</string> </array> </dict> <key>CustomModifyDate</key> <date>" & dateString & "</date> <key>CustomScope</key> <string>com.apple.speech.SystemWideScope</string> <key>CustomShortcutsWorkflow</key> <string>" & shortcutID & "</string> <key>CustomType</key> <string>RunShortcutsWorkflow</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>" & shortcutTitle & "</string> </array> </dict> <key>CustomModifyDate</key> <date>" & dateString & "</date> <key>CustomScope</key> <string>" & targetAppID & "</string> <key>CustomShortcutsWorkflow</key> <string>" & shortcutID & "</string> <key>CustomType</key> <string>RunShortcutsWorkflow</string> <key>Enabled</key> <true/> </dict> " end if ## COPY XML TO CLIPBOARD set the clipboard to commandXML ## USER PROMPT display dialog "The XML text for a new Shortcuts Workflow Voice Command is on the clipboard." buttons {"OK"} default button 1 with title "Voice Control Command: Run Shortcuts Workflow" 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

 

 

 

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.