Page 1 of 2

Calculator does not launch reliably with my code. Why?

Posted: 10 Dec 2018, 22:19
by reverberation
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

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

Posted: 11 Dec 2018, 04:24
by Hellbent
Try this:

Code: Select all

#c:: 
If(WinExist("ahk_class CalcFrame"))
	WinActivate,
else
	Run C:\Windows\System32\calc.exe
return

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

Posted: 12 Dec 2018, 04:42
by GEV
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

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

Posted: 23 Dec 2018, 23:44
by reverberation
Hi @GEV,

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

Any advice??

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

Posted: 24 Dec 2018, 06:50
by teadrinker
Try:

Code: Select all

#c::
   if WinExist("Calculator ahk_exe ApplicationFrameHost.exe")
      WinActivate
   else
      Run calc
   Return

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

Posted: 24 Dec 2018, 07:14
by Scr1pter
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

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

Posted: 24 Dec 2018, 07:47
by garry
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

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

Posted: 25 Dec 2018, 00:59
by Noesis
@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).

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

Posted: 25 Dec 2018, 05:33
by garry
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

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

Posted: 25 Dec 2018, 06:52
by just me

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

Posted: 25 Dec 2018, 09:38
by garry
@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

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

Posted: 26 Jan 2019, 22:44
by reverberation
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

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

Posted: 01 Feb 2019, 21:14
by reverberation
anyone knows?

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

Posted: 02 Feb 2019, 05:59
by garry
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

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

Posted: 02 Feb 2019, 07:26
by safetycar
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.)

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

Posted: 03 Feb 2019, 23:17
by reverberation
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.

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

Posted: 04 Feb 2019, 10:02
by safetycar
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.)

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

Posted: 05 Feb 2019, 08:17
by DRocks
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
	}

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

Posted: 05 Feb 2019, 10:05
by safetycar
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

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

Posted: 05 Feb 2019, 10:28
by garry
@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