Script: New Menu Item Voice Control Command
Here is an AppleScript script tool (macOS) for generating the XML of a new Voice Control Menu Item command. The script will generate the values for all of the placeholders (shown in orange) and place the resulting XML on the clipboard.
AppleScript Script: New Menu Item 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>CustomMenuTextList</key>
<array>
<string>MENU-ITEM-TITLE</string>
</array>
<key>CustomModifyDate</key>
<string>ISO-DATE-STRING</string>
<key>CustomScope</key>
<string>TARGET-APP-ID</string>
<key>CustomType</key>
<string>Menu</string>
</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.
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 Menu Item 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-menu-item-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 Menu Item Voice 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: Menu Item" 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: Menu"
set commandString to the text returned of the result
if commandString is not "" then exit repeat
end repeat
## PROMPT FOR MENU ITEM TITLE
repeat
display dialog "Enter the title of the menu item:" default answer "" with title "Voice Control Command: Menu"
set menuItemTitle to the text returned of the result
if menuItemTitle is not "" then exit repeat
end repeat
## INSERT VALUES INTO TEMPLATE
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>CustomMenuTextList</key>
<array>
<string>" & menuItemTitle & "</string>
</array>
<key>CustomModifyDate</key>
<string>" & dateString & "</string>
<key>CustomScope</key>
<string>" & targetAppID & "</string>
<key>CustomType</key>
<string>Menu</string>
</dict>
"
## PUT COMMAND XML ON CLIPBOARD
set the clipboard to commandXML
## PROMPT THE USER
display dialog "The XML text for a new Menu Item Voice Command is on the clipboard." buttons {"OK"} default button 1 with title "Voice Control Command: Menu"
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
“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.