XYplorer is extremely customizable and powerful, but sometimes that comes at a cost. In the case of calling scripts the user has a number of options which sometimes make it difficult for a beginner to know where scripts should go. However, when it comes right down to it, there are really only three ways to deal with scripts...
- Quick Scripting
- As seen in: Catalog Items; Favorites; Aliases; Portable File Associations/Open with Menu.
A quick script is one that is typically a one liner and can be called from any place that XYplorer accepts locations for navigation. This is the default type of script for the above features, which all default to a single line edit field.
- As seen in: Catalog Items; Favorites; Aliases; Portable File Associations/Open with Menu.
- Normal Script(s)
- As seen in: Catalog Items; Custom Toolbar Buttons; and User Commands -> Run Script
While all of these places have a single line edit field for quick scripts they also have an Edit button which allows you to more easily work with multi-line scripts.
These are scripts that typically become a little too cumbersome for a single line, or are a collection of scripts that need to be stored/called together.
- As seen in: Catalog Items; Custom Toolbar Buttons; and User Commands -> Run Script
- Script Files
- As seen in: User Commands -> Load Script File
In these cases the script is stored in a file, typically within XYplorer's script folder (Scripting -> Go to Scripts Folder), and XY reads the file to execute the script.
Storing scripts in a file also allows you to use "::Load('ScriptFile');" as a quick script, which gives you a lot more flexibility.
- As seen in: User Commands -> Load Script File
When you create a new UDC XY automatically assigns it an ID number starting from #1400, this is the same as all the other command IDs shown in the Customize Shortcut Keys dialog, in that it is also a scripting command '#1400;'. What this means is you can use UDCs to manage your scripts and then access them anywhere via their command IDs, and if you want to change/move the script you only have to do it in one place.
Let's walk through how you would do this using a script file for an (overly) simple script...
Steps:
- Save your script to a script file.
- Copy our script to the clipboard:
Code: Select all
$message = "Hello!"; Msg "$message";
- From XY's main menu select 'Scripting' -> 'Go to Scripts Folder'. This will navigate to XY's current script folder, which is the first place it looks when attempting to load a script file.
- From XY's main menu select 'Edit' -> 'Paste Special' -> 'Paste Text Into New File'. This will create a new file with the copied script as its contents.
- Name the file "Greeting.xys". Take care that you name the file properly with an extension of "xys" and not "Greeting.xys.txt".
- Copy our script to the clipboard:
- Create a new Load Script File UDC.
- From XY's main menu select 'User' -> 'Manage Commands...' to open the Manage UDC dialog.
- In the Manage UDC dialog on the left side under 'Category' select 'Load Script File'.
- Click 'New' -> 'Add New Command'.
- For 'Caption' enter 'Execute Greeting.xys'. Tip: See below for ways to hide this command from the user menu.
- Click 'Browse' beside the 'Script File' field, and navigate to/select the 'Greeting.xys' that we just created.
- As you can see there is a button on the right that shows '#1400' (or similar). This is the command ID that XY has assigned to this UDC. If you click the button it will be copied to the clipboard, so click it.
- You now have a UDC that can be referenced by '#1400;' so click OK.
- Reference the UDC where you want the script.
- Now anywhere you want to call that script (another script, a custom toolbar button) you can use '#1400;'.
- As a quick script in the address bar, alias, catalog, favorites you can use '::#1400;'.
- As a Portable File Association/Open with Menu: '"Say Hello" *>::#1400;'.
- Optional: Keep your script updated.
- Now if you want to change your script you only have to edit the script file and everywhere that uses the command ID will use the new version.
- The UDC's Label field allows you to call a specific Script in the file by label, which can be useful if your file has a specific entry point.
- Since we saved the script to XY's script folder, we can also just enter 'Greeting' in step 2E (as pictured); XY will automatically search the script folder for 'Greeting.xys'. XY will also resolve paths entered here relative to the Script folder, which is extremely beneficial when using XY portably.
- Sometimes you may want to manage a script but do not want to unnecessarily clutter up your User Menu, in that case set the 'HideUnderscoredUDCs' INI tweak to 1 and you will be able to prefix the caption with an underscore ('_Execute Greeting.xys'), telling XY to hide it from the menu, but maintaining the command ID and ability to customize its shortcut.
- It may be helpful to add custom icons to your script (and other) UDCs. You can do this by suffixing the UDC caption with '|' followed by the path to an icon (or internal icon name), such as 'Execute Greeting.xys|C:\Icons\Hello.ico' or 'Execute Greeting.xys|<xyscripts>\Greeting.ico'. (From SkyFrontier)
- Many (but not all) of the places you may want to reference your script accept aliases, so if you would rather refer to the script by name than by number you can create an alias by entering '@SayHello=::#1400;' into the address bar. Now in those places you can just use '@SayHello'. (From admin)
- The method I have described is not any different than calling 'Load("Greeting");' in all the places you might want to use the script, so why bother with a UDC? Indeed, there isn't much advantage, but if you ever want to rename/move the script file itself you only have to do it within the UDC as opposed to in every Load().
Please note that this is based on XYplorer v9.60.0106, but it applies to many previous and (hopefully) future versions.
EDIT: Clarity
2013-10-28: Corrected path to Go to Scripts Folder.
2013-10-28: Added link to INI tweak directions.
2010-10-26: Added some tips.