If you want to ask something or just give some feedback, you can use the Thanks to Stefan for the Scripting How-to - >thread<
- - -
This is an new thread with stuff from the collection thread How did you start scripting?, but with better layout i hope.
Tip: there is an another scripting tutorial made by user TheQwerty >here<
An good start to learn XYplorer in general is to take the official >Tour<
Here is an "Overview of existing scripts" > click
Let's start:
How to execute a script ?
An warning first:
Please note that you should test new scripts always with an backup of your real files, just to take care.
But since an script can switch to an other path and delete for example files,
you should execute only scripts you are sure what they do exactly.
Attention: if an script have no error-handling to prevent users from mistakes,
you should use an script only for the exact purpose it was indented, other-wise the script could make unexpected actions.
If an script does nothing for you you maybe have to select an file first?
Best test new scripts always via "Scripting > Try Script.." first, to learn what it do.
An example one-liner script would just be
Code: Select all
openwith Notepad, (m);
Or
Code: Select all
text "FILE: <curname><crlf>BASE: <curbase><crlf>EXT: <curext><crlf>FOLDER: <curfolder><crlf>DATE: <date>";
An "Multi-line" Script could be for example:
Code: Select all
$var_file = "<curname>";
$var_User = "%USERNAME%";
$var_DATE = "<date>";
msg " $var_User has selected <crlf 2>$var_file<crlf 2>at $var_DATE";
Code: Select all
"Multi-Script: Script 1"
echo "Hello";
"Multi-Script: Script Two"
text "Hello World";
"Multi-Script: Script number 3"
msg "Hi %USERNAME% on %COMPUTERNAME%";
Code: Select all
"Multi-Script: Script 1"
$var = input("What's your name?");
msg "Hello $var ( or %USERNAME% ?)";
msg "Hello World";
msg "Finshed";
"Multi-Script: Script Two"
$var_file = "<curname>";
$var_User = "%USERNAME%";
$var_DATE = "<date>";
msg " $var_User has selected <crlf 2>$var_file<crlf 2>at $var_DATE";
For more read the help > Advanced Topics > Scripting
Here is an short collection of possibilities to execute an XYplorer script file (xys)
There are two main ways:
A: execute an script on the fly without saving it first to an file.
B: first save the script to an file and then execute that script by using command "load"
Quick Explanation:
If you find an script in the forum the easiest way to use this
is to utilize "Scripting > Try Script.." which enables the debugger and you see step-for-step what happens.
Use "Scripting > Run Script.." if you know what you do because here is no debugger involved. The script just runs.
"One-liner" scripts can be just pasted into the address bar (maybe with leading '::')
::openwith Notepad, (m);
If you want to keep an script for ever i would find it handy to paste the script to an plain text file and then "load" it.
With a script in a separate file you can "load" it from everywhere but you have to modify it at an single place only.
Also it is easier to backup and exchange scripts if saved to a text file.
For example a text file "\Data\Scripts\OpenWithNotepad.xys" which contains the script can be
execute by utilizing "load OpenWithNotepad;" (no path needed for that folder, no extension needed).
(Tip: to find your "\Data\" folder utilize "Go" menu and "Go to Application Data Folder")
See below for more explanations.
A
A) How to execute a script without saving the code to an file first
To execute an script without creating an script file first you have for example this seven possibilities.
Chose one of this:
Code: Select all
1.) Use XYplorer menu "Scripting > Try Script.."
Paste or type in the script
Example:
msg "<xypath>";
and press OK.
"Try Script" always starts with stepping enabled.
Note that the debug window appears; try an right click on the [Continue] button and spot the additional options.
There is also the command "Scripting > Run Script..". which executes an script without calling the debugger.
Note that you can reuse this script till you overwrite it. XYplorer even remember it after an reboot.
Tip: you can disable code with
/*....*/ block comment
and // line comment signs.
Code: Select all
2.) Use the XYplorer address bar for an one-liner script with "Quick scripting"
- "Quick scripting" (Extended Scripting)
Enable entering scripts through location interfaces (Address Bar, Go To, Catalog, Favorites).
Such scripts must be additionally prefixed with a double-colon (::) to distinguish them from normal locations.
Note that that script is stored from XYplorer internally and you can reuse it e.g. from the address bar history.
Examples:
::msg "Hello World!";
::$temp = "<curitem>"; msg $temp; $new = replace($temp, "\", "_"); msg $new;
Only true for older XYplorer versions:
Note that you have to enable "Quick scripting" via "Tools > Configuration... > Advanced" (F9 key)
Code: Select all
3.) Use User-Defined Commands (UDC) for an one-liner script
UDC are user defined menu commands, you may also assign keyboard shortcuts to them.
How-to:
XYplorer menu "User > Manage Commands..."
"Run Script" section
New... > Add New Command
Caption: [ just an short description]
Script: [ add your code here;]
Note that that script is saved from XYplorer internally for later reuse.
Tip: click the [Assign Keyboard Shortcut] button and choose a Keyboard Shortcut to your new UDC.
See examples: http://www.xyplorer.com/xyfc/viewtopic.php?f=3&t=3146
Code: Select all
4.) Use Custom Toolbar Buttons (CTB) for an one-liner script
How-to:
XYplorer menu "Tools > Customize Toolbar..."
Add an "User Button #1" and click [OK]
Right click this new User-Button and customize it:
- Name: optional, this is not really needed
- Icon: optional, this is not really needed
- On click: [ add your code here;]
- On right-click: [ optional script for right clicking this CTB]
Note that that script is saved from XYplorer internally for later reuse.
Tip:
To assign a Keyboard Shortcut to an button, utilize 'User-Defined Commands' (see above)
and use command "button ctb23;" as script (if you work on CTB number "23").
http://www.xyplorer.com/release_8.40.htm
http://www.xyplorer.com/release_8.50.htm
See an example: http://www.xyplorer.com/xyfc/viewtopic.php?p=40908
Code: Select all
5.) Use Portable Openwith Menu (POM) for an one-liner script
XYplorer menu "Tools > Customize Files Associations... > New"
Example:
|"My Description" \;*>your code here;"
Later The Portable Openwith Menu (POM) is opened via menu "File | Open With... (Ctrl+Alt+Enter)".
Note that that script is saved from XYplorer internally for later reuse.
Read more about here: http://www.xyplorer.com/tour/index.php?page=pom
Examples: http://www.xyplorer.com/xyfc/viewtopic.php?p=28660
Examples: http://www.xyplorer.com/xyfc/viewtopic.php?p=42122
Code: Select all
6.) Use Custom Keyboard Shortcuts (CKS) for an one-liner script
How-to:
Define an "User-Defined Command" (UDC, point 3. above) and add an keyboard shortcut
Code: Select all
7.) Use Custom Event Actions (CEA) for an one-liner script
E.g. from an custom right click context menu:
Sorry, CEA are not implemented right now.
B
B.) How to execute an script by loading it from an file
Code: Select all
First save the script to an plain text file, best (for easy access) in XYplorers Scripts folder.
How-to:
- select and copy the whole script to the clipboard
- in XYplorer go to the script folder (using menu "Go > Go to Scripts Folder")
- right click there on the number column or on an empty space below of any file
and chose "Paste Special > Paste Text Into New File" from context menu.
Give that file an nice name and use XYS as extension.
E.g.: "X:\Tools\XYplorer\Scripts\scriptname.xys"
Use one of this:
Code: Select all
1.) XYplorer menu "Scripting > Load Script File..."
to load an saved script file from XYplorers script folder (or browse to your script)
Code: Select all
2.) Use the XYplorer address bar "Quick scripting" -feature to load an saved script file
Example:
::load "scriptname";
Note that the extension ".xys" is not needed to include here.
Note that the script path for scripts in "XYplorer\Scripts folder" is not needed here.
For script located other where you can use absolute or relative paths, like:
"::load "C:\Temp\myScript"; (to load an script from "C:\Temp\")
"::load "tests\test01"; (if you have an sub-folder "tests" in your XYplorer script folder)
Info:
- "Quick scripting" (Extended Scripting)
Enable entering scripts through location interfaces (Address Bar, Go To, Catalog, Favorites).
Such scripts must be additionally prefixed with a double-colon (::) to distinguish them from normal locations.
Code: Select all
3.) Use User-Defined Commands (UDC) to load an saved script file
How-to:
XYplorer menu "User > Manage Commands..."
"Load Script File" section
New... > Add New Command
Caption: [ just an short description]
Script File: [ myScript or path\to\script]
Label: [ optional: label inside the script file ]
Tip: click the [Assign Keyboard Shortcut] button and choose a Keyboard Shortcut to your new UDC.
See examples: http://www.xyplorer.com/xyfc/viewtopic.php?f=3&t=3146
Code: Select all
4.) Use Custom Toolbar Buttons (CTB) to load an saved script file
How-to:
XYplorer menu "Tools > Customize Toolbar..."
Add an "User Button #1" and click [OK]
Right click this new User-Button and customize it:
- Name: optional, this is not really needed
- Icon: optional, this is not really needed
- On click: [ myScript; or path\to\script;]
- On right-click: [ optional script for right clicking this CTB]
Note that that script is saved from XYplorer internally for later reuse.
Tip:
To assign a Keyboard Shortcut to an button, utilize 'User-Defined Commands' (see above)
and use command "button ctb23;" as script (if you work on CTB number "23").
http://www.xyplorer.com/release_8.40.htm
http://www.xyplorer.com/release_8.50.htm
See an example: http://www.xyplorer.com/xyfc/viewtopic.php?p=40908
or: http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=653
Code: Select all
5.) Use Portable Openwith Menu (POM) to load an saved script file
XYplorer menu "Tools > Customize Files Associations... > New"
Example:
|"My Description" \;*>::load scriptname;
Explanation:
An leading '|' -symbol means "don't act/execute on double click or enter, just show it in POM"
"*;\" means to which file types this POM should be associated. Here to all file types "*" and to all folders "\".
But you can chose single extensions also: txt;log;pas;cpp;html>::load textparserscript;
Later The Portable Openwith Menu (POM) is opened via menu "File | Open With... (Ctrl+Alt+Enter)".
Examples: http://www.xyplorer.com/xyfc/viewtopic.php?p=39914
Note that POM are an extension of "Portable File Associations" (PFA)
Read more about PFA at http://www.xyplorer.com/tour/index.php?page=portable
Code: Select all
6.) Use Custom Keyboard Shortcuts (CKS) to load an saved script file
Define an "User-Defined Command" (UDC, point 3. above) and add an keyboard shortcut
Code: Select all
7.) Use Custom Event Actions (CEA) to load an saved script file
E.g. from an custom right click context menu:
Sorry, CEA are not implemented right now.
Code: Select all
Hint about Script warning about Recursion:
Example:
the "Scripting Error" ('Step through scripts') box appears:
The current script appears to be recursive.
The stack depth is 3.
Regarding the recursion-warning, you need to turn the recursion checker off if an script uses recursion on purpose:
Switch Recursion Checker Off
When writing your scripts, you might want to have one script call itself (using command load or sub).
Of course, if you don't take care, you could end up in an infinite loop which would result in some problems...
Trying to prevent such an issue, XY comes with a recursion checker that will pop up a warning message
when a recursion is spotted and gives you the opportunity to cancel the script execution.
If you do not want these warnings and are confident that you won't create an endless loop,
you can switch off the recursion checker by setting this entry to a value of 1 :
[Settings]
ScriptRecursionWarningOff=0
You can also use this setting per script.
Example:
Setting("AllowRecursion", 1);
Explanation:
+ Scripting commands enhanced:
- setting, settingp
New named argument "allowrecursion"
0 = show warning on recursions
1 = simply go on and hope that the script writer knows what he's doing
Example:
::setting "allowrecursion", 1;
Note: This command is just using a simpler name for the already
existing INI tweak "ScriptRecursionWarningOff".
; Tweak: set to 1 to turn off the recursion checker
ScriptRecursionWarningOff=1
Code: Select all
Hint about path to script file and script extension .xys
If the script file is located in the XYplorer script folder "\Scripts" (see menu "Go > Go to scripts folder")
then you don't need to include the path to execute the script.
Otherwise call the script with an path, full or relative.
You can also use the var "<xyscripts>" to refer to XYplorers script folder. (load "<xyscripts>\sub-folder\testscript")
Note that you don't need to include the ".xys" extension while executing an script.
Code: Select all
Hint about debug window:
Enable "Scripting > Step Mode" to see the debug window for every script execution.
Note: do an right click on the [Continue] button to see some options.
You can also call the debugger from an script.
Example:
step;
Code: Select all
Hint about " " quotes:
If something doesn't works please try to add or remove quotes from commands.
Because here for this examples ""-quotes are used often for explanations to show you an start and end point,
and should NOT be used in real life. On the other hand quotes ARE needed if your commands, names or paths contain spaces.
You may want to read section about Quotes too?
Find me:
How to launch a script? , Howto start a script? , How-to run an script? , execute a script file , use a script , run this script