Jump to content
LaunchBox Community Forums

AutoHotKey wait for executable to close.


thedevilsjester

Recommended Posts

4 hours ago, Zombeaver said:

I think a per-emulator-entry "automatically launch before/after" function would be nice, but it hasn't happened yet (and I wouldn't expect something this niche to be particularly high priority, honestly).

I think that a per emulator check box that says "Persist AutoHotKey Script" that relies on the user to end the script if checked (unchecked by default), would be a much simpler addition.

4 hours ago, Zombeaver said:

You could do this via the additional apps function by adding an .ahk file as the app and then checking the box to "automatically run after main application". This will make whatever program/script/thing you specify run once the game has closed. You can't currently edit additional apps en masse though (although this is on the to-do list) so this would only be on a per-game basis.

That is an interesting solution, though it does not scale out to hundreds of games (not until the to-do-list item is complete at least).  IF my feature request gets accepted (https://bitbucket.org/jasondavidcarr/launchbox/issues/4147/autohotkey-persist-after-termination) it might be a good temporary solution to do on a per-game basis.

4 hours ago, Zombeaver said:

The problem that you're running into here is that AHK within LB ends as soon as the program that was started/launched ends. It's not like a normal external AHK script which only ends when you tell it to. In other words, it's not persistent once the launched program has ended, so it can't do anything further (like use a WinWaitClose function) at that point.

I thought that was the case, since I could not get anything to execute after the program has terminated.

4 hours ago, Zombeaver said:

(and I wouldn't expect something this niche to be particularly high priority, honestly).

I think you might be surprised how useful it can be and how many people would utilize it.  My personal use case is for some emulators, DS, 3DS, Arcade, etc... that use a vertical layout, I can have it automatically rotate the screen when the emulator launches and rotate it back when the emulator closes.  All while still using BigBox and no keyboard.  With the current setup, I cannot rotate the screen back, because that code is never executed.  I think quite a few people would find this useful.  It sure makes playing vertical resolution emulators/games so much nicer.

Edited by thedevilsjester
Link to comment
Share on other sites

48 minutes ago, thedevilsjester said:

I think that a per emulator check box that says "Persist AutoHotKey Script" that relies on the user to end the script if checked (unchecked by default), would be a much simpler addition.

They're not mutually exclusive.

48 minutes ago, thedevilsjester said:

I think you might be surprised how useful it can be and how many people would utilize it.

I didn't say it wasn't useful. What I'm saying is that if the option for development is this or say the "pause menu", one sells licenses and the other doesn't. There are small, practical application improvements that I've requested literally years ago that haven't been implemented. Jason's just one guy, and just because something's a priority for you or I it doesn't necessarily mean it's the best place to prioritize that one guy's efforts. I'm telling you from experience that you shouldn't expect something like this to be implemented with any particular alacrity. It might be put in tomorrow or it might be a year from now. So with that in mind, you might want to try one of the suggested solutions before waiting it out.

  • Like 2
Link to comment
Share on other sites

Ok, I just added this simple snippet to the emulator ahk tab:

$Esc::
{
        Run, %A_AHKPath% "C:\temp\dostuff.ahk"
}

 

Upon exiting the game and emulator, the dostuff.ahk script was still running. It slept a bit and then it killed a process i had running.

Is this not what you want to do? It's not much work at all? 

 

Link to comment
Share on other sites

This also works for me:

$Esc::
{
        Run, "C:\temp\dostuff.ahk"
}

 

Edit: I did not have to rename any ahk exe files or anything. just needed to add the line above to launch my ahk script.

Edited by ckp
  • Like 2
Link to comment
Share on other sites

10 hours ago, Zombeaver said:

and then set that script as the "emulator".

 

What I like about this suggestion is its usefulness with retroarch. If im remembering correctly using the variable %3% captures retroarch core. Using an ifinstring or maybe a if variable %3% equals bsnes.dll etc - do stuff, basically creating a per system script for retroarch. Cuts the need for multiple scripts acting as emulators. Is a cool solution. Although I haven't really found many uses for it except displaying a controller image.

Edited by jayjay
Link to comment
Share on other sites

On 4/27/2018 at 9:40 PM, ckp said:

This also works for me:

$Esc::
{
        Run, "C:\temp\dostuff.ahk"
}

 

Edit: I did not have to rename any ahk exe files or anything. just needed to add the line above to launch my ahk script.

I fear that this would create a race condition if the emulator is also polling for, and processing, the Esc key.

However the ability to run another AHK script directly like this is an interesting idea.  If a script run in this fashion continues to run when the callee is killed, then one could have an AHK script that defers entirely to another AHK script and that one waits for the application to end.

 

Link to comment
Share on other sites

On 28/04/2018 at 3:38 AM, ckp said:

Ok, I just added this simple snippet to the emulator ahk tab:

$Esc::
{
        Run, %A_AHKPath% "C:\temp\dostuff.ahk"
}

 

Upon exiting the game and emulator, the dostuff.ahk script was still running. It slept a bit and then it killed a process i had running.

Is this not what you want to do? It's not much work at all? 

 

Could you explain how this works because I cant seem to get it to work. Am getting "Cant find script" error. Please use simple steps for a simple mind ;) I figured out, trying your method that LB uses Launchbox\Metadata\Temp as temp directory. So placing do stuff.ahk script there works using :

SetWorkingDir %A_ScriptDir%
run, Autohotkey.exe "do stuff.ahk"

No need to copy autohotkey.exe. Just need to place do stuff.ahk inside Launchbox\Metadata\Temp.

 

11 hours ago, thedevilsjester said:

I fear that this would create a race condition if the emulator is also polling for, and processing, the Esc key.

However the ability to run another AHK script directly like this is an interesting idea.  If a script run in this fashion continues to run when the callee is killed, then one could have an AHK script that defers entirely to another AHK script and that one waits for the application to end.

 

You are one hard dude to please lol.  Could you not change the esc key in @ckp example?

Or you could...

Create a txt file at Launchbox\Metadata\Temp

Name it "do stuff.ahk", right click and edit in notepad

#SingleInstance, Force
#Persistent

loop
{
Process, Exist, retroarch.exe
pid1 := ErrorLevel
If (!pid1)
         {
	  continue
         }
	  else
		 {
	  	 Goto Next
		 }
}

Next:
SetTimer, ProcessCheckTimer, 1000
Return
 
ProcessCheckTimer:
Process, Exist, retroarch.exe
pid1 := ErrorLevel
If (!pid1)
         {
	 Send !{Space}
         ExitApp
         }
Return

This script uses retroarch as an example, in the script change retroarch.exe to whatever. It will check every 1 second for the process retroarch.exe, when it doesn't exist (when retroarch is closed) it sends !{Space} then exits the script.

If 1 second is to fast change the SetTimer, ProcessCheckTimer, 1000 to SetTimer, ProcessCheckTimer, 2000 for 2 seconds and so on.

Or if 1 second is to slow use a loop.

#SingleInstance, Force
#Persistent

loop
{
Process, Exist, retroarch.exe
pid1 := ErrorLevel
If (!pid1)
         {
	  continue
         }
	  else
		 {
	 	 Goto Next
		 }
}

Next:
loop
{
Process, Exist, retroarch.exe
pid1 := ErrorLevel
If (!pid1)
         {
	 Send !{Space}
         ExitApp
         }
}
Return

In LB emulator tab write:

SetWorkingDir %A_ScriptDir%
run, Autohotkey.exe "do stuff.ahk"

This will run along side the emulator but resources are minimal and you wont encounter the race condition. But what it is doing is:

11 hours ago, thedevilsjester said:

one could have an AHK script that defers entirely to another AHK script and that one waits for the application to end.

 

 

Edited by jayjay
  • Like 1
Link to comment
Share on other sites

7 hours ago, jayjay said:

Could you explain how this works because I cant seem to get it to work. Am getting "Cant find script" error.

Well, that's all I had to do as long as I put the actual ahk script in that path specified. Maybe it works for me because I have AutoHotKey actually installed on my system.

Link to comment
Share on other sites

20 minutes ago, ckp said:

Well, that's all I had to do as long as I put the actual ahk script in that path specified. Maybe it works for me because I have AutoHotKey actually installed on my system.

Yeah, you would have to have AHK standalone installed + have .ahk files have a launch association with it (which should be default once it's installed) and it should work. It would be like just double-clicking on the .ahk file.

Link to comment
Share on other sites

35 minutes ago, ckp said:

You may also just be able to use the autohotkey.exe that comes with LB, by running the path to autohotkey.exe and your script name.

Yeah that's what my post above does, uses LB's ahk... well I think it does... works without ahk installed anyway. Better for making it portable right.. for when we go selling hard drives full of games and BB right?... I mean for when we take our drives round our mates house.

Edited by jayjay
Link to comment
Share on other sites

10 hours ago, jayjay said:

You are one hard dude to please lol. 

I just want to run a single command after a program finishes executing.  It has to always run (no race conditions), it cannot rely on additional software that needs to be installed, and it cannot "break immersion" (additional popups, terminal windows, etc...).  These are not unreasonable asks.

10 hours ago, jayjay said:

Could you not change the esc key in @ckp example?

What is the end goal you have in mind with that?  I press a button to exit the emulator, that button cannot be the same button that triggers the script because of race conditions.  If I changed the button to something else, then I would have to hit one button (before?) I exit the emulator, and another button to exit the emulator.  

2 hours ago, jayjay said:

But anyway I think there's enough here for @thedevilsjester to create something. "Two decades of development"... should be jedi mind tricking this shit.

I think you have the wrong impression.  The goal has never been to achieve this in any way at any cost.   Thats easy.   I could rattle off a dozen different solutions that would get the job done.  LaunchBox is a visually pleasing, elegant, all-in-one launcher solution.  The goal is to use this system to perform the task.

I have yet to test it, but it seems like (from the last few posts) that I can use the AutoHotKey executable bundled with LaunchBox, to launch a separate script from the script inside of LaunchBox that will not be killed when the original script is killed, letting me execute code to wait for the process to end.  This would not require additional software, it would not create a race condition, and it would not cause additional windows/popups/terminals to show up.  I do appreciate the discussion, since starting this thread we have went from creating a batch file that runs some javascript and wraps the emulator, to an actual solution from within LaunchBox that seems like it might work.  I will update the thread once I have tried it out.

Edited by thedevilsjester
Link to comment
Share on other sites

To recap, putting together a few ideas from the thread, what I did was have a script that looks like:

Run "C:\Users\MyUser\LaunchBox\AutoHotkey\AutoHotkey.exe" "F:\Scripts\RunAfter.ahk"

No Esc or other key trigger, just this command by its lonesome.  Then in the RunAfter.ahk I can run

WinWait, SomeEmulator
WinWaitClose, SomeEmulator
MsgBox, SomeEmulator Closed!
Exit

And this works perfectly.  Thanks for everyone's participation and throwing around ideas.

Edited by thedevilsjester
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

10 hours ago, thedevilsjester said:

I just want to run a single command...

That's fair enough dude. I was just messing with ya. 

10 hours ago, thedevilsjester said:

What is the end goal you have in mind with that?

I was thinking, using @ckp example, something crude like:

f1::
{
        Run, %A_AHKPath% "C:\temp\dostuff.ahk"
	send {esc down}{esc up}
}

 

But that's cool you got it sorted. When you create some awesome scripts dont forget to share with us in the autohotkeys thread.

Link to comment
Share on other sites

FYI you can use relative paths in AHK scripts too, and I believe they're relative from AHK's location. So if you had the script in the same directory it should just be:

Run, Autohotkey.exe RunAfter.ahk

If you had it elsewhere on the same drive it'd be ..\..\scripts\ or whatever.

Link to comment
Share on other sites

8 hours ago, jayjay said:

I was thinking, using @ckp example, something crude like:


f1::
{
        Run, %A_AHKPath% "C:\temp\dostuff.ahk"
	send {esc down}{esc up}
}

 

I understand now. 

But that's cool you got it sorted. When you create some awesome scripts dont forget to share with us in the autohotkeys thread.

I will polish up the script I am using, make it more generic, and then post it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...