Page 1 of 1

SelScript - Script to Select a Script to run

Posted: 29 Nov 2011 00:19
by PeterH
Edit: Current version is V0.2 - see far below!

Wanting a bit of practise I "played" with an old codefragment from Stefan - and happened to create something that is quite helpful for me. Maybe it can help you, too?
(And after having had several wishes, problems and notes regarding scripting I thought I should show that I use parts of them...)

The XY script folder fills up with a lot of scripts. Some oftem used, some seldom. Some just for some tests, some in "creation status", some "high productive". And all mixed up...
And if you need one of your scripts, you have to 'search' through all this. (OK, there are alternatives, but...)

- One idea to help: move scripts for test and alike to a subfolder of <xyscripts>. But: it must be easy to call them...
- Both Main- and Sub-Folder *can* possibly contain scripts with the same name. (So you can create a test-version of a working script.)
- Another idea: while the list of scripts for selection should automatically be built from all existing scripts, "prefered" scripts should be sorted together "at the top" for easy finding.
- Special: when the selected script is started, the state of XY must be the same as before - especially selections: be it pane, tabs or files. Just as if the script would have been directly called.


The script needs a bit of customization: you find it rather on top, in the first part called User-CUSTOMIZATION:

- between $prefer0 = <<<>>> and >>> a list of "Prefered" Scripts in <xyscripts>, one per line, left aligned. See samples in script.
- $subf1 specifies the name of a subfolder of <xyscripts> for further scripts, without leading or trailing "\". If '' no subfolder will be searched. 'Test' means: <xyscripts>\Test
- between $prefer1 = <<<>>> and >>> a list of "Prefered" Scripts in subfolder of <xyscripts>, one per line, left aligned. See samples in script.
- separatp and separats specify 2 dividers in the selection menu. Leave as is or change as wanted.

And now to the script! I've tested it as good as possible, but you know...

If you are interested and have questions, have ideas, or find errors - just give me a note!

Code: Select all

//      SelScript V0.1
// Script to Show *all* ScriptFiles from XY-ScriptFolder...
//          ... PLUS from a Subfolder of XY-ScriptFolder (for TestScripts etc.),
// shown  with pre-defined "Prefered" Scripts sorted at the Top,
// letting the User Select which to run.
//
// Doesn't change anything in state of XY before Script-Execution,
//          neither to Tabs, Selections, or whatever
//
// Created by PeterH

// =================================================================================================
//  User-CUSTOMIZATION: Define "Prefered" Scripts (sorted on top of lists):
// ------------------------------------------------------------------------
   // Prefered Scripts in Main Script Folder:
   $prefer0    = <<<>>>
Rename GPX.xys
SelScript.xys
>>>;
// <== EndOfList. Add as many names as wanted in front of it, one per line.

   $subf1      = 'Test';                        // Sub-Path to Sub-ScriptFolder. ''=none 
   // Prefered Scripts in Sub-Script Folder:
   $prefer1    = <<<>>>
TestScripts.xys
>>>;
// <== EndOfList. Add as many names as wanted in front of it, one per line.

   $separatp   = '>'.StrRepeat('-', 40);        // SeparatorLine betw. "Prefered" & Rest
   $separats   = '>=====> TEST Scripts >=====>';// SeparatorLine betw. Main- & Sub-Folder

//  END of User-CUSTOMIZATION
// =================================================================================================

   Global $list, $prefer, $subf;                // for communication with subrtn
// -------------------------------------------------------------------------------------------------
// Start work, in MAIN ScriptFolder
   $list       = '';
   If ($prefer0 != '')                             // If Prefered are specified: 
   {  $list    = "$prefer0<crlf>$separatp"; }      // Start of Output for InputSelect
   $prefer     = $prefer0;                         // set LowerCase for UniqueCompare
   $subf       = '';                               // no subfolder: Main ScriptFolder
// List Existent Scripts, Sort out "Prefered", gather others
   Sub "_BuildList";     // Globals: $listfold, $prefer, $subf, $list
// $list = _BuildList($list, $prefer0, '');        // Build List for MAIN ScriptFile

// -------------------------------------------------------------------------------------------------
// ...and then the SUB-Folder
   If ($subf1 != '')                               // Only If a Subfolder is specified: handle it
   {  $list    = "$list<crlf>$separats";              // add SeparatorLine "Sub-Folder"
      If ($prefer1 != '')                             // If Prefered Sub- are specified: 
      {  ForEach ($entry, $prefer1, <crlf>) {$list = "$list<crlf>$subf1\$entry";}
         $list = "$list<crlf>$separatp";                 // add SepLine Prefered
      }
      $prefer  = $prefer1;                            // make LowerCase for UniqueCompare
      $subf    = "$subf1";                            // subfolder of Main Scriptfolder
// List Existent Scripts, Sort out "Prefered", gather others
      Sub "_BuildList";     // Globals: $listfold, $prefer, $subf, $list
//    $list = _BuildList($list, $prefer1, $subf1);    // Build List for SUB ScriptFile
   }
// -------------------------------------------------------------------------------------------------
// Let User Select a Script:
   $script  = InputSelect('Select a Script to Execute:', $list, <crlf>, , , 300, 500, 'SelScript');
// ...and execute it (if not '' or a separatorline selected):
   If ($script != '' && $script != $separatp && $script != $separats) { load $script; }

 End 1; // T H A T ' S   I T   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// =================================================================================================
// Subroutine(s))
// =================================================================================================
"_BuildList"    // Build combined list from Prefers & ListFolder
   Global $list, $prefer, $subf;                // for communication with subrtn

   $preferl    = ReCase($prefer, 'lower');         // make LowerCase for UniqueCompare

// List in Sub-ScriptFolder all *.xys (Files-only, without pathes)
   $listfold   = ListFolder("<xyscripts>".(($subf=='') ? '' : "\$subf"), "*.xys", 5);

   ForEach ($entry, $listfold)                     // For Each Entry in found Scripts...
   {  $entryl  = ReCase($entry, 'lower');             // LowerCase for Compare
      ForEach ($preferentry, $preferl, <crlf>)        // (already LowerCase)
      { If ($entryl == $preferentry)                     // Entry already in "Prefered"
         { $entry = ''; Break; }                            // => ignore it, Break for next
      }
      If ($entry != '') { $list = ($subf=='') ? "$list<crlf>$entry" : "$list<crlf>$subf\$entry"; }
   }

Re: SelScript - Script to Select a Script to run

Posted: 29 Nov 2011 02:05
by highend
A blacklist would be nice.

E.g.: I tend to store
XYplorerAutomaticUpdater.xys and
common-jacky.xys

in my script folder and don't want to see those scripts in such a dropdown list at all.

Re: SelScript - Script to Select a Script to run

Posted: 29 Nov 2011 10:04
by PeterH
highend wrote:A blacklist would be nice.

E.g.: I tend to store
XYplorerAutomaticUpdater.xys and
common-jacky.xys

in my script folder and don't want to see those scripts in such a dropdown list at all.
I think from my way of working I wouldn't like it.

But technically it should be very easy to integrate - most of the necessary logic is already in use. So I'll think about it. Give me a bit of time.
Who doesn't want it just leaves the list empty, so no harm...

Re: SelScript - Script to Select a Script to run

Posted: 29 Nov 2011 13:40
by PeterH
OK: new Version 0.2

The lists of Prefered Scripts now can contain a sublist of Scripts that are to be suppressed from display. The beginning of this Sublist is indicated by an entry >>>>>SUPPRESS>>>>> - see the sample.
You may define no entry after >>>>>SUPPRESS>>>>>,
and in that case you even may delete this line.

Some parts of the code got restructured: better adapted to a not (yet?) available functionality "function-call" in XY-scripts. So I had to simulate this. But I hate multiple coding the same code...

Again: it's tested, but can have bugs.
I'd expect: in this script bugs could lead to errors in functionality, but not destroy anything else.

Code: Select all

// Script to Show *all* ScriptFiles from XY-ScriptFolder...
//          ... PLUS from a Subfolder of XY-ScriptFolder (for TestScripts etc.),
// shown  with pre-defined "Prefered" Scripts sorted at the Top
// ...and the ability to Suppress the display of unwanted scripts,
// then letting the User Select which to run.
//
// Doesn't change anything in state of XY before Script-Execution,
//          neither to Tabs, Selections, or whatever
//
// Created by PeterH

   $vers       =   V0.2;
//       Selscript V0.2 chg: Add Ability to SUPPRESS selected Scripts
//       SelScript V0.1

   Global $suppind, $separatp, $separats;       // some Global Constants
// =================================================================================================
//  User-CUSTOMIZATION: Define "Prefered" Scripts (sorted on top of lists):
// ------------------------------------------------------------------------
   // Prefered Scripts in Main Script Folder. Files after >>>>>SUPPRESS>>>>> will be Suppressed:
   $prefer0    = <<<!!!
SelScript.xys
Rename GPX.xys
>>>>>SUPPRESS>>>>>
SelScript S01.xys
selscript s02.xyS
!!!;
// <== EndOfList. Add as many names as wanted in front of it, one per line.

   $subf1      = 'Test';                        // Sub-Path to Sub-ScriptFolder. ''=none 
   // Prefered Scripts in Sub-Script Folder. Files after >>>>>SUPPRESS>>>>> will be Suppressed:
   $prefer1    = <<<!!!
TestScripts.xys
>>>>>SUPPRESS>>>>>
ShowShellProperties.xyS
!!!;
// <== EndOfList. Add as many names as wanted in front of it, one per line.

   $separatp   = '>'.StrRepeat('-', 40);        // SeparatorLine betw. "Prefered" & Rest
   $separats   = '>=====> TEST Scripts >=====>';// SeparatorLine betw. Main- & Sub-Folder

   $suppind    = '>>>>>SUPPRESS>>>>>';          // Identifies Start of Scripts to Suppress

//  END of User-CUSTOMIZATION
// =================================================================================================

   Global $list, $prefer, $subf;                // for communication with subrtn
// -------------------------------------------------------------------------------------------------
// Start work, in MAIN ScriptFolder
   $listsum    = '';                               // Init to ''
   $prefer     = $prefer0;                         // current Prefered List
   $subf       = '';                               // no subfolder: Main ScriptFolder
// List Existent Scripts, Sort out "Prefered", gather others
   Sub "_BuildList";     // Globals: $prefer, $subf, $list
   $listsum    = $listsum . $list;                 // concat current list to list-summary
// $listsum    = $listsum . _BuildList($list, $prefer0, '');   // Build List for MAIN ScriptFile

// -------------------------------------------------------------------------------------------------
// ...and then the SUB-Folder
   If ($subf1 != '')                               // Only If a Subfolder is specified: handle it
   {  $listsum = "$listsum<crlf>$separats";           // add SeparatorLine "Sub-Folder"
      $prefer  = $prefer1;                            // current Prefered List
      $subf    = "$subf1";                            // subfolder of Main Scriptfolder
// List Existent Scripts, Sort out "Prefered", gather others
      Sub "_BuildList";     // Globals: $prefer, $subf, $list
      $listsum    = $listsum . $list;                 // concat current list to list-summary
//    $listsum    = $listsum . _BuildList($list, $prefer0, '');   // Build List for MAIN ScriptFile
   }
// -------------------------------------------------------------------------------------------------
   $listsum    = Substr($listsum, 2);                 // Suppress leading crlf
// Let User Select a Script:
   $script     = InputSelect('Select a Script to Execute:',
                              $listsum, <crlf>, , , 300, 500, "SelScript $vers");
// ...and execute it (if not '' or a separatorline selected):
   If ($script != '' && $script != $separatp && $script != $separats) { load $script; }

 End 1; // T H A T ' S   I T   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// =================================================================================================
// Subroutine(s))
// =================================================================================================
"_BuildList"    // Build combined list from Prefers & ListFolder
   Global $list, $prefer, $subf;                // for communication with subrtn
   Global $suppind, $separatp, $separats;       // some Global Constants

   $list       = '';                               // Init: nothing found yet
   If ($prefer != '')                              // If Prefered are specified: 
   {  
      $subfx = ($subf=='') ? '' : "$subf\";           // Append '\' if subfolder is not ''
      ForEach ($entry, $prefer, <crlf>)               // for each entry:
      {  If ($entry == $suppind) {Break;}                // If SUPPRESS-indicator: Break
         $list = "$list<crlf>$subfx$entry";              // Else concatenate
      }
      $list = "$list<crlf>$separatp";                 // at last: add SepLine "Prefered"
   }

   $preferlow  = ReCase($prefer, 'lower');         // make LowerCase for UniqueCompare

// List in Sub-ScriptFolder all *.xys (Files-only, without pathes)
   $listfold   = ListFolder("<xyscripts>".(($subf=='') ? '' : "\$subf"), "*.xys", 5);

   ForEach ($entry, $listfold)                     // For Each Entry in found Scripts...
   {  $entryl  = ReCase($entry, 'lower');             // LowerCase for Compare
      ForEach ($preferentry, $preferlow, <crlf>)      // (already LowerCase)
      { If ($entryl == $preferentry)                     // Entry already in "Prefered"
         { $entry = ''; Break; }                            // => ignore it, Break for next
      }
      If ($entry != '') { $list = ($subf=='') ? "$list<crlf>$entry" : "$list<crlf>$subf\$entry"; }
   }