Calculator does not launch reliably with my code. Why?

Get help with using AutoHotkey and its commands and hotkeys
reverberation
Posts: 118
Joined: 13 Dec 2015, 20:48

Calculator does not launch reliably with my code. Why?

10 Dec 2018, 22:19

As per code below. I am on the latest Win 10 build and calculator doesn't always launch based on this code. Any idea what can be done to ensure it launches every single time with Win+C?

Code: Select all

#c::
IfWinExist Calculator
WinActivate
Else
Run C:\Windows\System32\calc.exe
return
Hellbent
Posts: 572
Joined: 23 Sep 2017, 13:34

Re: Calculator does not launch reliably with my code. Why?

11 Dec 2018, 04:24

Try this:

Code: Select all

#c:: 
If(WinExist("ahk_class CalcFrame"))
	WinActivate,
else
	Run C:\Windows\System32\calc.exe
return
GEV
Posts: 771
Joined: 25 Feb 2014, 00:50

Re: Calculator does not launch reliably with my code. Why?

12 Dec 2018, 04:42

It seems that the first (child) window of the minimized UWP app is an invisible window of class Windows.UI.Core.CoreWindow that can't be activated.
Try

Code: Select all

#c::
IfWinExist Calculator ahk_class ApplicationFrameWindow 
	WinActivate
else
{
    Run calc
    WinWait, Calculator
    WinActivate
}
return
reverberation
Posts: 118
Joined: 13 Dec 2015, 20:48

Re: Calculator does not launch reliably with my code. Why?

23 Dec 2018, 23:44

Hi @GEV,

Just tried your code. It also does not launch Calculator all the time. No idea why.

Any advice??
teadrinker
Posts: 756
Joined: 29 Mar 2015, 09:41
Contact:

Re: Calculator does not launch reliably with my code. Why?

24 Dec 2018, 06:50

Try:

Code: Select all

#c::
   if WinExist("Calculator ahk_exe ApplicationFrameHost.exe")
      WinActivate
   else
      Run calc
   Return
User avatar
Scr1pter
Posts: 1069
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Calculator does not launch reliably with my code. Why?

24 Dec 2018, 07:14

How much is "not all the time" approximately?

Tested under Windows 7 - works fine:

Code: Select all

#c:: ; Win+C
if not WinExist("ahk_class CalcFrame")
{
  Run, calc.exe
}
else
{
  WinActivate, ahk_class CalcFrame
}
return
Regards
Please use code tags when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602 - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.29.00
garry
Posts: 1824
Joined: 22 Dec 2013, 12:50

Re: Calculator does not launch reliably with my code. Why?

24 Dec 2018, 07:47

last try , in WIN-10 , not depending language ( Calculator / Rechner )

Code: Select all

sc:="ahk_exe ApplicationFrameHost.exe"

#c::
   if WinExist(sc)
      WinActivate
   else
      Run calc
   Return
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: Calculator does not launch reliably with my code. Why?

25 Dec 2018, 00:59

@Gary, that won't work, the only difference between the vast majority of the Win 10 UWP apps is the actual Window Title, they all share a class and exe i.e. "ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe", meaning without the title component things like the settings app (and many others) will get picked up as being calculator.

I use this, had no problems with it in windows 10. So the hotkey is different (shouldn't matter) and I use ShellRun because my main script runs as admin so it just forces calc to be run without admin privileges. (i.e. using Run in a script with admin privileges causes the run program to be run as admin as well). ShellRun is essentially a function from the AHK installer, so you can copy it from there if you need it or just use Run, %A_Windir%\system32\calc.exe instead.

Code: Select all

NumpadMult & NumpadSub Up::
	If WinExist("Calculator ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe")
		WinActivate
	Else
		ShellRun(A_WinDir "\system32\calc.exe")
	Return
Do you have superfetch turned on ? (if you don't know what it is or have never turned it off then the answer is yes) and perhaps DetectHiddenWindows set On elsewhere in your script ?. Reason being superfetch starts frequently used programs in the background so they can appear to run quicker, perhaps the times your script doesn't work this is why as the window does exist but is hidden ? this should only matter if you also have DetectHiddenWindows On but still worth a look if it continues. (you'd have to check at a time when it plays up by looking for existing entries in task manager).
garry
Posts: 1824
Joined: 22 Dec 2013, 12:50

Re: Calculator does not launch reliably with my code. Why?

25 Dec 2018, 05:33

thank you Noesis , I was wrong , just a little confused , how to avoid to use the word 'Calculator' ?

Code: Select all

;- ( have no problem with notepad.exe  , start /minimize/show )
;- I need the german word 'Rechner' instead of 'Calculator' or maybe > how to use PID1
;- I start calc.exe but calculator.exe is running 
;--------------------------------------------------------------------------------
; - used german word 'Rechner'  in this case , replace with 'Calculator' for english
sc:="Rechner ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe"
#c::
xx=calculator.exe
process,exist,%xx%
pid:=errorlevel
if pid<>0
   {
   winactivate,%sc% 
   ;winactivate,ahk_class ahk_pid %PID1%  ;- NOT OK
   }
else
  run,calc,,,pid1
return
garry
Posts: 1824
Joined: 22 Dec 2013, 12:50

Re: Calculator does not launch reliably with my code. Why?

25 Dec 2018, 09:38

@just me , I think the problem is the same, want not replace 'Calculator' with 'Rechner' ( depending language / ok is not problem , just use for german 'Rechner' )
run,calc is not the problem , only when it's minimized then > winactivate maybe shoul'd work regardless of language
reverberation
Posts: 118
Joined: 13 Dec 2015, 20:48

Re: Calculator does not launch reliably with my code. Why?

26 Jan 2019, 22:44

Hi all,

I have tried both the ones in this thread and the other thread - and it still only launches approx. 4 out of 10 times only. Can't find a predictable pattern to it. Anyone else wants to have a go?

Code: Select all

sc:="Calculator ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe"
#c::
xx=calculator.exe
process,exist,%xx%
pid:=errorlevel
if pid<>0
   {
   winactivate,%sc% 
   ;winactivate,ahk_class ahk_pid %PID1%  ;- NOT OK
   }
else
  run,calc,,,pid1
return

#x::	; <-- Open/Activate/Minimize Windows Calculator
{
	if WinExist("Calculator ahk_class CalcFrame") or WinExist("Calculator ahk_class ApplicationFrameWindow")
		if WinActive()
			WinMinimize
		else
			WinActivate
	else
		Run calc.exe
	return
}

#numpad1::
	If WinExist("Calculator ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe")
		WinActivate
	Else
		(A_WinDir "\system32\calc.exe")
	Return
garry
Posts: 1824
Joined: 22 Dec 2013, 12:50

Re: Calculator does not launch reliably with my code. Why?

02 Feb 2019, 05:59

have no solution
example below worked for me , german > Rechner ( I run calc > calculator.exe is running (WIN-10 ) )

Code: Select all

;--- example german : Rechner / calculator.exe
sc1:="Rechner ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe"
;sc1:="Calculator ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe"
$F9::
xa=calculator.exe
process,exist,%xa%
pid1:=errorlevel
if pid1<>0
 winactivate,%sc1%
else
 run,calc
return
safetycar
Posts: 125
Joined: 12 Aug 2017, 04:27

Re: Calculator does not launch reliably with my code. Why?

02 Feb 2019, 07:26

One of the problems with the reliability of the script can be within the hotkeys.

If you try to launch a hotkey when the active window has admin rights, but your script doesn't, it won't get fired. (It doesn't matter that the hotkey is unrelated to the active window, it will be blocked anyway.)
reverberation
Posts: 118
Joined: 13 Dec 2015, 20:48

Re: Calculator does not launch reliably with my code. Why?

03 Feb 2019, 23:17

based on this theory, it should launch 100% of the time if my script is granted admin rights? I just tested it - still the same issue despite being given admin rights.
safetycar
Posts: 125
Joined: 12 Aug 2017, 04:27

Re: Calculator does not launch reliably with my code. Why?

04 Feb 2019, 10:02

I've tried to reproduce it and I could. Seems to happen when you minimize manually instead of just activating something over it.

What I've seen is that while the calculator is that there seems to be at least 2 windows created by the calculator.

I could make the calculator show up by activating the bottom-most window instead of the top-most one.

Now the trick is WinActivateBottom activates when it's minimized, but WinActivate is still needed when it's not minimized, just overlayed by other window.

So I'd just put both commands one after another to make sure it works in both cases.

(What I said about admin rights still applies.)
DRocks
Posts: 443
Joined: 08 May 2018, 10:20

Re: Calculator does not launch reliably with my code. Why?

05 Feb 2019, 08:17

Hello, I've struggled with that too.

Since I use this code it always works for me on all 3 of my win10 computers.
Just change the titles for your language mine is french currently

Code: Select all

	{ ; Calculatrice (F8)
		F8:: 
		DetectHiddenWindows, on
		If WinExist("Calculatrice ahk_exe ApplicationFrameHost.exe")
		{
			If WinActive("Calculatrice")
				;WinMinimize
				WinKill
			else 
			{		
				WinActivateBottom, Calculatrice ahk_exe ApplicationFrameHost.exe
				WinActivate, Calculatrice ahk_exe ApplicationFrameHost.exe
			}
		}
		else
		{			
			Run, Calc.exe
			WinWait, Calculatrice,,1
			WinMove, Calculatrice,, A_ScreenWidth-320, (A_ScreenHeight/A_ScreenHeight+250), 320, 500  ; Minimum size x320 y500
			WinSet, AlwaysOnTop
		}		
		return
	}
safetycar
Posts: 125
Joined: 12 Aug 2017, 04:27

Re: Calculator does not launch reliably with my code. Why?

05 Feb 2019, 10:05

This simplified code works for me (transaled to my language).

Code: Select all

F8::
	CalcTitle := "Calculatrice ahk_exe ApplicationFrameHost.exe"
	if WinExist(CalcTitle) {
		WinActivateBottom, %CalcTitle%
		WinActivate, %CalcTitle%
	} else {
		Run, Calc.exe
	}
return
garry
Posts: 1824
Joined: 22 Dec 2013, 12:50

Re: Calculator does not launch reliably with my code. Why?

05 Feb 2019, 10:28

@safetycar, thank you , this works fine
german > Rechner

Code: Select all

F8::
	CalcTitle := "Rechner ahk_exe ApplicationFrameHost.exe"
	if WinExist(CalcTitle) {
		;WinActivateBottom, %CalcTitle%  ;- is this needed ?
		WinActivate, %CalcTitle%
	} else {
		Run, Calc.exe
	}
return

Return to “Ask For Help”

Who is online

Users browsing this forum: aifritz, bennybroseph, Google [Bot], Micahs, momo007, Mushie, tmplinshi and 227 guests