Looping Through Selected Items (w/ Examples)

Discuss and share scripts and script files...
Post Reply
jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Looping Through Selected Items (w/ Examples)

Post by jacky »

Hey there,

Alright so in my daily XY scripting I always try to do what I need, even when it's not really/fully/natively supported. Sure, I can't do "onEvent scripting" (or whatever the official name for that feature will be, I keep forgetting :oops:) on my own, but we can still do a couple of little things.

For example, using regexp we can deal with basic conditions and create loops. Putting this to use, I made a little script that will allow one to (hopefully) easily write scripts without worrying about the whole looping part.

Move all items in selected folder(s) to current folder

It should be easy enough : you first need to put in your "Scripts" folder the script file "Loop Through Selected Items.xys" ; and then in that folder you can use your own scripts like this one little script, which will ignore any selected file, but move the content of each of the selected folders into the current one. Here you go (MoveToCurrent.xys) :

Code: Select all

"Move all items in selected folder(s) to current folder"
 setkey "MoveToCurrent", "File", "Load", "Loop Through Selected Items.ini";
 setkey "move", "Name", "Load", "Loop Through Selected Items.ini";
 load "Loop Through Selected Items", "_Start";

"_moveFile"
 // do nothing
"_moveFolder"
 moveto <curpath>, "<curpath>\<curname>\*";
"_moveDone"
 status "Moving done";
See, looks pretty easy, doesn't it? ;)

Okay, for more about all of this and how it works, please see here.

To use this, you'll need to download the "looping script" (Loop Through Selected Items.xys) ; and for examples here's one ready-to-use file with the "move content of selected folder to current location" example above, and another one that will count how many files/folders are in each of the selected folders : CountMove.xys

Before I go, two important things :
- Please note that I wrote this script with the idea that all selected items where in the same folder, and it will not work otherwise. In other words, you should not try to use it from search results, because results would be unexpected ! You've been warned.
- For internal reasons, both the "Loop Through Selected Items.xys" and your own script file(s) should all be located under "<xydata>\Scripts\"

Hopefully this'll be helpful/useful to some...

Happy XY scripting! ;)
Proud XYplorer Fanatic

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Looping Through Selected Items (w/ Examples)

Post by serendipity »

I am amazed what RegExp can do. I wish I had the time to learn RegExp. Thanks Jacky. :)
Based on this, I created an amateur script for fullscreen viewing of selected images.

Code: Select all

"Slideshow : slide"
  setkey "slideshow", "File", "Load", "Loop Through Selected Items.ini";
  setkey "slide", "Name", "Load", "Loop Through Selected Items.ini";
  load "Loop Through Selected Items", "_Start";
  
"_slideFile"
//Fullscreen preview
 #1003;
"_slideFolder"
//nothing 
"_slideDone"
//nothing
Its not really a slideshow because its not automatic and this is not advanced enough to go to previous image or stop between the scripts. But nice thing is clicking anywhere on the image goes to next image.

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Re: Looping Through Selected Items (w/ Examples)

Post by jacky »

serendipity wrote:I am amazed what RegExp can do. I wish I had the time to learn RegExp. Thanks Jacky. :)
Based on this, I created an amateur script for fullscreen viewing of selected images.
Happy it's useful to some :D
serendipity wrote:Its not really a slideshow because its not automatic and this is not advanced enough to go to previous image or stop between the scripts. But nice thing is clicking anywhere on the image goes to next image.
For the record: you know that if you select multiple files and go to Full Screen Preview, using the arrow keys you will be able to go through your selection and your selection only ;)
Proud XYplorer Fanatic

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Looping Through Selected Items (w/ Examples)

Post by serendipity »

jacky wrote:For the record: you know that if you select multiple files and go to Full Screen Preview, using the arrow keys you will be able to go through your selection and your selection only ;)
Oh yes, I know that. But you cant use mouse there. I wanted something that does both keyboard and mouse too.

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Re: Looping Through Selected Items (w/ Examples)

Post by jacky »

Hey guys :)

Okay so while I still think this little script is cool (yeah I love myself too :P ;)) and can be useful, it might be a little too much for cases when you don't care whether it's a file or a folder that was selected, because you feel fine assuming it will always be either one, or you will do exactly the same for both.

In such a case, here's another little trick, shorter, faster and maybe easier to use (is it?) that some of you might find useful...
Note that this has to be done from a script file otherwise it can't/won't work. The first script is the "menu item", the one to be triggered, your code goes into the second script (_ForEachItem) :

Code: Select all

"To All Selected Items..."
  // this script file
  self $me, file;
  // Item Name(s)
  focus l;
  #102;
  // locate CRLF (when multiple items are selected)
  strpos $p, <clipboard>, <crlf>;
  // there's one, will do nothing
  regexreplace $script, $script, "^[0-9]+$", 'incr $void;';
  // no CRLF, only one item, let's add a CRLF as we require each line to end with one
  regexreplace $script, $script, '^((?!^incr \$void;$).)*$', "copytext '<br>', a;";
  // execute
  load $script,,s;
  // now turn the list of all items<crlf> into a little script
  regexreplace $script, <clipboard>, "(.+)\r\n", " copytext ""$1""; load ""$me"", ""_ForEachItem""; ";
  // and go
  load $script,,s;
  // clean
  self $path, path;
  delete 0, 0, "$path\tmp.ini";

"_ForEachItem"
  // item name
  set $item, <clipboard>;
  // select the item
  sel 1,0;
  sel "[$item]";
  // whatever
  msg <curitem>;
Voilà! As you can see it uses the clipboard, and so the script _ForEachItem will be called for each of the selected item, and the item's name will be put in the clipboard before so you know what to work with.

Of course, the selection is unchanged, so if you need to use a command relating to the selected item(s) you better make sure to only select the item you should be dealing with, hence why I used Item Names (#102;) and not Item Path/Names (#101;) in the first place.


Now, in order to be able to use this trick for different scripts without too much work, we will simply add a little something : a suffix for the the script's label called for each item, one that will be read from an INI file. As a result, all it takes to use this is to first set this INI setting, then call the "main" script.

And, for a more practical example, the same one as I had given above (I love this one I guess ;)) that will move all items of the selected folders into the current folder, and then remove the (now empty) folders.

Code: Select all

"Sample"
  // set the "type" to know which script to call
  setkey "sample", "type", "ForEachItem", "tmp.ini";
  // do everything else
  sub _ToAllItems;
"Sample w/Selection"
  // set the "type" to know which script to call
  setkey "sample_selection", "type", "ForEachItem", "tmp.ini";
  // do everything else
  sub _ToAllItems;
"Move Folders' Items To Current"
  // set the "type" to know which script to call
  setkey "MoveItemsInFolderToCurrent", "type", "ForEachItem", "tmp.ini";
  // do everything else
  sub _ToAllItems;

"_ToAllItems"
  // this script file
  self $me, file;
  // Item Name(s)
  focus l;
  #102;
  // locate CRLF (when multiple items are selected)
  strpos $p, <clipboard>, <crlf>;
  // there's one, will do nothing
  regexreplace $script, $script, "^[0-9]+$", 'incr $void;';
  // no CRLF, only one item, let's add a CRLF as we require each line to end with one
  regexreplace $script, $script, '^((?!^incr \$void;$).)*$', "copytext '<br>', a;";
  // execute
  load $script,,s;
  // get type of calls
  getkey $type, "type", "ForEachItem", "tmp.ini";
  // now turn the list of all items<crlf> into a little script
  regexreplace $script, <clipboard>, "(.+)\r\n", " copytext ""$1""; load ""$me"", ""_ForEachItem_$type""; ";
  // and go
  load $script,,s;
  // clean
  self $path, path;
  delete 0, 0, "$path\tmp.ini";

"_ForEachItem_sample"
  // item name
  set $item, <clipboard>;
  // whatever
  msg $item;

"_ForEachItem_sample_selection"
  // item name
  set $item, <clipboard>;
  // select the item
  sel 1,0;
  sel "[$item]";
  // whatever
  msg <curitem>;

"_ForEachItem_MoveItemsInFolderToCurrent"
  moveto <curpath>, "<curpath>\<clipboard>\*";
  delete 0, 0, "<curpath>\<clipboard>";
Like before, note that it won't work on Search Results. (Though you could easily use full path/names instead of just names, thus managed to do it I think, except for the selecting each item part...)

Hopefully that'll be useful to some of you,
Happy XY Scripting! :)
Proud XYplorer Fanatic

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Looping Through Selected Items (w/ Examples)

Post by Mesh »

With the current version of XY, is this script still necessary? Or is XY finally able to loop through selected items?

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Looping Through Selected Items (w/ Examples)

Post by admin »

Mesh wrote:With the current version of XY, is this script still necessary? Or is XY finally able to loop through selected items?
Actually I'm working on a something (a foreach syntax...).

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Looping Through Selected Items (w/ Examples)

Post by Mesh »

admin wrote:
Actually I'm working on a something (a foreach syntax...).

Oh, that would be so sweet. Thank you! Frankly, I'm shocked that this wasn't done years ago. :)

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

Re: Looping Through Selected Items (w/ Examples)

Post by TheQwerty »

Mesh wrote:
admin wrote:Actually I'm working on a something (a foreach syntax...).
Oh, that would be so sweet. Thank you! Frankly, I'm shocked that this wasn't done years ago. :)
There hasn't been much need since we have a while loop and gettoken; especially sine gettoken ca now return the total number of tokens.

Post Reply