(2015-06-12, 04:16)Supremesoul Wrote: (2015-06-11, 22:19)Tacticious Wrote: That line kills the script. It theoretically never triggers when below the "While", because nothing triggers the exiting of the "While" loop. Your script, with the placement of that line at the top, kills itself immediately upon running. Your placement in the second code block is exactly where it should be: after the loop.
That's perfect explanation, Thank you a ton for showing me what it was doing and why it failed if i had it first. EDIT: Would adding Return to the end of the bracket but before the "; force shut down..." cause it to work ahead of the loop??
No problem at all. Anything after a semicolon is a comment. As soon as "#z::ExitApp" is ran, the application will close without evaluating any further code. That code just needs to be exactly where you want it to close, which in this case, is if it somehow exits the never-ending loop.
(2015-06-12, 04:16)Supremesoul Wrote: Quote:Code:
Code:
var1 = 0
#SingleInstance force
While True
{
ifWinExist , Sponsored session
{
WinClose('Sponsored session')
}
sleep, 200
}
I had to change this a little bit so that AHK (From here https://www.autohotkey.com/docs/commands/WinClose.htm) could read it. changed it too whats below. With that being said. I created a .txt doc, called it Sponsored Session. and when the AHK is running it closes the notepad instantly. so that works for that. Ill test it with the actual sponsored message from TV a little later.
Ahh, I didn't know you were working with AHK; I thought you were working with AutoIt. Your code is definitely solid, though.
(2015-06-12, 04:16)Supremesoul Wrote: Quote:While True
{
ifWinExist , Sponsored session
{
WinClose, Sponsored session
}
sleep, 200
Now i know what you mean by increasing the sleep time but where would i put it so that AHK sleeps the code for 10ms. you are correct in thinking that it does NOT have to be instant....change it from 200 to 1000? or place it else where so that I'm not using 12% of my cpu just with just this script?
Presently, that "sleep, 200" is causing it to sleep for 200 milliseconds (the number itself is just defining the sleep duration in milliseconds). If you changed that to 1000, you'd wait a second between each iteration of the loop, and save 5x more CPU usage on that script compared to what my 200ms sleep is saving.
(2015-06-12, 04:16)Supremesoul Wrote: Quote:One thing that could prove useful in your situation(s) as well: Set the title it searches for to an array. Put in as many titles as you'd like and modify the code to loop through the array.
If I knew what window you were wanting to maintain focus, I'd also write some code out for you that focused that window if it wasn't already. I don't, so I'm just going to suggest that you throw a focus check in right before the sleep.
Based on this code:
Code:
ifWinExist , Steam
WinActivate
}
It looks like it's steam. That code should check to see if Steam actually has focus prior to activating it. If it's got focus, you don't want something trying to give it focus over and over. That's wasted CPU.
With this you are correct, I have Steam running usually in big picture mode when my son needs me to remote in, i do what i gotta do and get out, sometimes its longer then 1 min of work, thus causing all this chaos and the sponsored messaged popup. would i put the sleep command (to reduce CPU usage) somewhere in here? Or somewhere mentioned above?
Now I'm no noobie when it comes to PC's, but when it comes to programming things or writing code I simply suck at it and cant seem to grasp whats going on in my head even with trying my hardest.
So when we talk about making the code look to see if it has focus before 'winactavte', and creating a array of windows, im not sure at all what you would be talking about but im willing to learn and write code with you if youd like to teach me a little bit.
Thanks for all the help already and the explinations.
SupremeSoul
So, one great thing about arrays is that it's just a variable of variables. I'm not familiar with AutoHotkey, so I'll post in AutoIt (if you download the AutoIt SCITE Editor, you can copy and paste the code I provided). If, for example, you wanted two windows to be searched, you'd first set an array of the names of those windows:
Code:
local $windows[2]
$windows[0] = 'Sponsored session'
$windows[1] = 'Skype'
That's simply defining our two windows, which you could certainly have as many as you want in theory. Next, instead of the code above, you'd do something like this:
Code:
While True
{
For $i = 0 to 1 step +1
If WinExists($windows[$i]) Then
WinClose($windows[$i])
EndIf
Next
sleep, 200
}
That code is another loop within our loop. What it does is defines $i, and loops over it starting at the first number (in this case, 0), stopping at the second number (in our case 1), and stepping a certain amount (this example is +1, you could do -2, +50, etc.). Everything from that line to "Next" will be ran through that loop. So, logically what we are doing is checking for "Sponsored Session" as $windows[0] is "Sponsored Session", and then "Skype" as $windows[1] is "Skype". If you had 20, you'd simply set the "local $windows[2]" to "local $windows[20]", and change "For $i = 0 to 1 step +1" to "For $i = 0 to 19 step +1". Array indexes always start at 0 (some languages don't, but AutoIt does). Remember that, as it'll cause some frustration when your brain says "two variables, the first is one" and the code says "two variables, the first is zero" and you forget to translate it into code.
That should get you where you can make a (nearly) infinite list, not that you'd need to, but you could have it killing every WINDOW but Steam
Finished product:
Code:
#include <Misc.au3> ;This is simply here to include functions we reference by setting hotkeys.
local $windows[2] ;This is the amount of windows you will be looping through
$windows[0] = 'Sponsored session' ;Window 1, starting at 0 because arrays start at 0
$windows[1] = 'Skype' ;Window 2
HotKeySet("{F1}", "Exit") ; HotKey set to F1
Func Exit()
Exit ; Code for action here, in this case to exit the script (i.e. close the program)
EndFunc
;Never-ending loop, built this way so it will run until you stop it
While True
{
;Small loop that only loops through each of your Windows, in this case, 2 (0 through 1) incrementing by 1 per iteration
For $i = 0 to 1 step +1
;If the window at the present index exists ($window[$i], where $i = 0 or 1)
If WinExists($windows[$i]) Then
WinClose($windows[$i])
EndIf
;On to the next window
Next
;Now, make sure steam is focused
If WinExists('Steam') Then
WinActivate('Steam')
EndIf
;Give the CPU a rest for 200ms
sleep, 200
}
I removed a few things as I wasn't aware it was AHK before. This is fully compatible with AutoIt. What it's not presently doing in that code is checking for itself to ensure it's not multi-instantiated. You MAY want to do that, but it may also be fluff if you're diligent and make sure you're not running more than one instance of this script at a time.
There is an instance where you may want to kill something that is not rendering a window. In AutoIt you can kill a process as opposed to a window. If that becomes a need, it's just as easy as killing a window. I don't imagine you'll need to, as a process won't block the view of streaming, but I thought I'd mention it just in case.
I do think you'll be better-served with AutoIt, but that's more of an opinion than anything else. AutoIt has a lot of classes, is more updated (AHK has been quite a bit behind the times in the past), and as such will be more optimized and probably use less CPU. I don't have both of them running in front of me to compare, but I would dare say you could have that AutoIt script running with less CPU usage. Maybe not, just a guess.
P.S. I want to commend you. Scripting isn't the easiest (albeit, not the hardest) thing in the world to jump into. Way to go on getting your hands dirty and coming up with a functional script so quickly! I LOVE coding (do it for a living - love work), and I am of the opinion that it is very rewarding to see keystrokes translate into something functional. Again, good job.
P.P.S. Oh, also, I would like to bring up one point. If you go to close this script, it may be difficult as every 200ms Steam will receive focus. A great way to give yourself an easy kill-command is hotkeys. I've included that in the final code snippet. In my example it's F1, but change it to your liking. You may ask yourself "if the Func Exit() is before the while loop, why doesn't it kill the program without doing anything?" which would be a good question. Functions are only ran when called. You're storing a bit of code in memory to be called later on. You may know that, you may not. Thought I'd elaborate just in case there was curiosity. One thing about AutoIt, though (I think it's this way with AHK as well) once you click the task tray icon it pauses. Always a good idea to have a kill hotkey for any infinite loop, however.