×

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)

  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.

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.

Script Security Dialog

After approval by the user:

Greeting Dialog

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 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 NSNumberFormatterNoStyle : a reference to 0 property NSCharacterSet : a reference to current application's NSCharacterSet property NSString : a reference to current application's NSString property scriptID : "com.nyhthawk-productions.script.vc-run-omni-automation-script" property defaultTargetAppName : "OmniFocus" property defaultTargetAppID : "com.omnigroup.OmniFocus4" 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 Open URL Voice Command element that will execute an Omni Automation script." & 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: Open URL" 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 ## QUERY USER set 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 3 set chosenScope to the button returned of the result ## PROMPT FOR SCRIPT ENCODING display 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" then set lowercaseAppName to my changeCaseOfText((targetAppName as string), 1) set contentsOfSelection to get the clipboard set encodedString to my encodeUsingPercentEncoding(contentsOfSelection) set scriptURL to lowercaseAppName & "://localhost/omnijs-run?script=" & encodedString set URLPlaceholder to scriptURL else set URLPlaceholder to "REPLACE-WITH-URL" end if ## PROMPT FOR COMMAND STRING repeat display dialog "Enter the command text or phrase:" default answer "" with title "Voice Control Command: Open URL" set commandString to the text returned of the result if commandString is not "" then exit repeat end repeat ## GENERATE COMMAND IDENTIFIER set IDstring to ("Custom." & generateAbsoluteTimeID()) as string ## GENERATE DATE STRING 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) ## CREATE THE XML ELEMENT if chosenScope is "System-Wide" then set 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> " 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> <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 USER set the clipboard to commandXML display 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 encoded set allowedCharacterSet to NSCharacterSet's alphanumericCharacterSet -- apply the indictated transformation to the Cooca string set the adjustedString to the sourceString's stringByAddingPercentEncodingWithAllowedCharacters:(allowedCharacterSet) -- convert from Cocoa string to AppleScript string return (adjustedString as string) on error msg display dialog msg end try end encodeUsingPercentEncoding on 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 string if the caseIndicator is 0 then set the adjustedString to sourceString's uppercaseString() else if the caseIndicator is 1 then set the adjustedString to sourceString's lowercaseString() else set the adjustedString to sourceString's capitalizedString() end if -- convert from Cocoa string to AppleScript string return (adjustedString as string) end changeCaseOfText 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.