Why this simple script needs a reload first to work? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
natsume
Posts: 6
Joined: 04 Aug 2018, 21:16

Why this simple script needs a reload first to work?

04 Aug 2018, 21:26

I put this script in the startup folder of Windows.
After a fresh reboot, it doesn't work, but if I reload it, the script works flawlessly.
It works perfectly on windows 10, and this problem appears after I install Windows 7 (x64, with latest autohotkey version).
Can you tell me how to fix this?
I think one way to work around that is add some line to make the script reloads itself once?

Sorry for my bad English.
Thanks!

Code: Select all

#If MouseIsOver("ahk_exe SumatraPDF.exe")
WheelUp::send +{Space}
WheelDown::send {Space}
F12::send {F1}
^y::send {F3}
#If

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Why this simple script needs a reload first to work?

04 Aug 2018, 22:32

Try adding this at the top of the script:
WinWait, ahk_exe SumatraPDF.exe
natsume
Posts: 6
Joined: 04 Aug 2018, 21:16

Re: Why this simple script needs a reload first to work?

04 Aug 2018, 23:33

Xtra wrote:Try adding this at the top of the script:
WinWait, ahk_exe SumatraPDF.exe
It still doesn't work.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Why this simple script needs a reload first to work?

04 Aug 2018, 23:50

- You could have one script that sleeps for a long time, then starts another script.
- I don't know if there is a problem that if a script starts too early, then any hotkeys aren't registered. Did all of the hotkeys fail?
- Also, perhaps you have hotkeys from different scripts interfering with each other.
- If you reload too soon, does that also fail? You can set up for one reload to happen by triggering Reload conditionality, based on the command line string. If it contains '/restart' then the script was restarted at least once.

Code: Select all

vCmdLn := DllCall("kernel32\GetCommandLine", Str)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
natsume
Posts: 6
Joined: 04 Aug 2018, 21:16

Re: Why this simple script needs a reload first to work?

05 Aug 2018, 01:16

- Yes, all of the hotkeys fail.
I have another script also starting with Windows, and it's working fine. These 2 scripts have differrent hotkeys.

- I remove the Sumatra script from startup folder, and it doesn't need a reload to work. So the problem maybe because the script was made to start during system boot.
I really don't know how to deal with this problem. When I still on Windows 10, it just works as expected.
Should I concede defeat and just make a shortcut for the script in the taskbar, then click on it before I open SumatraPDF?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Why this simple script needs a reload first to work?

05 Aug 2018, 01:27

- Possibly you could add it to the Windows Task Scheduler (which I haven't used in years) or make it reopen itself as admin.

Code: Select all

if !A_IsAdmin
	Run, % "*RunAs " (A_IsCompiled ? "" : A_AhkPath " ") """" A_ScriptFullPath """"
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
natsume
Posts: 6
Joined: 04 Aug 2018, 21:16

Re: Why this simple script needs a reload first to work?

05 Aug 2018, 01:57

This one returns 1.

Code: Select all

MsgBox % DllCall("Shell32.dll\IsUserAnAdmin")
Does the code needs to look like this?

Code: Select all

#If MouseIsOver("ahk_exe SumatraPDF.exe")
WheelUp::send +{Space}
WheelDown::send {Space}
F12::send {F1}
#If

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}

if (A_IsAdmin==1)
	Run, % "*RunAs " (A_IsCompiled ? "" : A_AhkPath " ") """" A_ScriptFullPath """"
It still needs a reload.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Why this simple script needs a reload first to work?

05 Aug 2018, 02:19

- Put the is admin check at the top of the script.
- The issue is whether the script is run as admin, rather than whether the user is admin.
- I've outlined various things to try. There may be other ideas that people can think of.
- So it works on Windows 10, but not Windows 7.
- If you double-click the script to run it, does that work OK?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
natsume
Posts: 6
Joined: 04 Aug 2018, 21:16

Re: Why this simple script needs a reload first to work?

05 Aug 2018, 02:38

- Put the check at the top and it's still not working (if starts at boot).
- The script runs OK if I double-click it.
- Yes, it works on Windows 10 (with boot too), but doesn't work on windows 7 if I make it start with Windows.
Double-click works on both OSes.
natsume
Posts: 6
Joined: 04 Aug 2018, 21:16

Re: Why this simple script needs a reload first to work?  Topic is solved

06 Aug 2018, 15:31

I answer this thread anyway, perhaps this will help someone else in the future.
The problem can be solved by putting elevated shortcut of the script to the startup folder, as suggested here.
It's interesting that in my case, I have another script booting with windows and don't have to use this trick to make it work.
It happens on windows 7 and works fine on windows 10, and the script does run - it has a corresponding autohotkey.exe process in Task Manager.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, doanmvu, uchihito, zvit and 285 guests