Tips: Managing Scripts

Discuss and share scripts and script files...
Post Reply
TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Tips: Managing Scripts

Post by TheQwerty » 25 Oct 2010 18:04

Just wanted to share a tip for making management of various scripts a little easier... :wink:

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.
Collage.png
Collage of various quick scripting methods.
Collage.png (221.63 KiB) Viewed 185783 times
However, when it comes right down to it, there are really only three ways to deal with scripts...
  1. 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.
  2. 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.
  3. 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.
Those are the three main options, but it doesn't show how you can make it easy to manage all of your scripts and access them everywhere - that key is within User Defined Commands (UDC).

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:
  1. Save your script to a script file.
    1. Copy our script to the clipboard:

      Code: Select all

      $message = "Hello!"; Msg "$message";
    2. 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.
    3. 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.
    4. Name the file "Greeting.xys". Take care that you name the file properly with an extension of "xys" and not "Greeting.xys.txt".
  2. Create a new Load Script File UDC.
    2B-G.png
    Steps 2B through 2G: Creating a UDC for the Script File.
    2B-G.png (36.24 KiB) Viewed 185783 times
    1. From XY's main menu select 'User' -> 'Manage Commands...' to open the Manage UDC dialog.
    2. In the Manage UDC dialog on the left side under 'Category' select 'Load Script File'.
    3. Click 'New' -> 'Add New Command'.
    4. For 'Caption' enter 'Execute Greeting.xys'. Tip: See below for ways to hide this command from the user menu.
    5. Click 'Browse' beside the 'Script File' field, and navigate to/select the 'Greeting.xys' that we just created.
    6. 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.
    7. You now have a UDC that can be referenced by '#1400;' so click OK.
  3. 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;'.
  4. Optional: Keep your script updated.
    1. 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.
Tips:
  • 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.
Last edited by TheQwerty on 26 Oct 2010 16:11, edited 1 time in total.

aurumdigitus
Posts: 1017
Joined: 30 May 2008 21:02
Location: Lake Erie

Re: Tips: Managing Scripts

Post by aurumdigitus » 26 Oct 2010 15:29

A succinct and illustrated tutorial for newbies (and some not so newbies). Bravo!

admin
Site Admin
Posts: 47703
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Tips: Managing Scripts

Post by admin » 26 Oct 2010 15:48

aurumdigitus wrote:A succinct and illustrated tutorial for newbies (and some not so newbies). Bravo!
Indeed! :D

I should add that if you have problems with memorizing numbers you can give "captions" to them using aliases:

Code: Select all

@SayHello=::#1400;
Now you can use @SayHello everywhere where you would use ::#1400;.

PS: Note that aliases will not work within a script though, where #1400; would be a valid line!

TheQwerty
Posts: 4353
Joined: 03 Aug 2007 22:30

Re: Tips: Managing Scripts

Post by TheQwerty » 26 Oct 2010 16:13

Thanks guys, and also those that sent PMs! :D

grindax

Re: Tips: Managing Scripts

Post by grindax » 09 Mar 2012 17:57

.
Last edited by grindax on 22 Jan 2016 22:37, edited 1 time in total.

kodyman
Posts: 185
Joined: 09 Apr 2011 04:05

Re: Tips: Managing Scripts

Post by kodyman » 10 Mar 2012 15:06

Thank you TheQuerty for taking the time to post this. The combination of the steps with the illustrations is really helpful. Especially for those of us who are working to get comfortable with scripts.
I quickly copied this over to the the notebook program I use to store tips & tricks of the various programs I use.

Can I suggest this be made a sticky here or in the scripting section for others to find easily?

admin
Site Admin
Posts: 47703
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: Tips: Managing Scripts

Post by admin » 10 Mar 2012 15:12

kodyman wrote:Can I suggest this be made a sticky here or in the scripting section for others to find easily?
OK. I moved it to the Script Exchange forum and made it sticky.

Post Reply