# Command

The **Command** object is used by a script to run Opus commands. Any command that you can run from a button or hotkey you can run from a script - a script can even run [commands added by other scripts](../../../scripting/example_scripts/adding_a_new_internal_command). Fundamentally, using the **Command** object is similar to configuring an Opus button. You add one or more command lines to the object just the same as you add one or more command lines to a button's function. You can tell it which files and folders to act upon, and you can use the various methods and properties of the object to modify the behavior of the command. Once the object has been initialized you can use the **Run** or **RunCommand** methods to invoke the command.

A **Command** object can be created by the [**DOpusFactory**](dopusfactory)**.Command** method. By default, this object has no source, destination, files to operate on, etc. - you must use the appropriate methods to configure the command before running it. You can also retrieve a **Command** object from the [**Func**](func)**.command** property, and in this case the source, destination, files to operate on and several other properties are pre-initialized for you.

| Property Name | Return Type                       | Description                                                                                                                                                                                                                                                                                        |
| ------------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| deselect      | _bool_                            | Set this property to **False** to prevent files used by this command from being deselected, and **True** to deselect them once the function is finished. Note that files will only be deselected if they came from a [**Tab**](tab) object, and only then if the command is successful.            |
| dest          | _object:_[**Path**](path)         | Returns a [**Path**](path) object that represents the destination folder of this command. If a destination tab is set, this will be the path in the tab. You can not set this property directly - instead, use either the **SetDest** or **SetDestTab** methods to change the destination folder.  |
| desttab       | _object:_[**Tab**](tab)           | Returns a [**Tab**](tab) object that represents the destination tab for this command (if it has one - not all commands require a destination). You can not set this property directly - instead, use the **SetDestTab** method to change the destination tab.                                      |
| filecount     | _int_                             | Returns the number of items in the **files** object.                                                                                                                                                                                                                                               |
| files         | _object:_[**Items**](items)       | Returns a [**Items**](items) object that represent the files and folders this command is to act upon. You can not modify this object directly - instead you can use the various methods (**ClearFiles**, **SetFiles**, **AddFile**, **RemoveFile**, etc.) to modify the list of items to act upon. |
| linecount     | _int_                             | Returns the number of instruction lines added to the command.                                                                                                                                                                                                                                      |
| progress      | _object:_[**Progress**](progress) | Returns a [**Progress**](progress) object that you can use to display a progress indicator to the user.                                                                                                                                                                                            |
| results       | _object:_[**Results**](results)   | After every command that is run with this object, a **Results** object is available from this property. This provides information about the outcome of the command.                                                                                                                                |
| source        | _object:_[**Path**](path)         | Returns a [**Path**](path) object that represents the source folder of this command. If a source tab is set, this will be the path in the tab. You can not set this property directly - instead, use either the **SetSource** or **SetSourceTab** methods to change the source folder.             |
| sourcetab     | _object:_[**Tab**](tab)           | Returns a [**Tab**](tab) object that represents the source tab for this command. You can not set this property directly - instead, use the **SetSourceTab** method to change the source tab.                                                                                                       |
| vars          | _object_:[**Vars**](vars)         | This [**Vars**](vars) object represents all defined variables with _command scope_ (that are scoped to this function - e.g. that were set using the **@set** directive).                                                                                                                           |

| Method Name           | **Arguments**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | Return Type                         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |                               |                                                                                                                                                                              |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| AddFile               | <p>&#x3C;string:path><br>or &#x3C;**<a href="path">Path:</a><strong>path></strong><br><strong>or &#x3C;</strong><a href="item">Item:</a>**item></p>                                                                                                                                                                                                                                                                                                                                                                 | _int_                               | <p>Adds the specified item to the collection of items this command is to act upon. You can pass the item's path as either a <em>string</em> or a <a href="path"><strong>Path</strong></a> object, and you can also pass an <a href="item"><strong>Item</strong></a> object directly.</p><p>This method returns the total number of items in the collection.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| AddFiles              | <p><em>object:</em><a href="items"><strong>Items</strong></a><br>or <a href="vector"><em><strong>Vector</strong></em></a><em>:</em><a href="item"><strong>Item</strong></a><br>or <a href="vector"><em><strong>Vector</strong></em></a><em>:</em><a href="path"><strong>Path</strong></a><br>or <a href="vector"><em><strong>Vector</strong></em></a><em>:string</em><br>or <a href="stringset"><em><strong>StringSet</strong></em></a><br>or <a href="unorderedset"><em><strong>UnorderedSet</strong></em></a></p> | _int_                               | <p>Adds the items in the specified collection to the list of items this command is to act upon. The return value is the new number of items in the collection.</p><p>You can also pass a <a href="vector"><strong>Vector</strong></a> of <a href="item"><strong>Item</strong></a> or <a href="path"><strong>Path</strong></a> objects, or full path strings, instead of a collection. Or a <a href="stringset"><strong>StringSet</strong></a> or <a href="unorderedset"><strong>UnorderedSet</strong></a> of full path strings.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                            |                               |                                                                                                                                                                              |
| AddFilesFromClipboard | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _int_                               | Adds the contents of the clipboard to the collection of items this command is to act upon. This method supports both files and file paths copied to the clipboard as text. The return value is the new number of items in the collection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |                               |                                                                                                                                                                              |
| AddFilesFromFile      | <p>&#x3C;string:path><br>&#x3C;string:encoding></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | _int_                               | <p>Reads file paths from the contents of the specified file and adds them to the item collection. You can provide the file's path as either a <em>string</em> or a <a href="path"><strong>Path</strong></a> object. The file must consist of one absolute path per line.<br>The encoding of the file is assumed to be ANSI, unless it has a BOM (byte-order-mark) at the start, or you specify the <em>encoding</em> argument. If you specify the encoding this must be a <em>string</em> equal to one of the following: <strong>utf16be</strong>, <strong>utf16le</strong>, <strong>utf8</strong>, <strong>ansi</strong> or <strong>cp:</strong><em><strong>XXXX</strong></em> where <em>XXXX</em> specifies the code page number).</p><p>The return value is the new number of items in the collection.</p>                                                                                                                                                                                  |                               |                                                                                                                                                                              |
| AddFilesFromFolder    | \<string:path>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | _int_                               | Adds the contents of the specified folder to the collection of items this command is to act upon. You can pass the folder's path as either a _string_ or a [**Path**](path) object. You can also append a [wildcard pattern](../../wildcard_reference/pattern_matching_syntax) to the path to only add files matching the specified pattern.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |                               |                                                                                                                                                                              |
| AddLine               | \<string:instruction>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | _none_                              | Adds the specified instruction line to the command that this object will run. The **AddLine** method lets you build up complicated multiple line commands - add each line in turn and then run the command using the **Run** method. For a single line command it is simpler to use the **RunCommand** method.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |                               |                                                                                                                                                                              |
| Clear                 | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _none_                              | Clears all instruction lines from the command.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |                               |                                                                                                                                                                              |
| ClearFailed           | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _none_                              | Clears the failure flags from the [**Items**](items) collection. Any items that fail when a command is run will have their **failed** property set to **True**, and once this has happened the file will be skipped over by any subsequent commands. You can call this method to reset all the failure flags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |                               |                                                                                                                                                                              |
| ClearFiles            | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _none_                              | Clears the collection of items this command is to act upon.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |                               |                                                                                                                                                                              |
| ClearModifier         | \<string:modifier>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | _none_                              | Clears any modifiers that have been set for this command. The supported modifiers are a subset of the full list of [command modifiers](../../command_reference/command_modifier_reference) - see the **SetModifier** method for a list of these. You can also pass \* to clear all modifiers that have been set.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |                               |                                                                                                                                                                              |
| CommandList           | <p><em>none</em><br>or &#x3C;string:types></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | _object:_[**StringSet**](stringset) | <p>Returns a <a href="stringset"><strong>StringSet</strong></a> containing the names of all the Opus commands. You can optionally filter this set by providing one or more of the following flags as an argument to the <strong>CommandList</strong> method:</p><table><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>i</strong></td><td>internal (built-in) commands</td><td></td></tr><tr><td><strong>s</strong></td><td>script commands</td><td></td></tr><tr><td><strong>u</strong></td><td>user commands</td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td>Dlg</td><td></td><td></td></tr></tbody></table><p><em>none</em></p>                                                                                                                                                                                                                                                                                 | _object:_[**Dialog**](dialog) | Creates a new [**Dialog**](dialog) object, that lets you display dialogs and popup menus. The dialog's **window** property will be automatically assigned to the source tab. |
|                       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| **i**                 | internal (built-in) commands                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| **s**                 | script commands                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| **u**                 | user commands                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
|                       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
|                       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| Dlg                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| GetModifiers          | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _object:_[**Map**](map)             | Returns a \*\*[Map](map)\*\*of the modifiers that have been set for this command (either by the **SetModifier** method, or in the case of [script add-ins](../../../scripting/script_add-ins) any modifiers that were set on the button that invoked the script).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |                               |                                                                                                                                                                              |
| IsSet                 | <p>&#x3C;string:condition><br>[&#x3C;string:command>]</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                           | _bool_                              | Returns **True** if the specified [**Set**](../../command_reference/internal_commands/set) command condition is true. This is the equivalent of the **@ifset** [command modifiers.](../../command_reference/command_modifier_reference) The optional second parameter lets you test a condition based on a command other than **Set** - for example, **IsSet("VIEWERCMD=mark", "Show")** in the viewer to test if the current image is marked.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |                               |                                                                                                                                                                              |
| RemoveFile            | <p>&#x3C;string:path><br>or &#x3C;<a href="path"><strong>Path:</strong></a><strong>path></strong><br><strong>or &#x3C;</strong><a href="item"><strong>Item</strong></a>:item><br>or &#x3C;int:index></p>                                                                                                                                                                                                                                                                                                            | _int_                               | Removes the specified file from the [**Items**](items) collection. You can pass the file's path as either a _string_ or a [**Path**](path) object. You can also pass the [**Item**](item) itself, or its index (starting from 0) within the collection. The return value is the new number of items in the collection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |                               |                                                                                                                                                                              |
| Run                   | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _int_                               | Runs the command that has been built up with this object. The return value indicates whether or not the command ran successfully. Zero indicates the command could not be run or was aborted; any other number indicates the command was run for at least some files. (Note that this is not the "exit code" for external commands. For external commands it only indicates whether or not Opus launched the command. If you need the exit code of an external command, use the WScript.Shell Run or Exec methods to run the command.) You can use the **Results** property to find out more information about the results of the command, and also discover which files (if any) failed using the **failed** property of each [**Item**](item) in the **files** collection.                                                                                                                                                                                                                   |                               |                                                                                                                                                                              |
| RunAsync              | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _int_                               | Asynchronously runs the command that has been built up with this object, without waiting for it to return. The return value indicates whether or not the command was launched successfully, but no further information about the results of the command is available.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |                               |                                                                                                                                                                              |
| RunCommand            | \<string:instruction>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | _int_                               | Runs the single line command given by the _instruction_ argument. Calling this method is the equivalent of adding the single line with the **AddLine** method and then calling the **Run** method.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |                               |                                                                                                                                                                              |
| RunCommandAsync       | \<string:instruction>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | _int_                               | Asynchronously runs the single line command given by the _instruction_ argument, without waiting for it to return. Calling this method is the equivalent of adding the single line with the **AddLine** method and then calling the **RunAsync** method.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |                               |                                                                                                                                                                              |
| SetDest               | \<string:path>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | _none_                              | Sets the command's destination to the specified path. You can provide the path as either a _string_ or a [**Path**](path) object. Calling this method clears the destination tab property from the command.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |                               |                                                                                                                                                                              |
| SetDestTab            | <[**Tab**](tab):tab>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | _none_                              | Sets the command's destination to the specified tab. The destination path will be initialized from the tab automatically (so you don't need to call **SetDest** as well as **SetDestTab**).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |                               |                                                                                                                                                                              |
| SetFiles              | <p><em>object:</em><a href="items"><strong>Items</strong></a><br>or <a href="vector"><em><strong>Vector</strong></em></a><em>:</em><a href="item"><strong>Item</strong></a><br>or <a href="vector"><em><strong>Vector</strong></em></a><em>:</em><a href="path"><strong>Path</strong></a><br>or <a href="vector"><em><strong>Vector</strong></em></a><em>:string</em><br>or <a href="stringset"><em><strong>StringSet</strong></em></a><br>or <a href="unorderedset"><em><strong>UnorderedSet</strong></em></a></p> | _none_                              | <p>Configures the command to use the files in the specified <a href="items"><strong>Items</strong></a> collection as the items the command will act upon.</p><p>You can also pass one of the other collection types, the same as with the <strong>AddFiles</strong> method.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |                               |                                                                                                                                                                              |
| SetModifier           | <p>&#x3C;string:modifier><br>&#x3C;string:value></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                | _none_                              | <p>Turns on a modifier for this command. The supported modifiers are a subset of the full list of <a href="../../command_reference/command_modifier_reference">command modifiers</a>:<br><strong>admin</strong>, <strong>async</strong>, <strong>codepage</strong>, <strong>externalonly</strong>, <strong>leavedoswindowopen</strong>, <strong>nodeselect</strong>, <strong>noexpandenv</strong>, <strong>nofilenamequoting</strong>, <strong>nolocalizefiles</strong>, <strong>noprogress</strong>, <strong>norunbatch</strong>, <strong>resolvelinks</strong>, <strong>runmode</strong>.</p><p>Using this method is the equivalent of using the <strong>AddLine</strong> method to add the modifier to the command as an instruction; e.g. <strong>Command.SetModifier("admin")</strong> is the same as <strong>Command.AddLine("@admin")</strong>. If the modifier requires a value it is passed as the second argument, e.g. <strong>Command.SetModifier("runmode", "hide")</strong>.</p> |                               |                                                                                                                                                                              |
| SetProgress           | <[**Progress**](progress):progress>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | _none_                              | Lets you share the progress indicator from one command with another command. You can pass this method the value of **progress** property obtained from another **Command** object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |                               |                                                                                                                                                                              |
| SetQualifiers         | \<string:qualifiers>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | _none_                              | <p>This method lets you control which qualifier keys the command run by this object will consider to have been pressed when it was invoked. For example, several internal commands change their behavior when certain qualifier keys are held down - calling this method allows you to set which keys they will see.</p><p>The <em>qualifiers</em> argument must consist of one or more of the following strings (comma-separated): <strong>none</strong>, <strong>shift</strong>, <strong>ctrl</strong>, <strong>alt</strong>, <strong>lwin</strong>, <strong>rwin</strong>, <strong>win</strong>.</p>                                                                                                                                                                                                                                                                                                                                                                                        |                               |                                                                                                                                                                              |
| SetSource             | \<string:path>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | _none_                              | Sets the command's source to the specified path. You can provide the path as either a _string_ or a [**Path**](path) object. Calling this method clears the source tab property from the command.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |                               |                                                                                                                                                                              |
| SetSourceTab          | <[**Tab**](tab):tab>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | _none_                              | Sets the command's source to the specified tab. The source path will be initialized from the tab automatically (so you don't need to call **SetSource** as well as **SetSourceTab**).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |                               |                                                                                                                                                                              |
| SetType               | \<string:type>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | _none_                              | Sets the type of function that this command will run. This is equivalent to the drop-down control in the [Advanced Command Editor](../../../customize/creating_your_own_buttons/command_editor/advanced_command_editor). The _type_ argument must be one of the following strings: **std**, **msdos**, **script**, **wsl**. Standard (**std**) is the default if the type is not specifically set.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |                               |                                                                                                                                                                              |
| UpdateToggle          | _none_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | _none_                              | This method can be used to update the appearance of toolbar buttons that use variables to determine their labels or states. For example, a button might use [**@toggle:if**](../../command_reference/command_modifier_reference) to set its selection state based on the existence of a _global-_, _tab-_ or _Lister-scoped_ variable. You would call this method if you have changed such a variable from a script to force buttons that use it to update.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |                               |                                                                                                                                                                              |
