Can a script run endlessly?

Discuss and share scripts and script files...
Post Reply
binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Can a script run endlessly?

Post by binocular222 »

For example: this script evaluates if we'are in E: drive then rename Tab's caption to "Folder name + icon":

Code: Select all

 if(substr(tab("get", "path"), 0, 3) = E:\);
 Tab("rename", "<folder>|<xypath>\icon\XYplorer.ico", Tab("get"))
I want this script to runs endlessly so that whenever I visit E: drive, the tab's caption change accordingly.
Is this possible?
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

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

Re: Can a script run endlessly?

Post by serendipity »

No not possible currently. But this might be possible in future with Custom Event Actions (CEA).

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Can a script run endlessly?

Post by binocular222 »

Thanks to SkyFrontier, I made it possible now:

Code: Select all

while ("<clipboard>" != "<xx>") {
   wait 10;
   if(substr(tab("get", "path"), 0) LikeI "Desktop") { Tab("rename", "<curfolder>|<xypath>\icon\Desktop.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 25) LikeI "D:\KPMG\To be transferred") { Tab("rename", "<curfolder>|<xypath>\icon\heart.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 6) LikeI "D:\Doc") { Tab("rename", "<curfolder>|<xypath>\icon\book.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 10) LikeI "D:\Picture") { Tab("rename", "<curfolder>|<xypath>\icon\camera.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 8) LikeI "D:\Movie") { Tab("rename", "<curfolder>|<xypath>\icon\video camera.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 31) LikeI "D:\KPMG\Job\S2a\VPB 30-Jun\VPBS") { Tab("rename", "VPBS2012\<curfolder>|<xypath>\icon\VPB.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 30) LikeI "?:\7Utilities\Video edit magic") { Tab("rename", "<curfolder>|<xypath>\icon\Playboy.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 21) LikeI "D:\KPMG\Job\S2\VPBank") { Tab("rename", "VPB\<curfolder>|<xypath>\icon\VPB.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 8) LikeI "D:\Music") { Tab("rename", "<curfolder>|<xypath>\icon\Music.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 3) LikeI "c:\") { Tab("rename", "<curfolder>|<xypath>\icon\C.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 8) LikeI "?:\1Game") { Tab("rename", "<curfolder>|<xypath>\icon\1Game.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 10) LikeI "?:\2System") { Tab("rename", "<curfolder>|<xypath>\icon\2System.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 10) LikeI "?:\3Office") { Tab("rename", "<curfolder>|<xypath>\icon\3Office.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 9) LikeI "?:\4Media") { Tab("rename", "<curfolder>|<xypath>\icon\4Media.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 10) LikeI "?:\5Design") { Tab("rename", "<curfolder>|<xypath>\icon\5Design.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 12) LikeI "?:\6Internet") { Tab("rename", "<curfolder>|<xypath>\icon\6Internet.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 22) LikeI "?:\7Utilities\XYplorer") { Tab("rename", "<curfolder>|<xypath>\icon\XYplorer.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 33) LikeI "?:\7Utilities\Launcher\AutoHotkey") { Tab("rename", "<curfolder>|<xypath>\icon\AutoHotkey.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 13) LikeI "?:\7Utilities") { Tab("rename", "<curfolder>|<xypath>\icon\7Utilities.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 3) LikeI "k:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 3) LikeI "i:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 3) LikeI "g:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 3) LikeI "h:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico", Tab("get")); }
   elseif(substr(tab("get", "path"), 0, 2) LikeI "\\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico", Tab("get")); }
   elseif(1=1) { Tab("rename", tab("get", "path"), Tab("get")); }
                                   }
Feel free to modify it to your like.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

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

Re: Can a script run endlessly?

Post by serendipity »

Glad this workaround works for you and thanks for sharing.
I have tried something similar before but never shared for the following reasons and personally don't recommend this method because:
1) You cannot run another script until you end the current one (a problem for someone who uses scripts very often).
2) Its not exactly endless because escape key will end it (unfortunately, i use escape a lot).
3) Most importantly the WHILE loop is always running. So even when XYplorer is idle, the process XYplorer.exe in task manager will show constant CPU usage (atleast for me it was about 10%) which will keep jumping up and down until you end this script. Normal CPU usage without the script running is mostly 0%.

I don't mean to patronize, just letting you (and others) know some downsides of this.
Happy scripting. :)

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Can a script run endlessly?

Post by PeterH »

Please excuse me for changing your code - but I couldn't resist.

Code: Select all

while ("<clipboard>" != "<xx>") {
   wait 10;
   $path = tab("get", "path");
   if(    substr($path, 0)     LikeI "Desktop") { Tab("rename", "<curfolder>|<xypath>\icon\Desktop.ico"); }
   elseif(substr($path, 0, 25) LikeI "D:\KPMG\To be transferred") { Tab("rename", "<curfolder>|<xypath>\icon\heart.ico"); }
   elseif(substr($path, 0,  6) LikeI "D:\Doc") { Tab("rename", "<curfolder>|<xypath>\icon\book.ico"); }
   elseif(substr($path, 0, 10) LikeI "D:\Picture") { Tab("rename", "<curfolder>|<xypath>\icon\camera.ico"); }
   elseif(substr($path, 0,  8) LikeI "D:\Movie") { Tab("rename", "<curfolder>|<xypath>\icon\video camera.ico"); }
   elseif(substr($path, 0, 31) LikeI "D:\KPMG\Job\S2a\VPB 30-Jun\VPBS") { Tab("rename", "VPBS2012\<curfolder>|<xypath>\icon\VPB.ico"); }
   elseif(substr($path, 0, 30) LikeI "?:\7Utilities\Video edit magic") { Tab("rename", "<curfolder>|<xypath>\icon\Playboy.ico"); }
   elseif(substr($path, 0, 21) LikeI "D:\KPMG\Job\S2\VPBank") { Tab("rename", "VPB\<curfolder>|<xypath>\icon\VPB.ico"); }
   elseif(substr($path, 0,  8) LikeI "D:\Music") { Tab("rename", "<curfolder>|<xypath>\icon\Music.ico"); }
   elseif(substr($path, 0,  3) LikeI "c:\") { Tab("rename", "<curfolder>|<xypath>\icon\C.ico"); }
   elseif(substr($path, 0,  8) LikeI "?:\1Game") { Tab("rename", "<curfolder>|<xypath>\icon\1Game.ico"); }
   elseif(substr($path, 0, 10) LikeI "?:\2System") { Tab("rename", "<curfolder>|<xypath>\icon\2System.ico"); }
   elseif(substr($path, 0, 10) LikeI "?:\3Office") { Tab("rename", "<curfolder>|<xypath>\icon\3Office.ico"); }
   elseif(substr($path, 0,  9) LikeI "?:\4Media") { Tab("rename", "<curfolder>|<xypath>\icon\4Media.ico"); }
   elseif(substr($path, 0, 10) LikeI "?:\5Design") { Tab("rename", "<curfolder>|<xypath>\icon\5Design.ico"); }
   elseif(substr($path, 0, 12) LikeI "?:\6Internet") { Tab("rename", "<curfolder>|<xypath>\icon\6Internet.ico"); }
   elseif(substr($path, 0, 22) LikeI "?:\7Utilities\XYplorer") { Tab("rename", "<curfolder>|<xypath>\icon\XYplorer.ico"); }
   elseif(substr($path, 0, 33) LikeI "?:\7Utilities\Launcher\AutoHotkey") { Tab("rename", "<curfolder>|<xypath>\icon\AutoHotkey.ico"); }
   elseif(substr($path, 0, 13) LikeI "?:\7Utilities") { Tab("rename", "<curfolder>|<xypath>\icon\7Utilities.ico"); }
   elseif(substr($path, 0,  3) LikeI "k:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico"); }
   elseif(substr($path, 0,  3) LikeI "i:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico"); }
   elseif(substr($path, 0,  3) LikeI "g:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico"); }
   elseif(substr($path, 0,  3) LikeI "h:\") { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico"); }
   elseif(substr($path, 0,  2) LikeI "\\")  { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico"); }
   else                                     { Tab("rename", $path); }
                                   }
Minor change is the form: some alignment. But this is just a question of personal taste. (For me I would have aligned still more "fields", like all "{".)

Other more essential changes:
- calculate tab("get", "path") only once (performance) - it shouldn't change during one pass
- don't supply Tab("rename", ...) explicit with the default tab number Tab("get")
- replace in the last line: elseif(1=1) just by else. That's what "else" is for...

And by the way: I wouldn't like to have such an always running script - not sure if it affects XY in some way?

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

Re: Can a script run endlessly?

Post by serendipity »

PeterH wrote:And by the way: I wouldn't like to have such an always running script - not sure if it affects XY in some way?
Yeah it does, see my post.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Can a script run endlessly?

Post by PeterH »

serendipity wrote:
PeterH wrote:And by the way: I wouldn't like to have such an always running script - not sure if it affects XY in some way?
Yeah it does, see my post.
Ahhh - I was too lazy when writing :oops:
I meant more restrictions than you have mentioned :roll:
Your hint about CPU usage brought me to the idea of optimization :whistle:

OK - just made a small test: XY seems quite usable when then script is running.
(Just selected some folders, viewed a .jpg, ...)

The "wait 10" in the beginning of the loop might be very short - so the high cpu.
I tested with 1000 (= 1sec): too slow
then with 100 (=100msec): seems fast enough.
Will be dependent from the hardware...

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Can a script run endlessly?

Post by binocular222 »

serendipity wrote:Glad this workaround works for you and thanks for sharing.
I have tried something similar before but never shared for the following reasons and personally don't recommend this method because:
1) You cannot run another script until you end the current one (a problem for someone who uses scripts very often).
2) Its not exactly endless because escape key will end it (unfortunately, i use escape a lot).
3) Most importantly the WHILE loop is always running. So even when XYplorer is idle, the process XYplorer.exe in task manager will show constant CPU usage (atleast for me it was about 10%) which will keep jumping up and down until you end this script. Normal CPU usage without the script running is mostly 0%.

I don't mean to patronize, just letting you (and others) know some downsides of this.
Happy scripting. :)
1) I can load other scripts at the same time (either via Scripting > Load selected script file or via shortcut keys), don't know why not work for you.
2) Yes, Esc will end it, fortunately, I only press Esc when renaming file (the script is not terminated then)
3) Yes, there's a trade off between speed and CPU load. wait=10 means super fast folder navigation with high CPU load. Wait=100 maybe more reasonable

PS: Thanks PeterH for script optimization
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

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

Re: Can a script run endlessly?

Post by serendipity »

binocular222 wrote:1) I can load other scripts at the same time (either via Scripting > Load selected script file or via shortcut keys), don't know why not work for you.

Yes, but some of my scripts end your script. To name a few commands: assert, end, input, inputfile. To try it yourself, start your script. then paste this in address bar: input(); and click cancel or simply end 1==1;. Your script ends.
binocular222 wrote: 2) Yes, Esc will end it, fortunately, I only press Esc when renaming file (the script is not terminated then)

Yeah, it depends on each user's work flow. I use it for lot of things: end find files, end thumbs cache, address bar path.
binocular222 wrote: 3) Yes, there's a trade off between speed and CPU load. wait=10 means super fast folder navigation with high CPU load. Wait=100 maybe more reasonable.

Yes, with 100 its lot better.

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Can a script run endlessly?

Post by binocular222 »

I found options to detect USB & CD-ROM & Mapped network drive:

Code: Select all

   elseif(strpos(get("drives", 2), substr($path, 0, 2)) >-1)  { Tab("rename", "<curfolder>|<xypath>\icon\USB.ico"); }
   elseif(strpos(get("drives", 4), substr($path, 0, 2)) >-1)  { Tab("rename", "<curfolder>|<xypath>\icon\Network drive.ico"); }
   elseif(strpos(get("drives", 5), substr($path, 0, 2)) >-1)  { Tab("rename", "<curfolder>|<xypath>\icon\CD-ROM.ico"); }
The value for get("drives", 2) can be:
DRIVE_UNKNOWN = 0
DRIVE_NO_ROOT_DIR = 1
DRIVE_REMOVABLE = 2
DRIVE_FIXED = 3
DRIVE_REMOTE = 4
DRIVE_CDROM = 5
DRIVE_RAMDISK = 6
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Can a script run endlessly?

Post by binocular222 »

Gladly, I can run 3rd party software (Autohotkey's complied script) to tell XYplorer to run the script every 0.5 seconds.
No more "Stopped by Escape key", no longer interfere with other scripts!.
Use scenario C in here http://www.xyplorer.com/xyfc/viewtopic. ... 233#p82412 and Use Autohotkey to complie the exe.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Post Reply