Need Help With Simple Code Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Drungle
Posts: 2
Joined: 12 Aug 2017, 23:52

Need Help With Simple Code

13 Aug 2017, 00:16

Hello, everyone. New guy here with AHK and to the forums. I have a fairly simple task that I'm trying to automate that I can't seem to nail down correctly. I've spent the last couple hours searching around on Google and trying different suggestions from older posts, but it's not working correctly.

So the goal of what I'm trying to accomplish is relatively simple in theory, but I may not be going about this with the correct method. I play Playerunknown's Battlegrounds (game for those of you who don't know it) which requires me to change my default audio device on Win 7 to my headphones everytime I want to play. It's a pain in the ass. So I figured I could just automate this with Autohotkey. All I've seen for the most part are hotkey scripts. I'm looking for a shortcut related script.

For instance:

Start PUBG from desktop shortcut
AHK script runs - changes default sound to headphones via batch file
Once I exit PUBG - changes default sound back to my Amp via batch file

Simple as that. So to do this I've opted to use Nircmd so I don't have to see the sound window popup and such. So far this is what I've got..

Code: Select all

#Persistent
WinWaitActive, PLAYERUNKNOWN'S BATTLEGROUNDS
Sleep, 100
Run, headphones.cmd (first batch file here)
WinActivate, PLAYERUNKNOWN'S BATTLEGROUNDS			
Sleep, 100											
Process, Exist, TslGame.exe							
Variable= ErrorLevel									
If Variable= 0										
WinActivate, PLAYERUNKNOWN'S BATTLEGROUNDS			
Else if Variable != 0									
Run, amp.cmd (second batch file here)
ExitApp
Return
So what happens with this right now is, when I activate my PUBG window it runs my headphone batch file and changes the sound no problem. But then it also runs my amp batch file to change it back to my speakers right after..even with the game running which is not what I want. I want that to happen after I exit the game. So I need it to check and see if the game is running, not just an active window before switching back to my speakers. I would really appreciate input on how to fix this!
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Need Help With Simple Code  Topic is solved

13 Aug 2017, 02:52

Code: Select all

#Persistent
WinWaitActive, PLAYERUNKNOWN'S BATTLEGROUNDS
Sleep, 100
Run, headphones.cmd
WinActivate, PLAYERUNKNOWN'S BATTLEGROUNDS
Sleep, 100
SetTimer, checkIfGameIsClosed, 1000    ; Check once per second
return

checkIfGameIsClosed:
    Process, Exist, TslGame.exe
    if (ErrorLevel = 0)
	{   ; The game is not running
        Run, amp.cmd
        ExitApp
    }
Return
HTH
Drungle
Posts: 2
Joined: 12 Aug 2017, 23:52

Re: Need Help With Simple Code

13 Aug 2017, 18:32

Yes, thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Sniperman and 391 guests