IDM PowerTips

Using “copied” and “selected” variables for dynamic macros

We’ve been frequently asked how to use ‘variables’ in Macros. The answer is to use the contents of the clipboard and selected text. This isn’t as difficult as may sound. You can use “^c” and “^s”, which represent the clipboard and selected text, to act as variables.

Using these as ‘variables’ can dramatically increase the power and flexibility of the macro when used with other macro commands.

Using the “Copied” and “Selected” Variables for Dynamic Macros

The variables “^c” and “^s” may be used with many macro commands and will be replaced with the contents of the clipboard (^c) and the text currently selected (^s) when used. This allows users to record a macro that may reference a specific string and replace this with one of these two items to allow the string to be dynamically “specified” as the macro is run. For instance, you can use the “^c” variable with a Replace command in the macro (Replace “^c”) which will command UltraEdit to replace the selected text with the content on the clipboard.

There are countless ways of using the clipboard and selected text within macros. For this example, we will be using them to convert a batch of files in a directory from DOS to MAC filetype.

Step 1: Create the Master file

Create a “master” list of all the files in the directory that you want to convert to MAC. You can do this easily by using the “dir” command in DOS, or by using a customized tool.

You will need to format the directory listing to include the full path, filename, and extension of the files. The macro we are creating is going to take each file name and then open the file using the Open command and the contents of the clipboard. If you don’t have the full name/path of the file, Windows won’t be able to open the file.

(Hint: To create a tool which lists the directory contents, read the Power Tip Creating user and project tools.)

This tool, when invoked, yields a directory listing that you can then format with the file paths for your master file.

Note: If your directory list is extensive, you may want to write a separate macro to prepend the file list with the file path instead of doing it manually.

Step 2: Record the Macro

The macro is going to take the file name, copy it to the clipboard, open the file that was copied, perform the conversion, and then close the file.

Record your actions and keystrokes for the macro by navigating to Macro> Record…

When prompted, Give your macro a name and if desired assign it a hotkey, then press “OK.” UltraEdit will now begin recording your keystrokes for the macro.

To begin, you will press CTRL + HOME so you start at the very top of the file. Press SHIFT and then END to select the entire first line. Hit CTRL + X to cut the data, or you can select “Cut” from the Edit menu.

After you have cut the first file path, you will then press the DEL key to bring the next file up to line 1. Deleting the file name is important as you will need it to create a ‘loop’.

Loops and Conditional statements are general programming practices, but are not covered in the scope of this powertip. Further examples are available in the Creating a powerful macro” Power Tip.

Bring up the File> Open panel by pressing CTRL + O. You will now use CTRL + V to paste the file name into “File Name” field in the Open dialog. Once you’ve pasted the file path and name, press “Open” to open the file.

Once you have opened the the first file in the new tab, you can then convert it to MAC (or other) format by going to File> Conversions> DOS to MAC.

Finally, after converting your file, you must close the file to return back to your master list. Now stop the Macro recording by going to Macro> Stop Recording.

Step 3: Edit and Save the Macro

UltraEdit allows you to view and customize all of the steps that were taken during the recording of the macro. You will edit your macro by going to Macro> Edit Macro, which will bring up the Edit/Create Macro dialog.

From here you are able to customize your macro to use the “^c” variable in order to use the content copied to the clipboard for each of the different files in your master list.

The macro needs a way to open each unique file sequentially in order to convert all the files, so you will need to customize the “Open” command within the macro.

Highlight the file path/name following the “Open” command and replacing it with “^c”. When the macro is executed the macro will open whatever has been copied to the clipboard.

Note: If you’d like to keep your master list intact while still using the macro to convert the batch of files, you may do so by making some small changes to the macro commands. Instead of cutting the file path from the list, use the “^s” variable to open the file. With the text still selected, you will use the macro command ‘Open “^s”‘ to open the selected text. You can then process the conversion normally, save and close the converted file, and then return to the master list. Simply command the macro to move down one line (Key DOWN ARROW) and move to the beginning of the second line (Key HOME). This will allow your master list to remain intact.

You may now include a LOOP command within your macro to repeat the cut (or selection) for each filepath, or line, so the macro runs down the entire list of files. (Hint: For a detailed explanation of how to do this, please view our power tip “Creating a powerful macro.)

Our final macro included the following commands:

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
Loop
IfEof
ExitMacro
Else
StartSelect
Key END
Cut
Key DEL
Open “^c”
DosToMac
Save
CloseFile
EndLoop

The macro basically opens each file from the list and performs the conversion. When it hits the End of File (Eof), it will stop. Note, when you run this macro, make sure that the Master File list is the ONLY file open in the edit window.

Please note, it is always a good idea to first test the macro on sample data, to ensure it is performing as you intend it to.

Congratulations! You now know how to manipulate data with a macro using the dynamic and powerful “^c” and “^s” variable functionality within UltraEdit.