Hey guys
Okay so while I still think this little script is cool (yeah I love myself too

) 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!
