XYplorer scripting, introduced with version 7.00, offers the ultimate in file management efficiency. Roll your own custom commands, combine them into scripts, wrap them into an XY Script file (XYS) or a User-Defined Command, and execute them with a single click or keystroke. Can't get better than that? Sure it can! Share scripts with colleagues or download them from the web: Just drop a script file into your application folder and you have fresh plug-in commands at your fingertips. |
Without a doubt, scripting is an advanced feature that only pays off if you take the time to dive in and explore the possibilities. However, you'll find that it's not rocket science at all.
Before we go any further, here are some of the truly wonderful things that scripting can do for you:
To get you started, let's do some simple warm-ups:
Select menu Scripting | Try Script...
Paste msg "Hello world!"; into the edit box.
Click OK.
You should now see a "Hello world!" message box. Congratulations, you have just written your first XYplorer script! | |
Okay, now for something a little bit more interesting: Try msg "%temp%";. You should see a message box that displays your TEMP path. %temp% is a standard Windows environment variable.
Try msg "XYplorer.exe runs from <xypath>";. <xypath> is a native XYplorer variable that resolves to XYplorer's application path.
Try msg "Press OK to copy the time!"; copytext "<date hh:nn:ss>";. When the message box is OK'd, the second command copytext is immediately executed and the current time is copied to the clipboard.
Try $a = "<curpath>"; msg $a;. You should see a message box with the current path. First, the $a variable is set to the current path, then it is used in the msg command.
Try $a = "Year " . "<date yyyy>"; msg $a;. You should see a message box that says "Year 2024" (or whatever the year is when you try this). First, the $a variable is set to two strings, a literal and a variable, concatenated by a period, and then used in the msg command.
What you have seen so far of XY scripting is: There are commands consisting of a function name like msg or copytext and arguments which can contain literals like "Hello!" and variables like %temp% (environment), <curpath> (XYplorer) or $a (user defined).
An argument may have more than one part. The parts are concatenated by periods (.).
A command can take more than one argument. The arguments are separated by commas ( , ).
A script may have more than one command. Commands are separated by semicolons ( ; ).
|
Of course, the Try Script... dialog is only for playing and debugging. For everyday use, we need something better. | |
Let's start with Quick Scripting: Type ::msg "Hello world!"; in the address bar and press Enter. You should see a "Hello world!" message box.
|
As you can see, Quick Scripting means Scripts, when prefixed with "::", can be executed directly (i.e. quickly) through any interface in XYplorer that can process locations: Address Bar, Go To, Favorites, Catalog, etc. This gives you a lot of options for using and storing scripts.
For mouse users, the catalog is a very good place for scripts. Using the "::" prefix, scripts can simply be entered in the location field of a catalog element. They are executed with a single click. For example: Create a new category "Scripts" and add a new item to it.
Set the item's Location field to ::#1026 (screenshot) and save it. The icon changes to the script icon.
Now click on the item. The Find Files tab of the Info Panel will open, all search filters will be reset and the cursor will be placed in the Name field. Nice!
| |
But how did this work? And what is #1026? In XYplorer, almost every function has a fixed number, the function ID or command ID, by which it can be referred to in a script. ID #1026 happens to refer to "Miscellaneous / Find Files / Open Find Files and Reset". Open the Customize Keyboard Shortcuts dialog (Tools menu) and find this function in the Miscellaneous category. At the bottom of the dialog you'll see a button with the function's ID (screenshot). Clicking this button copies the function ID to the clipboard for easy use in a script. Here's another simple but useful script. You could place it in the Favorite Folders menu, for example, by adding it from the List Management dialog (screenshot). ::rename "b", "*-<datem yyyymmdd>"
This little script will rename all currently selected items by appending the current date.
Of course, longer scripts can also fit into a catalog item or other location (screenshot): ::goto "Desktop|"; sortby "m", "d"; sel 1; focus "List";
This script goes to the Desktop folder (making sure the list is unfiltered), sorts the list by date modified (descending), selects the first item, and moves the focus to the list.
|
For passionate keyboarders, there are UDCs (User-Defined Commands). Using the "::" prefix, scripts can simply be entered into the Location field of a UDC Go To item (screenshot), to which a keyboard shortcut can then be assigned.
Another example:
Now press Ctrl+Alt+3. You should see a small window displaying the current text content of the clipboard.
Of course, you don't have to abuse the Go To UDC for scripts. There's also the UDC Run Script, which accepts scripts without a "::" in front, e.g. text "<clipboard>";.
Run Script can also handle more advanced stuff like multi-line scripts, and multi-scripts, the rules and possibilities of which will be further explained below under "XYplorer Script Files". Here's just an appetizing preview:
goto "Desktop|"; // sort by modified date, descending sortby "m", "d"; // select first sel 1; focus "List";
What you have learned in this section about XY scripting is:
Suppose you have an older script #230; and you forgot what #230 refers to. What do you do now? Simple, go into step mode:
A dialog will pop up telling you what is about to happen (screenshot). You can now decide whether to execute the command, skip it, or abort the entire script. In step mode you are on the safe side. It is highly recommended when writing or debugging scripts!
There's also a toolbar button for toggling the stepping mode. When pressed (stepping is ON) its color changes to red to make the current state very clear.
We are talking about popup menus that are user-defined by a text file. Let's make one:
// some little test scripts "Go to C:\" goto "C:\" "Go to System Folder" goto "%winsysdir%" "Go to XYplorer Folder" goto "<xypath>"
A script file is basically a library of scripts. It is nothing more than a simple text file that can contain one or more scripts. You can either call one of these scripts directly or just load the whole file. In the latter case, XY will create a menu based on the scripts contained in this file and pop it up, allowing you to choose which script to execute.
The syntax for the scripts themselves inside script files is exactly the same as for scripts anywhere else in XY, since this is just another way to store and run scripts.
To load a script file, do one of the following
Of course, the existence of a load command means that one script file can load another. You should be getting an idea of the potential of scripting by now...
You can use labels to execute a script directly inside a file, bypassing the pop-up menu. The label is appended to the caption, separated by " : " (space-colon-space). For example
// some little test scripts, using labels "Go to C:\ : croot" goto "C:\" "Go to System Folder : system" goto "%winsysdir%" "Go to XYplorer Folder : xy" goto "<xypath>"
If the above is saved in a file named "test.xys" in the application data path, the following command will take you directly to the System folder: load "test.xys","system".
Hidden scripts can be executed, but are not displayed in the popup menu of the script file. To hide a script, simply prefix the caption or label with an underscore (a hidden script does not need a caption anyway).
For example, create a script file "date.xys" in the application data path with the following content:
// this is in script file "date.xys" "_date" msg "<date yyyy-mm-dd>" "_time" msg "<date hh:nn:ss>" "Show Date : date" sub _date; "Show Date && Time : datetime" sub _date; sub _time
Now run the script load "date.xys". The popup menu will only show two of the four scripts it contains. Select one of them and see what happens. |
|
Now run the script load "date.xys","date". The script named "date" is executed directly. It has only one command: sub "_date";. The sub command is a special command to invoke a script within the same script file. In this case, the hidden script named "_date" is called and executed. Its command msg "<date yyyy-mm-dd>" produces the message box with the current date. |
As you progress in writing scripts, you will soon feel the need for variables. XYplorer allows you to define and use as many variables as you want using a number of commands like set, input, replace, etc. The script $a = "Hi!"; msg $a; will define a new variable $a and assign the string "Hi!" (without the quotes) to it; then a message box will display "Hi!".
Variables are resolved wherever they occur in the arguments of all subsequent commands in the script, even if they occur within quoted strings; for example
$name = "Ted"; msg "Hi, I'm uncle $name!";
will display the message "Hi, I'm uncle Ted!".
Variables must conform to the following rules:
Variable names are case-sensitive. The scope and lifetime of a variable begins and ends with the script in which it is defined.
Sample output:
FindTemplates | [DIR] | 2008-10-15 08:17:33 |
NewItems | [DIR] | 2008-10-15 08:17:33 |
Scripts | [DIR] | 2008-10-15 08:17:33 |
catalog.dat | 4.612 bytes | 2008-11-04 10:33:34 |
CatalogDefault.dat | 4.612 bytes | 2008-10-15 11:00:00 |
fvs.dat | 92 bytes | 2008-11-04 10:33:34 |
ks.dat | 8.224 bytes | 2008-11-04 10:33:34 |
LicenseXY.txt | 4.120 bytes | 2008-10-15 11:00:00 |
ReadmeXY.txt | 5.535 bytes | 2008-10-15 11:00:00 |
TipOfTheDay.htm | 20.120 bytes | 2008-10-16 07:51:58 |
udc.dat | 36 bytes | 2008-11-04 10:33:34 |
Uninstall.exe | 69.872 bytes | 2008-11-04 10:32:53 |
XYplorer.cnt | 1.244 bytes | 2008-10-15 11:00:00 |
XYplorer.exe | 3.096.576 bytes | 2008-11-04 10:32:18 |
XYplorer.hlp | 293.040 bytes | 2008-10-15 11:00:00 |
XYplorer.ini | 25.038 bytes | 2008-11-04 10:33:34 |
XYplorer.url | 49 bytes | 2008-11-04 10:32:53 |
For a complete reference of all scripting commands, refer to the Help file.
Here is a place where you can find free scripts to use and learn: