Autohotkey function 'reload' starts an infinite loop that only Taskkill will stop - loops

I'm a relative newbie with Autohotkey but I'm fairly certain this is not supposed to happen. When I use the 'reload' command, an infinite loop begins. As far as I can tell, it is a loop of whatever script invoked it. But it won't stop unless I use Taskkill. Actually, Task Manager might work but I haven't tried it. I'm running Windows 10 21H2. AHK is ver 1.1.34.03. I have tried a diagnostic boot of windows (almost no running apps) and I have also uninstalled and reinstalled AHK. Result remains the same.
I have a script that is only
Reload
msgbox, 4096, , Pausing
(There isn't really a blank line in there. Without it, the editor made it into a single line.)
My assumption was that msgbox would stop any loop. What actually happens is that the program begins to loop, flashing the msgbox momentarily. This goes on and on. I'm unable to stop it with the ahk icon because it also appears and disappears (in the hidden icons tray), so fast that I cannot click it. Only thing I have killed it with is taskkill.
Any suggestions?
-----Paul-----

#SingleInstance is probably not really what you need for this. Normally Reload is used with a hotkey, or a timer that checks if the file has changed, then reloads it. Where I use this I have a script running in the background with many little scripts attached to hotkeys. When I save the script it reloads. Something like:
~^s:: ; pass through hotkey when saving
If winActive(A_Scriptname) {
Reload
}
Return
Make sense?
(Above code is just an idea, which might work depending on what editor you are using)

Related

about what to write on a text.bat in order to overload(not sure about that) my computer to crash

A couple years ago i watched a video on youtube on how to make my computer overload and crash with a 4-line text file made into a .bat because my brother would come to my room and turn the pc on and wake me up every single time, the only problem is, i can in no way remember which exact lines were written on the text and neither i can find the video which indicated those lines. The characeristics of the subject were as follows: it was a .bat file which when you doubled clicked on it it would pop up a command prompt window which would almost immediately render any interaction with the displayed icons on the desktop impossible, and after a 10 second period it would cause the mouse to make more sharp edged movements and after a while of lets say a minute or so it would make my main wallpaper background covering my desktop to get all black and dissappear while slowly incapacitating the whole pc and making it unusable like it wasnt working properly, sort of like turning off the "explorer"..
The only main components that i can bring back to memory are:
#echo off
something like "start"
-//-
-//-
goto "something"
...There wasnt anything like 0% or any other characteristic numbers or codes, only simple words.
I think this is the video you were talking about:
https://www.youtube.com/watch?v=8ASUgrSh5ic
If you make a batch file and inside you start an infinite number of something the computer run out of resources and will kill itself. for example in the video they start cmd.exe an infinite number of times:
#echo off
:here
start cmd
goto here

Open a HTTPS URL under unattended batch control

My problem is that I need to send a shutdown command to my NAS when the UPS tells my home server that there's a power cut. I want to do this by using the (Windows 10 Home) PC server's closedown notification, which can trigger a batch file as part of the closedown event, rather than by using extra hardware. I have determined that opening a particular URL will cause the NAS to close down gracefully.
https://192.168.1.10/get_handler?PAGE=System&OUTER_TAB=tab_shutdown&INNER_TAB=NONE&shutdown_option1=1&command=poweroff&OPERATION=set
The problems seem to be related to trying to bypass dialogues that result in the browser. First it asks for credentials. I can bypass this one by including them in the URL call, like this:
start "" "C:\Program Files\Mozilla Firefox\firefox.exe"
"https://username:password#192.168.1.10/get_handler?PAGE=System&OUTER_TAB=tab_shutdown&INNER_TAB=NONE&shutdown_option1=1&command=poweroff&OPERATION=set"
Now it pops up a dialogue asking me to confirm that I want to log on as "username", again defeating my attempt to make this run unattended (I might be anywhere when a power cut strikes). Is there a way around this? Just FYI, I gave up trying to work with Microsoft Edge as it kept cutting the URL paramaeters into pieces by inserting spaces everywhere. It also kept manually complaining about the security certificate each and every time I tried it. I think I'll have the same confirmation request problems with that browser too. So I need a way to surpress all these dialogues and just make the URL call. Any thoughts please?
The lack of any answers after this question had been posted over 24 hours told me that this was a tricky one & kept me trying. After I tried everything I could think of to solve it with Window's own tool set and finding nothing worked I realised I'd need something additional to solve the problem. Finally I gave up and installed the free Autohotkey and wrote a script to do the entire job, including handling the dialogue boxes. Once it was all working I made an executable from the script for convenience so I can easily call it from the close down batch (.bat) file. This solution works. Here is the script for anyone who needs to solve a similar problem:
> runWait, C:\Program Files\Mozilla Firefox\firefox.exe
> https://admin:password#192.168.1.10/get_handler?PAGE=System&OUTER_TAB=tab_shutdown&INNER_TAB=NONE&shutdown_option1=1&command=poweroff&OPERATION=set
>
> #Persistent SetTimer, MsgBoxCheck, 3000 return
>
> MsgBoxCheck: If WinExist("ahk_class MozillaDialogClass") {
> SetControlDelay -1 ControlClick, , Confirm, SetTimer,
> MsgBoxCheck, off exitApp } else { SetTimer, MsgBoxCheck, off
> MsgBox, 4, , Debug - No Confirm Window Found. exitApp } return
Please note, to make this work with non-Mozilla programs you will need to change the ahk_class parameter. I suggest you use Windows Spy to determine the class of window you want to close. You will also need to change 'Confirm' to whatever your window's title might be.

Is there a way to hide the desktop items using applescript WITHOUT quitting finder?

Is there an applescript command that will make the desktop items disappear (without trashing them, moving them or quitting finder)?
Thanks,
Eric
[I'm ] Not yet on Mavericks (how embarrassing), but this works on 10.6.8:
do shell script "defaults write com.apple.finder CreateDesktop -bool false"
quit application "Finder"
delay 0.5
tell application "Finder" to activate
--and of course change false to true to re-activate the icon visibility
[edit] This solution works on Mavericks as well, and I assumed the request for a solution "without quitting finder" does not mean temporarily quitting the Finder, which is a necessary step.

How to refactor a Windows batch script littered with GOTOs?

I have to maintain a batch script of about 3500 lines littered with GOTO. Seems that the original "developer" hasn't heard of this famous paper and modular programming.
What the script does?
The script deals with the (silent) installation/uninstallation/reinstallation of several programs using different options. It could be split in several files that deal with each program in part. The problem is that if you're trying to take a part in another file that part will still GOTO another section that needs to be in the original script.
Refactoring?
Normally you wouldn't do a refactoring without having automated tests (so you can be sure you didn't break anything), but I don't know how to do it. There's no testing framework for that.
Partial Solution
I have come up with a partial "solution" that is some kind of adaptation of characterization tests (from Working Effectively with Legacy Code by Michael Feathers) and approval tests:
- create another script: test.py that replaces all commands (like copy or msiexec) with echo,
- redirect the output to a text file (good.txt),
- change the original batch script,
- run the test.py script again and save the output to another text file (current.txt),
- diff good.txt and current.txt -> if there are no differences then I didn't break anything, but if they are different I need to check if I broke something.
Problem with partial solution
How can I capture and replace all the commands? I could make a list of commands to replace, but there are also a lot of string concatenations to get the name and path of the program to be installed.
CMD level capture/hook?
Is there any way I can hook into the command line interpreter (CMD.exe) so I can replace on the fly all the calls to installers with echo?
Other suggestions?
Do I approach the problem in the wrong way? Can I do it better somehow? Do you have some advice I could use?
You could replace all COPY, DEL or CALL with %COPY%, %DEL% ,...
So you can use the same file for production and also for the tests.
#echo off
if not defined UNITTEST (
set "COPY=COPY"
set "DEL=DEL"
set "CALL=CALL"
)
%COPY% src dest
%DEL% somefile.txt
%CALL% installer.exe
And from your unittest.bat, you could start it via
#echo off
set "COPY=>>trace.log ECHO COPY"
set "DEL=>>trace.log ECHO DEL"
set "CALL=>>trace.log CALL ECHO "
del trace.log
set "unittest=Active"
call production.bat
fc good.txt trace.log
I'm not an expert in Batch, but I have done my fair share of it. With that said, I can offer a few tips.
Forget trying to do it all at once. Batch is very hard to debug. Echoing out to a log file helps a lot, but it will not capture everything you need if something goes wrong.
Work on breaking out the exe and msiexec calls into self-contained scripts. It is much easier to test the small script for the functionality you desire. Once you have that working, it is simple to call that script from the "Master" script.
Establish a good protocol for passing args to, and return codes from the smaller scripts. If there are common settings needed to be used for all the scripts consider using a central settings file.
GOTOs are not the devil, unless they pass control all over the place. Normally there are two good reasons that I know of to use GOTO’s.
Skip past a block of code that does not need to run.
To SET values into variables. Note there is a bug that can prevent variables from having their value set from within an 'IF' statement block. That little bug caused a big headache for me at one time.
Calls to a label might be better option at times.
Depending on how far back the legacy support is required, consider using Powershell when possible. The power and debugging capabilities of Powershell far out way the benefits of simple scripting of Batch. Which at 3500 lines simplicity has already been lost. You are already looking at Python, so maybe that could be used instead.
If you need a break point, use Pause. ECHO all the settings you need to examine right before the pause. This is as close to a break point I have found for batch.
Echo the command you intend to run to a log file and actually run it.
Write small verification scripts to be used independently or with the “Master” script to confirm you are getting the results you are expecting.
Use the right tool for the job. I like to use EditPadPro, RegexBuddy, and BeyondCompare for batch editing and comparing differences. There free tools that can be used too NotePad++ and Windiff. Making many edits in a file of that size is best handled by a good editor. IE inserting an echo at the beginning of a line that calls a cmd.exe.
Remember it is scripting not programming. While there is a lot of overlap of the two, the same exact approach to a problem may not be viable between the two.
Always make a backup copy of the scripts as a whole before mucking around. A fallback position is greatly appreciated when there is one small bug that you can’t find.
If it ain't broke... well you wouldn't be working on it if everything was working just fine.
Always test changes. And when you are done test it again. After that have someone else test it.
Just my .02. I’m sure someone else can chime in with more advanced advice. My knowledge on Batch has been acquired from the school of hard knocks, supplemented by ss64.com

Help getting Conky to work with LXDE & PCManFM

As some of you might be aware, there's a bug with either Conky or PCManFM (the desktop manager aspect of it) that makes the Conky window disappear in one of these situations:
Setting "own_window_type override" in .conkyrc (the usual configuration for Nautilus) flat out doesn't show the Conky window at all.
Setting "own_window_type desktop" in .conkyrc shows the Conky window, but it disappears if you click on the desktop.
Setting "own_window_type normal" in .conkyrc shows the Conky window, it doesn't disappear when clicking on the desktop, BUT... it disappears if you use LXPanel's "Minimize all windows" plugin ("Show desktop" equivalent). This happens if using the keyboard shortcut as well.
There are some workarounds to this, such as deactivating PCManFM's desktop management (and using feh for setting the wallpaper and, presumably, iDesk for the icons) or using another file manager altogether, but I've come to notice these are all subpar solutions. I really like PCManFM and I really like Conky. What's a guy to do? :-/
So I dug in LXPanel's source and found a relevant piece of code that might house my fix. The file is src/plugins/wincmd.c .
The idea I have is that when the "Minimize all windows" button is clicked, all windows (class) names would be compared to a static string "Conky" and if it matches, it simply wouldn't minimize that window. Simple, and it should work. But the problem is it has been a few years since I've touched any C code and I haven't really played with Xlib that much.
So far, I have done the following changes:
19a20
> #include <string.h>
77a79,82
>
> /* Getting window's class name */
> XClassHint class;
> XGetClassHint(GDK_DISPLAY(), client_list[i], &class);
81c88
< if (((task_desktop == -1) || (task_desktop == current_desktop))
---
> if (((task_desktop == -1) || (task_desktop == current_desktop) || strcmp(class.res_name, "Conky") != 0)
This compiles correctly, but when I run the new lxpanel and click on "Minimize all windows", Conky still disappears as before.
If someone can look into this file and see if my changes make sense, I'd be very grateful.
Thanks SO! :)
This is still impossible afaik. I made this quirky workaround. Just use the own_window_type normal mode where you cannot use the Win+D desktop shortcut, and fix conky on show desktop.
First I thought, just remapping the conky window will do the trick. But I found out that as long as the desktop is focussed, nothing will get drawn over it. Then I thought, just kill and restart conky. But while in show desktop mode, it will get created in the background.
So in order to get conky back:
The desktop must be unfocussed
restart conky or remap the window
Install xdotool; apt-get install xdotool
Create a script, e.g. /usr/local/bin/conky-remap.sh:
#!/usr/bin/env bash
zenity --info --text "Remapping Conky..." &
pid=$!
sleep 0.3
kill $pid
xdotool windowmap `xdotool search --classname 'conky'`
Edit ~/.config/openbox/lxde-rc.xml (or wherever shortcuts are in your distro).
Find:
<keybind key="W-d">
<action name="ToggleShowDesktop"/>
</keybind>
Change to:
<keybind key="W-d">
<action name="ToggleShowDesktop"/>
<action name="Execute">
<execute>conky-remap.sh</execute>
</action>
</keybind>
Activate the new keyboard shortcut configuration:
openbox --reconfigure
Press Win+D. You'll see a flash and conky is back!
This quirky solution is the only solution in the universe according to a week of google, so improving on it is encouraged.
Explanation:
zenity --info --text "Remapping Conky..." &
We need to create a random window to unfocus the desktop. Conky itself doesn't work. :(
We also need the script to continue, so use &
pid=$!
Get the pid of that last process
sleep 0.3
Sleep any less (slow HTPC) and the window is killed before it is created.
kill $pid
Kill the window as soon as it has performed its function.
xdotool windowmap xdotool search --classname 'conky'
Now finally, we can raise (remap) conky.
Quirks:
Desktop is not focussed, so you cannot use the arrows to navigate desktop icons
Can we refocus the desktop? As long as the windows are not programatically minimized, conky will stay put.
300 ms delay, and a flash of the dialog box.
Pressing Win+D for a second time to go back ('unshow desktop')
We would need a way to detect if the desktop is already in show-desktop-mode, in which case the script should exit 0
The simplest solution working perfectly with Openbox (and should work with other WMs too):
In your .conkyrc file:
own_window yes
own_window_type desktop
own_window_class Conky
The "desktop" window type prevents conky from being minimized when show desktop is executed. Moreover your windows will be restored by executing "show desktop" again
Install xdotool package (it's very lightweight, don't worry), in terminal emulator run the following:
xdotool search --class "Pcmanfm" behave %# focus windowraise $(xdotool search --class "Conky")
and make sure your conky doesn't hide when you focus a desktop
If everything is okay, interrupt xdotool by pressing Ctrl+C in your terminal and place the code above in autostart. It probably won't start right away, so you have to create a simple script (e.g. "show_conky.sh") containing
#!/bin/sh
sleep 5
xdotool search --class "Pcmanfm" behave %# focus windowraise $(xdotool search --class "Conky")
The minimal required sleep time should be defined accordingly to your system. To be sure, conky won't hide at the first launch before "show_conky.sh" is executed, you can also create another script with xdotool focus windowraise $(xdotool search --class "Conky") command and proper sleep time in it (or try to add it between "sleep" and "xdotool behave" commands it "show_conky.sh").
Restart your Xorg server. Enjoy! :)
I personally advice you using SpaceFM for managing desktop icons. It allows you to set margins for the desktop area, so conky never covers icons. Pcmanfm looks more friendly as an explorer, but you can use both of them: spacefm can open directories located on the desktop with other application. Its desktop behavior is identical to pcmanfm, so you should simply replace "Pcmanfm" in the code above with the "Spacefm"
This is an old "question", but it came up fairly high in Google hits when I searched for "Conky LXDE", because I couldn't get Conky to work in Lubuntu. But there is no need to hack LXPanel!
After MUCH searching and false trials and trails, I found a .conkyrc file in the "PCLinuxOS-Forums" that Just Worked™. It has proved to be an excellent base to tweak from on Lubuntu 11.10.
I now use the same set up on Linux Mint 13 LTS "Maya" Xfce (based on Ubuntu 12.04), and it continues to meet my (very simple) Conky requirements. The current form of my .conkyrc file is saved in a Gist if anyone is thinks it might help them. (The original source link for the forum is commented into the rc file.)
The solution is:
NetWMState nws;
guint task_desktop = get_net_wm_desktop(client_list[i]);
get_net_wm_state(client_list[i], &nws);
if (((task_desktop == -1) || (task_desktop == current_desktop))
&& ( ( ! nwwt.dock) && ( ! nwwt.desktop) && ( ! nwwt.splash)
&& ( ! nws.skip_pager) )
To build on Redsandro's solution:
Follow Redsandro's advice and sudo apt-get install xdotool and create some conky-remap.sh (or whatever) script in your /usr/local/bin (or wherever) directory. Also, install wmctrl if you do not already have it. We will be using this to make Conky active (I know this is different than mapping, but it behaves as I expect it to, so I'm not worried.)
run wmctrl -l to list all windows while Conky is running and take note of Conky's title (mine was something like Conky (<my-machine-name>).
While you're at it create another file in which we will save the current state of the desktop (0=not show-desktop-mode, 1=show-desktop-mode... almost) called something like .conky_desktop_state. I have put this into /usr/local/bin as well. Now back to conky-remap.sh:
#!/usr/bin/env bash
state=$(cat /usr/local/bin/.conky_desktop_state)
dt=$(xdotool get_desktop)
if (( $state == 1 )) ; then
echo 0 > /usr/local/bin/.conky_desktop_state
wmctrl -a "Conky (<my-machine-name>)"
else
echo 1 > /usr/local/bin/.conky_desktop_state
wmctrl -a "Conky (<my-machine-name>)"
xdotool search --desktop $dt . windowmap %#
fi
wmctrl's command to activate a window doesn't care about the desktop being in focus or not, so we don't need to sleep or flash a dialog window to the screen. $state and $dt are the variables which hold the current state of the desktop (again, kind of) and the current desktop id number.
xdotool search --desktop $dt . windowmap %# maps all of the windows from the current desktop back to the screen and leaves other desktops alone.
Edit ~/.config/openbox/lubuntu-rc.xml or wherever to execute the script when Win+D is pressed.
<keybind key="W-d">
<action name="ToggleShowDesktop"/>
<action name="Execute">
<command>conky-remap.sh</command>
</action>
</keybind>
openbox --reconfigure to refresh keybindings and you should be good to go.
With Win-D to show desktop, the desktop is focused, so you may use arrow keys to navigate desktop icons.
This solution does have its own set of quirks:
A second pressing of Win+D brings up all windows of the current desktop, not just the ones which were active before showing the desktop.
A toggle is stored in .conky_desktop_state, but it doesn't really match up with show-desktop-mode since it only toggles when the script is executed. This means it is possible to have one desktop shown and the other not. In this case, the desktop will be shown briefly and then all windows will be brought active. One more pressing Win+D will show the desktop.
Sometimes the whole thing fails and Conky disappears with all of the other windows, albeit rarely. Just cat /usr/.../.conky_desktop_state and toggle the opposite value in (ie- echo 1 > /usr/.../.conky_desktop_state)
Although most of the time the last active window before Win+D is on top after a second Win+D, it is not necessarily active, meaning you may have to click in the window or Alt+Tab to it to start typing in it. This generally is the case with terminal windows.
Ideally, we would be able to look at all active (mapped) windows on each desktop individually and execute one or the other command based on if Conky is the only active window, but I wasn't smart (dedicated) enough to do that, so here is my contribution. Please build on it and reply.
Simplifying #imiric's answer:
Openbox RC.xml (e.g. ~/.config/openbox/lxde-rc.xml):
<keybind key="W-d">
<action name="ToggleShowDesktop"/>
<action name="Execute">
<command>wmctrl -a "Conky (hostname)"</command>
</action>
</keybind>
~/.conkyrc:
own_window yes
own_window_type normal
own_window_class conky-semi
own_window_transparent yes
own_window_hints undecorated,sticky,skip_taskbar,skip_pager
own_window_argb_visual yes
got success with conky transparency in LXDE on debian squeezy, with pcmanfm running.
In the conky config file (i.e. .conkyrc) I removed all 'own_window' lines, except the following:
own_window yes
own_window_class conky
own_window_transparent yes
own_window_hints undecorated,below,skip_taskbar,sticky,skip_pager
Adding again some often used lines caused transparency to fail, or conky wouldn't start at all. Very strange...

Resources