Script: New Run Omni Automation Script Voice Control Command
Here is an AppleScript script tool (macOS) for generating the XML of a new Voice Control Open URL command. this script tool will generate the values for all of the placeholders (shown in orange) and place the resulting XML on the clipboard. The encoded script URL string value for the key CustomURLStringList can be created by the script, or be inserted later by the user.
For example, the following Omni Automation script copied to the clipboard prior to running the script tool, will return XML similar to the examples.
Hello World!
new Alert("Greeting", "Hello World!").show()
The XML for an Open URL Voice Control command that has a system-wide scope:
AppleScript Script: New Open URL Command XML
<key>Custom.672708148.08394381</key><dict><key>CustomCommands</key><dict><key>en_US</key><array><string>Show Greeting</string></array></dict><key>CustomModifyDate</key><date>2022-04-26T16:22:28Z</date><key>CustomScope</key><string>com.apple.speech.SystemWideScope</string><key>CustomType</key><string>URL</string><key>CustomURLStringList</key><array><string>omnifocus://localhost/omnijs-run?script=new%20Alert%28%22Greeting%22%2C%20%22Hello%20World%21%22%29%2Eshow%28%29</string></array><key>Enabled</key><true/></dict>
The XML for an Open URL Voice Control command that has an application-specific scope:
AppleScript Script: New Open URL Command XML
<key>Custom.672708148.08394381</key><dict><key>CustomAppName</key><string>OmniFocus</string><key>CustomCommands</key><dict><key>en_US</key><array><string>Show Greeting</string></array></dict><key>CustomModifyDate</key><date>2022-04-26T16:22:28Z</date><key>CustomScope</key><string>com.omnigroup.OmniFocus3</string><key>CustomType</key><string>URL</string><key>CustomURLStringList</key><array><string>omnifocus://localhost/omnijs-run?script=new%20Alert%28%22Greeting%22%2C%20%22Hello%20World%21%22%29%2Eshow%28%29</string></array><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)
- 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.
1st-Run of the Script Command
For security purposes, external Omni Automation script URLs must be approved by the user. In the forthcoming security dialog, there is an option to indictate that further approval is not necessary (see below). Full documentation regarding Omni Automation script security is available here.
After approval by the user:
AppleScript Script Code
The code of the script tool, written in AppleScript-ObjC in the Script Debugger editor, is shown below:
AppleScript Script: New Run Omni Automation Script 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 NSNumberFormatterNoStyle : a reference to 0property NSCharacterSet : a reference to current application's NSCharacterSetproperty NSString : a reference to current application's NSStringproperty scriptID : "com.nyhthawk-productions.script.vc-run-omni-automation-script"property defaultTargetAppName : "OmniFocus"property defaultTargetAppID : "com.omnigroup.OmniFocus4"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 Open URL Voice Command element that will execute an Omni Automation script." & returnset dialogMessage to dialogMessage & return & "Target Application: " & targetAppNameset dialogMessage to dialogMessage & return & "Application ID: " & targetAppIDdisplay dialog dialogMessage with title "Voice Control Command: Open URL" 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## QUERY USERset dialogMessage to "Should this Voice Control command be available only in " & targetAppName & " or System-Wide?"display dialog dialogMessage with title "Availability of Voice Control Command" buttons {"Cancel", "System-Wide", targetAppName} default button 3set chosenScope to the button returned of the result## PROMPT FOR SCRIPT ENCODINGdisplay dialog "Should this script encode the Omni Automation script currently on the clipboard?" buttons {"Cancel", "Skip Encoding", "Encode Copied Script"} default button 3 with title "Voice Control Command: Open URL"if button returned of the result is "Encode Copied Script" thenset lowercaseAppName to my changeCaseOfText((targetAppName as string), 1)set contentsOfSelection to get the clipboardset encodedString to my encodeUsingPercentEncoding(contentsOfSelection)set scriptURL to lowercaseAppName & "://localhost/omnijs-run?script=" & encodedStringset URLPlaceholder to scriptURLelseset URLPlaceholder to "REPLACE-WITH-URL"end if## PROMPT FOR COMMAND STRINGrepeatdisplay dialog "Enter the command text or phrase:" default answer "" with title "Voice Control Command: Open URL"set commandString to the text returned of the resultif commandString is not "" then exit repeatend repeat## GENERATE COMMAND IDENTIFIERset IDstring to ("Custom." & generateAbsoluteTimeID()) as string## GENERATE DATE STRINGset dateString to do shell script "date +'%Y-%m-%dT%H:%M:%SZ'"## GET THE CURRENT LOCALEset localeString to user locale of (system info)## CREATE THE XML ELEMENTif chosenScope is "System-Wide" thenset commandXML to " <key>" & IDstring & "</key><dict><key>CustomCommands</key><dict><key>" & localeString & "</key><array><string>" & commandString & "</string></array></dict><key>CustomModifyDate</key><date>" & dateString & "</date><key>CustomScope</key><string>com.apple.speech.SystemWideScope</string><key>CustomType</key><string>URL</string><key>CustomURLStringList</key><array><string>" & URLPlaceholder & "</string></array><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><date>" & dateString & "</date><key>CustomScope</key><string>" & targetAppID & "</string><key>CustomType</key><string>URL</string><key>CustomURLStringList</key><array><string>" & URLPlaceholder & "</string></array><key>Enabled</key><true/></dict>"end if## ANNOUNCE TO USERset the clipboard to commandXMLdisplay dialog "The new Open URL Voice Command XML is on the clipboard." buttons {"OK"} default button 1 with title "Voice Control Command: Open URL"on encodeUsingPercentEncoding(sourceText)try-- create a Cocoa string from the passed AppleScript string, by calling the NSString class method stringWithString:set the sourceString to NSString's stringWithString:sourceText-- indictate the allowed characters that do not get encodedset allowedCharacterSet to NSCharacterSet's alphanumericCharacterSet-- apply the indictated transformation to the Cooca stringset the adjustedString to the sourceString's stringByAddingPercentEncodingWithAllowedCharacters:(allowedCharacterSet)-- convert from Cocoa string to AppleScript stringreturn (adjustedString as string)on error msgdisplay dialog msgend tryend encodeUsingPercentEncodingon changeCaseOfText(sourceText, caseIndicator)-- create a Cocoa string from the passed text, by calling the NSString class method stringWithString:set the sourceString to NSString's stringWithString:sourceText-- apply the indictated transformation to the Cocoa stringif the caseIndicator is 0 thenset the adjustedString to sourceString's uppercaseString()else if the caseIndicator is 1 thenset the adjustedString to sourceString's lowercaseString()elseset the adjustedString to sourceString's capitalizedString()end if-- convert from Cocoa string to AppleScript stringreturn (adjustedString as string)end changeCaseOfTexton 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 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.