Renamer with html form

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
kiwichick
Posts: 738
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Renamer with html form

Post by kiwichick »

I have a simple renamer script that I use:

Code: Select all

$a = input("Text to replace", , <curbase> , s);
   $b = input("Replace with", , , s);
   rename s, "$a/$b";   
I know next-to-nothing about html but I downloaded Muroph's text replacer http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=4387 and I've managed to edit the html form so that it looks like this:
Image
But I don't know how to incorporate the two. Any pointers would be great.

Cheers :D
Windows 10 Pro 22H2
Scaling 150%

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Renamer with html form

Post by Stefan »

That have been explain several times,
just see some older scripts like this http://www.xyplorer.com/xyfc/viewtopic. ... 748#p76748

In short:
- the html() command is launched to return the data into an variable: " $htmlVAR = html() "
- the HTML elements itself have fixed NAMES and given or modified VALUES
- by clicking at an " <INPUT type="submit" " -button everything is stored into that variable
in the format " ?NAME=VALUE&NAME=VALUE&NAME=VALUE

- Next it is up to you to parse this $htmlVAR to see if it contains valid content
and then to split it up at the "&" delimiter: "$varNameAndValue = gettoken($html, "1", "&"); "

- Again split this into parts at the equal sign: "$varValue = gettoken( $varNameAndValue ,2, "=" ); "

- Next add your working code:
for example:
if ( $varValue == "this") { do this}
if ( $varValue == "that") { do that}
but there are many ways and possibilities.


basically that is it, i assume.
Of course one should have some HTML scripting skills, and read about urldecode and utf8decod

Please ask again if unclear and i will collect this discussion to make an How-to of it....


Later more
.

kiwichick
Posts: 738
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Renamer with html form

Post by kiwichick »

Whoosh! That's the sound of all that going right over my head :veryconfused: Guess I'll give it a miss for now :lol:

EDIT: But I'll be back with questions at some stage, I'm sure.
Windows 10 Pro 22H2
Scaling 150%

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Renamer with html form

Post by Stefan »

Ok i will start my how-to for the XYplorer scripting command html()

Get Help

Code: Select all

   /*
   For XYplorer script command syntax see 
      Help > Adv. Topics > Scripting Command Reference > HTML
      html([html], [width=600], [height=400], [caption=XYplorer])

   For HTML syntax see e.g. 
      http://werbach.com/barebones/barebones.html
   */
Mandatory basics

Code: Select all

  $myHTMLvar = urldecode(html('
        <html>
          <body>
             <form method="get" action="xys:">
                <INPUT type="submit" name="DoItButton" value="DoIt" >
             </form>
          </body>
        </html>
        ',"600", "400", "My HTML GUI")); 
Explanations:
urldecode(); --->is useful
html(' ... ', , ,); --->is XYplorers scripting command
action="xys:" --->XYS: is mandatory to use this html code inside an XYplorer script
<INPUT type="submit" name="" value=""> --->is needed to start the action
<html><body><form></form></body></html> --->is HTML code

Extra tip:
You can also put the HTML code into an text file and load it from the script command:
html("D:\drive\path\to\example.html", , ,);


An starting working code:

Code: Select all


  //Execute the XYplorer htlm() script command
  //and get the result into an variable called $myHTMLvar

  $myHTMLvar = urldecode(html('

        //compose a HTML page
        <html>
        <body>
        <form method="get" action="xys:">
        Replace:                  <INPUT TYPE="TEXT" NAME="InputONE" SIZE="80" VALUE="string (to search)"><BR>
        With: &nbsp;&nbsp; &nbsp; <INPUT TYPE="TEXT" NAME="InputTWO" SIZE="80" VALUE="replace string">  <BR>
        <BR><HR>
        <p align="right"><INPUT type="submit" name="DoItButton" value="    DoIt    " ></p>
      
        </form>
        </body>
        </html>',"600", "400", "My HTML GUI")); 

   //end of the html() command


  //----------------------------------------------------------------------------
  // After the user clicked at one of the buttons, you have to parse the returning 
  // of this html() command which was stored into the $myHTMLvar variable:


        //If the user clicked at [Close] of the XYplorer dialog:
          end($myHTMLvar == ""), "Close pressed. Script will quit here.";



        //else one of the type="submit" buttons are clicked:
          msg "The script returned:<crlf 2>$myHTMLvar";
            


         //so you will see an dialog like this:
           /*
            ---------------------------
                        XYplorer
            ---------------------------
            The script returned:
      
            ?InputONE=string (to search)&InputTWO=replace string&DoItButton=    DoIt    
            ---------------------------
                          OK   
            ---------------------------
           */

     //Note: without the urlDEcode() command the returning string would look like:
          //?InputONE=string+%28to+search%29&InputTWO=replace+string&DoItButton=++++DoIt++++

//to continue the how-to INSERT HERE THE SECOND PART ==>



Now we split the content of the $myHTMLvar into parts.

THE SECOND PART

Code: Select all

//THE SECOND PART

  //Now we split the content of the $myHTMLvar into parts.
  //Since we already know the correct order of the returned NAME/VALUE pairs,
  //(just as they appears in the html code) we do simple processing one by one.
  //The content of the $myHTMLvar is like: ?NAME1=VALUE1&NAME2=VALUE2&NAME3=VALUE3

  //So we split first at the ampersand sign &:
     $FieldOne = gettoken($myHTMLvar, "1", "&");
     $FieldTwo = gettoken($myHTMLvar, "2", "&");
  //Test:
    msg $FieldOne;      //> ?InputONE=string (to search)
    msg $FieldTwo;     //> InputTWO=replace string


  //Next we split at the equal sign =:
     $FieldOne = gettoken($FieldOne, "2", "=");
     $FieldTwo = gettoken($FieldTwo, "2", "=");
  //Test:
    msg $FieldOne;      //> string (to search)
    msg $FieldTwo;     //> replace string

For me this works:
Image






Next
you can use this to continue e.g. with an another XYplorer scripting command:

rename s, "$FieldOne/$FieldTwo";


Of course you should code some more error-handling
for example to see if the variable content is valid for your further scripting.


And of course there are more then this way to parse the $myHTMLvar.


to be continued....

kiwichick
Posts: 738
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Renamer with html form

Post by kiwichick »

Ah, the fog is clearing a little. I'll take a much closer look when I get some time later this afternoon. Thanks heaps!!
Windows 10 Pro 22H2
Scaling 150%

kiwichick
Posts: 738
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Renamer with html form

Post by kiwichick »

Awesome Stefan, I've got it working!!! I only needed to add:

Code: Select all

    rename s, "$FieldOne/$FieldTwo";    
Thank you so much!!!
Windows 10 Pro 22H2
Scaling 150%

kiwichick
Posts: 738
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Renamer with html form

Post by kiwichick »

Hi Stefan, in Muroph's script I changed the 'Replace this' Value to "<curbase>" which works just fine and I get the selected file basename pre-filled in the field. But it doesn't work in your script. I changed:

Code: Select all

VALUE="string (to search)"
to

Code: Select all

VALUE="<curbase>"
but I don't get the basename in the 'Replace this' field, I just get <curbase>. I also tried storing "<curbase>" to a variable but then I just get the variable name in the field. There's must be some kind of difference in the html code used between the two of you but it's beyond my ability to tell the difference. So is there a way to achieve the same thing in your code?
Windows 10 Pro 22H2
Scaling 150%

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Renamer with html form

Post by Stefan »

kiwichick wrote:Hi Stefan,
I don't get the basename in the 'Replace this' field, I just get <curbase>.
That is shown in the above linked post too:

Code: Select all

<TEXTAREA WRAP=Off NAME="taINPUT" ROWS=14 COLS=90>'. "<get SelectedItemsPathNames>" .'</TEXTAREA> 
How-to use XYplorer code inside HTML code

You have to understand that our whole HTML code is only an long string inside the html('...') command.

The trick here to include XY-vars is to escape from the HTML code for the moment,
concatenate an XYplorer $variable or XYplorer native variable like "<curname>",
and then continue with concatenate the remaining HTML code again.

For our code above we change

Code: Select all

Replace: <INPUT TYPE="TEXT" NAME="InputONE" SIZE="80" VALUE="string (to search)"><BR>
to

Code: Select all

Replace: <INPUT TYPE="TEXT" NAME="InputONE" SIZE="80" VALUE="'."<curbase>".'"><BR>

Let's take an closer look:
VALUE="string (to search)"
is replaced by
VALUE="'."<curbase>".'"

And the same with more spaces added to clarify:
VALUE=" ' . "<curbase>" . ' "

Explanation:
' ---> will close and begin (means 'enclose') the HTML code string.
. ---> the dot is the XY script command to concatenate strings.
"<curbase>" ---> is an XY script native variable (see Help > Adv Top > Variables)
You can use e.g. <curbase>, <curname>, <curitem>, <curfolder>, <curpath>, <date>, "<get SelectedItemsNames>" or "<get SelectedItemsPathNames>"

Note:
the HTML code element VALUE itself needs surrounding double quotes: VALUE="my value"
which makes this example even more confusing, but i hope you get it.
This second example e.g. will make this more clear:

Code: Select all

<TEXTAREA >'   . "<get SelectedItemsPathNames>" .  '</TEXTAREA>

Additional tip:
If you want to include an $var inside the HTML string, do something like this:
$myVar = "<curbase>";

Code: Select all

Replace: <INPUT TYPE="TEXT" NAME="InputONE" SIZE="80" VALUE="  '  .  $myVar  . '  "><BR>


The next how-to is about having two buttons.

.

kiwichick
Posts: 738
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Renamer with html form

Post by kiwichick »

Yes, I see now. Thank you for pointing that out to me :appl:
Windows 10 Pro 22H2
Scaling 150%

Post Reply