Script Request - Name Replacer aka Themes Switcher.

Discuss and share scripts and script files...
Post Reply
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Script Request - Name Replacer aka Themes Switcher.

Post by SkyFrontier »

Can someone please provide me a little script that:

-Renames a "Themes" folder as something else (just to avoid names collision).
-gets currently selected folder "Themes_Arts" and renames it as "Themes", storing "Themes_Arts" for future usage.
-if I want another themes folder to be the current "Themes" base folder, say "Themes_Nature", script will grab current "Themes" and rename it after its original "Themes_Arts", then rename "Themes_Nature" as the current "Themes" folder.
-Ideally, the script could pop a dialog with a "Please Select Folder to be the Active Themes Folder", filled with a list of all available options, for example:

Code: Select all

<xydata>\_Res\Base:
"Themes" <this can be left out of displaying, any way - whatever is easier.
"Themes_Arts"
"Themes_Beauty"
"Themes_Digital"
"Themes_Nature"
"Themes_Tech"
Thanks!
Last edited by SkyFrontier on 27 Nov 2010 22:09, edited 1 time in total.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script Request - Name Replacer.

Post by TheQwerty »

Are you sure you cannot figure this one out?

I'd think with all the scripts you've seen and had built for you, that you could glean at least an inkling of how to do this.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Name Replacer.

Post by SkyFrontier »

Pois é.

(portuguese for: I should but I can't. I'm stuck at the conceptual part of it. I envisioned a way to set a key inside an .ini file and get the script to read such info and do the rename thing while alternating previous folder, but just like with the URL Detagger - Offline Mode, I can't get a two-state/avoid-name-collision-in-an-intermediate-state situation to work, like in

Code: Select all

   input $name, "Please Enter Name of the File", "<curitem>";
   input $met, "Chose and ADJUST the method to be used", "clipboard|readfile(curitem, b),,b)";
   $a = $met;
-in case, it should be a mere act of adjusting input box to read just <clipboard> OR readfile(<curitem>, b),,b)"; and voilà, $a get the proper parameter and keep going. But not, something goes wrong and the script ends broken, so I ended up with a set-both-parameters-and-get-one-commented/disabled solution, a minor hassle and better than nothing. Scripting smartness is what's missing - in portuguese, "o pulo do gato")
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script Request - Name Replacer.

Post by TheQwerty »

How and where were you thinking the currently used themes' actual name should be stored?

If, for example, the collection of theme folders is under "D:\Data", I'd like to store a file ".nameReplacer.ini" (or similar) there. It would be really great to make it hidden but Don still hasn't graced us with a SC for changing attributes, and I'm reluctant to call the shell here. (But the script wouldn't care so the user could hide it after it's created.)

I'm leaning this way because I feel it's an easy solution that also provides the most flexibility, but it does require being able to write directory.

The runner up to this would be adding a line to the XY comment of either "D:\Data" or "D:\Data\Theme," but I don't particularly like the idea of working around the user's comments to abuse this for the script.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Name Replacer.

Post by SkyFrontier »

.Ini should be fine to go under "D:\Data".
No problems it not being hidden.
If you bother to check if chosen item is a "folder" (MUST be - default - just do it) or a "file" ("item is a file - proceed?") it should be great both for learning purposes and error handling.
I'll be offline for a few hours now, hope this is enough feed...
Thanks for working on this one too, TheQwerty! Probably later I'll can properly answer about the remaining request (I'm trying to get it done via the "URL Extractor" lesson, but you know...).
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Name Replacer aka Themes Switcher.

Post by SkyFrontier »

In hopes this one is worthy enough and... complaint-free.
Any comments on possible usages other than I planned (real cases, please) or bug reports will be highly appreciated.
Initially it was designed to switch Lighttek's Talisman (external link) "themes" folders, but I've rewrote it completely to make it a universal switcher. The only drawback: I'd really like it should be possible for it to avoid the initial (post-setup, of course...) navigation after the main app folder. Hard-coding it seems to be confuse, but I may be missing something at any point - and as always, I avoid using <xyscripts> as home for scripts as much as possible. Ultimate portability matters. :wink:

Code: Select all

"'Themes' Folders Switcher|:steps"

   inputfolder $mpath, "c:\", "Please indicate your main application's path<crlf>which contains the target 'Themes' folder:";
   goto $mpath;
   $ini = "$mpath\ThemesHandler.ini"; IF (exists($ini) == 0)
   {
    new $ini, file;
    echo "$ini was not present and it has been created!<crlf>It is STRONGLY recommended that you set it up now.<crlf>Please click 'OK' to proceed.";
    sub "_SETUP";
   }

   elseif

   {
   selfilter """ThemesHandler.ini""";
   $size=report("{Size}",1);
   IF (($size) == 0) {
   $cnfrm = confirm("The program requires a basic setup which was not completed thus far.<crlf><crlf>Please click 'OK' to config it now<crlf> or 'Cancel' to end this script.")?"YES":"NO");
   IF ($cnfrm == YES) { sub "_SETUP" }
   ELSE  ($cnfrm == NO) { sub "_NOP" }
   }

   $th = getkey("Default", "1_ThemesDEF", "$ini");
   $base = getkey("Path", "2_Path", "$ini");
   $ini = getkey("ini", "2_INI", "$ini");

   $tback = getkey("Old", "1_ThemesOLD", "$ini");
   $tfut = getkey("Current", "1_ThemesCUR", "$ini");

   $answer = confirm("New '$th' main folder will be '$tback'.<crlf>Current '$th' folder will be renamed as '$tfut'<crlf>and stored for future fast switches.<crlf><crlf>Please click 'OK' to continue or 'Cancel' to choose another folder.")?"_YES":"_NO";
   IF ($answer == _YES) {  }
   ELSE  ($answer == _NO) { sub "_NON" }

   $tback = getkey("Old", "1_ThemesOLD", "$ini");
   $tt = getkey("Default", "1_ThemesDEF", "$ini");
   selfilter """$tt""";

   $tc = getinfo("CountSelected");
   IF (($tc) != 1) { status "Fatal error detected: '$th' not present.", BF0000, stop; end 1, "Folder '$th' is not available and it's crucial to your app.<crlf>Please check what's wrong and fix it!<crlf><crlf>This script will now be stopped.<crlf><crlf>Have a nice day! ^^"; }

//   #485; //refresh list - avoid surprises on list...
   selfilter """$tt""";
   rename r, "$tt > themeTMP";
//   #485; //refresh list - see what's going on...
   $tback = getkey("Old", "1_ThemesOLD",  "$ini");
   selfilter """$tback""";
   rename r, "$tback > $tt";
//   #485; //refresh list - see what's going on...
   $ct =  getkey("Current", "1_ThemesCUR",  "$ini");
   selfilter """themeTMP""";
   rename r, "themeTMP > $ct";

   setkey "$mpath", "Path", "1_Path", "$ini";
   setkey "$tback", "Current", "1_ThemesCUR", "$ini";
   setkey "Themes", "Default", "1_ThemesDEF", "$ini";
   setkey "$ct", "Old", "1_ThemesOLD", "$ini";

   #485; //refresh list - to display updated .ini...
   echo "'$tback' is your new 'Themes' folder!";

"_NON"
   $ini = "<curpath>\ThemesHandler.ini";
   $tnn = getkey("Default", "1_ThemesDEF", "$ini");
   $resp = confirm("Please click 'OK' to select another folder which will be set<crlf>as current '$tnn' folder or 'Cancel' to end this script.")?"_YO":"_NOP";
   IF ($resp == _YO) { sub "_YO"; }
   ELSE  ($resp == _NO) { sub "_NOP"; }

"_NOP"
   status "Operation canceled by user.", BF0000, alert;
   end 1;

"_SETUP"
   $ini = "<curpath>\ThemesHandler.ini";
   input $th, "Please enter the default 'Themes' folder name:", "Themes";
   $c2r = "$th _";
   replace $c2r, $c2r, "$th _", "$th _"; replace $c2r, $c2r, " ", "";
   input $ct, "Set the category name for the current '$th' folder ('$c2r' will be prepended):", "CategoryName";
   $sug = replace ("$ini", "ThemesHandler.ini", "");
   $cr1 = "$th $ct";
   replace $cr1, $cr1, "$th $ct", "$th _$ct"; replace $cr1, $cr1, " ", "";
   inputfolder $old, "$sug", "Now, please select the folder that will replace current 'Themes' folder in future category swaps:<crlf>(current 'Themes' will be named: '$cr1')";
   $old = replace ("$old", "$sug", "");

   setkey "<curpath>", "Path", "2_Path", "$ini";
   setkey "$ini", "ini", "2_INI", "$ini";
//test: ::$th = 1; $ct = ab; $y = "$th $ct"; replace $crr, $y, "$th $ct", "$th_$ct"; echo $crr;
//test OK: ::$th = 1; $ct = ab; $y = "$th $ct"; replace $crr, $y, "$th $ct", "$ct _$th"; replace $crr, $crr, " ", ""; echo $crr;
   $crr = "$th $ct";
   replace $crr, $crr, "$th $ct", "$th _$ct"; replace $crr, $crr, " ", "";
   setkey "$crr", "Current", "1_ThemesCUR", "$ini";
   setkey "$th", "Default", "1_ThemesDEF", "$ini";
   setkey "$old", "Old", "1_ThemesOLD", "$ini";
   #485; //refresh list - to display updated .ini; generates a bug if not present (runs setup a 2nd time)...

"_YO"
   $ini = "<curpath>\ThemesHandler.ini";
   $tnow = getkey("Current", "1_ThemesCUR", "$ini");
   $ty = getkey("Default", "1_ThemesDEF", "$ini");
   $tback = getkey("Old", "1_ThemesOLD", "$ini");
   inputfolder $oldy, "<curpath>", "Replacement for '$ty' in next category swap:<crlf>(previous choice would be: $tback;<crlf>current '$ty' folder is: $tnow)";
   $oldy = replace ("$oldy", "<curpath>\", "");
   setkey "$oldy", "Old", "1_ThemesOLD", "$ini";
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Name Replacer aka Themes Switcher.

Post by SkyFrontier »

Updates:
-Found a hardcoded legacy code - fixed.
-Added a way to easily set a predefined path for the script to work, thus avoiding initial browsing for the main app's dir.
-work on progress: adapting the same script to work with FILES, too - useful to test localization patches... :wink:

EDIT: Scratched v2 in favor of this.
Updates for v3:
-now it lists similar base-named folders to inform user on what names *not* to use, prior to chose a category name.
-additional error-checking code to stop script if it detects potential name collision that would lead to unwanted effects.
-better warnings, rephrasing.
-improved preview of what will happen and to show who's who, now and after the changes if they are accepted by user.
-better ending status messages that will tell the user what is what, and keeps the last used "themes" folder selected in list.

Code: Select all

"'Themes' Folders Switcher, v3 - FINAL|:steps"

   inputfolder $mpath, "c:\", "Please indicate your main application's path<crlf>which contains the target 'Themes' folder:"; //disable this to get no prompts on main app's path.
//   goto "c:\path\"; $mpath = "<curpath>"; //enable this to get no prompts regarding main app's path; optional - disable the next command.
   goto $mpath;

   $ini = "$mpath\z_ThemesHandler.ini"; IF (exists($ini) == 0)
   {
    new $ini, file;
    echo "$ini<crlf>was not present and it has been created!<crlf>It is STRONGLY recommended that you set it up now.<crlf>Please click 'OK' to proceed.";
    sub "_SETUP";
   }

   elseif

   {
   selfilter """z_ThemesHandler.ini""";
   $size=report("{Size}",1);
   IF (($size) == 0) {
   $cnfrm = confirm("The program requires a basic setup which was not completed thus far.<crlf><crlf>Please click 'OK' to config it now<crlf> or 'Cancel' to end this script.")?"YES":"NO");
   IF ($cnfrm == YES) { sub "_SETUP" }
   ELSE  ($cnfrm == NO) { sub "_NOP" }
   }

   $th = getkey("Default", "1_ThemesDEF", "$ini");
   $base = getkey("Path", "2_Path", "$ini");
   $ini = getkey("ini", "2_INI", "$ini");

   $tback = getkey("Old", "1_ThemesOLD", "$ini");
   $tfut = getkey("Current", "1_ThemesCUR", "$ini");

   $answer = confirm("New '$th' main folder will be<crlf>'$tback'.<crlf>Current '$th' folder will be renamed as<crlf>'$tfut'<crlf>and stored for future fast switches.<crlf><crlf>Please click 'OK' to continue or 'Cancel' to choose another folder.")?"_YES":"_NO";
   IF ($answer == _YES) {  }
   ELSE  ($answer == _NO) { sub "_NON" }

   $tback = getkey("Old", "1_ThemesOLD", "$ini");
   $tt = getkey("Default", "1_ThemesDEF", "$ini");
   selfilter """$tt""";

   $tc = getinfo("CountSelected");
   IF (($tc) != 1) { status "Fatal error detected: '$th' not present.", BF0000, stop; end 1, "Folder '$th' is not available and it's crucial to your app.<crlf>Please check what's wrong and fix it!<crlf><crlf>This script will now be stopped.<crlf><crlf>Have a nice day! ^^"; }

//   #485; //refresh list - avoid surprises on list...
   selfilter """$tt""";
   rename r, "$tt > themeTMP";
//   #485; //refresh list - see what's going on...
   $tback = getkey("Old", "1_ThemesOLD",  "$ini");
   selfilter """$tback""";
   rename r, "$tback > $tt";
//   #485; //refresh list - see what's going on...
   $ct =  getkey("Current", "1_ThemesCUR",  "$ini");
   selfilter """themeTMP""";
   rename r, "themeTMP > $ct";

   setkey "$mpath", "Path", "1_Path", "$ini";
   setkey "$tback", "Current", "1_ThemesCUR", "$ini";

//could raise potential bug:   setkey "Themes", "Default", "1_ThemesDEF", "$ini";

   $nt =  getkey("Default", "1_ThemesDEF",  "$ini");

   setkey "$nt", "Default", "1_ThemesDEF", "$ini";

   setkey "$ct", "Old", "1_ThemesOLD", "$ini";

   #485; //refresh list - to display updated .ini...
   echo "'$tback' is your new '$nt' folder!";
   status "'$tback' is now '$nt'!";

"_NON"
   $ini = "<curpath>\z_ThemesHandler.ini";
   $tnn = getkey("Default", "1_ThemesDEF", "$ini");
   $resp = confirm("Please click 'OK' to select another folder which will be set<crlf>as current '$tnn' folder or 'Cancel' to end this script.")?"_YO":"_NOP";
   IF ($resp == _YO) { sub "_YO"; }
   ELSE  ($resp == _NO) { sub "_NOP"; }

"_NOP"
//   msg "Operation canceled by user.<crlf>Have a nice day! ^^";
//   status text, [color RRGGBB], [icon=ready|progress|alert|stop]
   status "Operation canceled by user.", BF0000, alert;
   end 1;

"_SETUP"
   $ini = "<curpath>\z_ThemesHandler.ini";
   input $th, "Please enter the default 'Themes' folder name:", "Themes";
   $c2r = "$th _";
   replace $c2r, $c2r, "$th _", "$th _"; replace $c2r, $c2r, " ", "";
   selfilter "$th";
   $rep = get("SelectedItemsNames", "<crlf>");
   $ct = input("Set the category name for the current '$th' folder:", "('$c2r' will be auto-prepended)<crlf><crlf>Currently on list (you should REALLY avoid using them):<crlf>$rep", "ALT", "S");

   $sug = replace ("$ini", "z_ThemesHandler.ini", "");
   $cr1 = "$th $ct";
   replace $cr1, $cr1, "$th $ct", "$th _$ct"; replace $cr1, $cr1, " ", "";

      selfilter """$cr1""";
      $ex = getinfo("CountSelected");
      IF ($ex == 1) { status "Error: '$cr1' would crash script.", BF0000, stop; end 1, "~~~~~~~~~~---------~~~~~~~~~~<crlf>    ~~~~~~~-WARNING-~~~~~~~<crlf>~~~~~~~~~~---------~~~~~~~~~~<crlf><crlf>Setup has detected that a file of same name<crlf>already exists and it could lead to several problems.<crlf><crlf>Please run the script again and pay attention to the list<crlf>presented list during setup and avoid duplicates.<crlf><crlf>Fatal error detected: '$cr1' would be<crlf>duplicated and could crash either the script or the operation.<crlf>Have a nice day! ^^<crlf><crlf>~~~~~~~~~~---------~~~~~~~~~~"; }
      ELSE ($ex != 1) {   }
//      setting "AllowRecursion", 0;

   inputfolder $old, "$sug", "Now, please select the folder that will replace current '$th' folder in future category swaps:<crlf>(current '$th' will be named: '$cr1')";
   $old = replace ("$old", "$sug", "");

   setkey "<curpath>", "Path", "2_Path", "$ini";
   setkey "$ini", "ini", "2_INI", "$ini";
//test: ::$th = 1; $ct = ab; $y = "$th $ct"; replace $crr, $y, "$th $ct", "$th_$ct"; echo $crr;
//test OK: ::$th = 1; $ct = ab; $y = "$th $ct"; replace $crr, $y, "$th $ct", "$ct _$th"; replace $crr, $crr, " ", ""; echo $crr;
   $crr = "$th $ct";
   replace $crr, $crr, "$th $ct", "$th _$ct"; replace $crr, $crr, " ", "";
   setkey "$crr", "Current", "1_ThemesCUR", "$ini";
   setkey "$th", "Default", "1_ThemesDEF", "$ini";
   setkey "$old", "Old", "1_ThemesOLD", "$ini";
   #485; //refresh list - to display updated .ini; generates a bug if not present (runs setup a 2nd time)...

"_YO"
   $ini = "<curpath>\z_ThemesHandler.ini";
   $tnow = getkey("Current", "1_ThemesCUR", "$ini");
   $ty = getkey("Default", "1_ThemesDEF", "$ini");
   $tback = getkey("Old", "1_ThemesOLD", "$ini");
   inputfolder $oldy, "<curpath>", "Replacement for '$ty' in next category swap:<crlf>(previous choice would be: $tback;<crlf>current '$ty' folder is: $tnow)";
   $oldy = replace ("$oldy", "<curpath>\", "");
   setkey "$oldy", "Old", "1_ThemesOLD", "$ini";

New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Name Replacer aka Themes Switcher.

Post by SkyFrontier »

I've updated this one to v3 (Final) and posted its counterpart (final version, either, as far as no one reports any bug for both of them and I'm completely happy with the achievements on them), Name Replacer - Swap FILES Names.
Look after the Updates for v3 section in the previous post and you'll see the difference.

*if you think you must make comments on code, suggestions, bug reports or anything else, please do so! I'll be very pleased to receive such a thing.*
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply