Batch File to Project Screen After Program Closes?

pilotguy251

Commendable
Mar 12, 2016
5
0
1,510
Hello, I am running a program, and I am trying to get the screen to extend to my second display after said program closes. I have been successful in getting the opposite to happen, take the screen from the second monitor to only my primary when the program starts, but I want to reverse it when the program is finished. Here is what I am working with so far. It changes to internal, but does not reverse back to extend.

@echo off
DisplaySwitch.exe /internal
/Wait DisplaySwitch.exe /extend
Start "" "A:\Steam\steamapps\common\FSX\fsx.exe"
Start "" "C:\Users\hamha\Desktop\FSISERVER - Shortcut"
Start "" "C:\Users\hamha\Documents\aismv120\AISmooth.exe"
Taskkill "" FSISERVER.EXE
Taskkill "" AISmooth.exe

I guess I should mention that I want the display to extend after the closure of fsx.exe.
 
Solution
Tinkered around with some scripts.

Get-Process -Name "fsx" will tell you if Flight Simulator X is running or not. (Where the quotes can actually contain any program name.)

[Sidebar: I actually used "notepad" for my testing. Much quicker to start and stop than my retail version of FSX.]

If the program is running, Get-Process will provide information about the program. If not running then you will get an error message.

Another thing happens as well. If successful (running) a variable $? will be set to "True". If unsucessful (not running) the variable $? will be set to "False".

$? simply captures the results of the last executed command. True if successful, False if not sucessful.

So $? can be used as a flag to trigger...
Are you trying to play Flight Simulator X on just one monitor and then, when done playing, go back into extended monitors?

Overall, not sure about the flow of your batch file with respect to the text of your post. Are you using a desktop shortcut to open the game, etc.?

However, will offer an observation.

I.e., I see fsx.exe started but not stopped (via Taskkill).

You want, I think the /WAIT to be in place until fsx.exe is closed

And then, after FSX.exe is closed, then you would apply the "DisplaySwitch.exe /extend

The other part is that your batch file has no way to know/test that you closed FSX - you may need a shortcut to do that or some conditional built into your batch file to initiate Taskkill on FSISERVER.EXE and AISmooth when FSX is closed. Then the DisplaySwitch.exe /extend is applied.

Just taking a bit of a guess at both the logic and syntax within your batch file. Can you provide some additional clarification - especially if I have gone off track?
 

pilotguy251

Commendable
Mar 12, 2016
5
0
1,510


 

pilotguy251

Commendable
Mar 12, 2016
5
0
1,510
Is this the way to are suggesting it should look? If not, would you mind modifying it how you see fit?
@echo off
DisplaySwitch.exe /internal
Start /wait "" "A:\Steam\steamapps\common\FSX\fsx.exe"
Start "" "C:\Users\hamha\Desktop\FSISERVER - Shortcut"
Start "" "C:\Users\hamha\Documents\aismv120\AISmooth.exe"
Taskkill "" FSISERVER.EXE
Taskkill "" AISmooth.exe
DisplaySwitch.exe /extend
 
First see what all goes on by allowing echo.

Came up with a question about your second "Start": why a shortcut versus just a path directly to FSISERVER.EXE as you did for FSX.EXE?

Also wondering about AISmooth being in the Documents folder. Was that required or did it just end up there? Not a criticism as I have a few stray .exe's floating around myself.

Digression aside and to address modifying:

The key part is the need for a continuous running test between the "Start" and the "Taskkill" sections. I.e., a loop.

What that loop should do is monitor if fsx.exe is running.

To get a sense of it, run "Get-Process fsx" via powershell with and without Flight Simulator X running.

Should fsx be found running the loop continues. Should fsx be found not to be running (i.e., you exited Flight Simulator X) then the loop exits and the Taskkill commands are executed and then the DisplaySwitch command returning to the desired extended screen display.

Full disclosure: I very much consider myself a beginner with respect to PowerShell. Powershell is very powerfull - not always sure that my script is all that clean and efficient. Hence I restrain from just tossing scripts out. Especially if loops are involved.

A nice clean loop monitoring fsx that returns a simple "True" if fsx is running or "False" if fsx is not running is the objective.

Looking at applying one of the common parameters to do that.

And perhaps some Powershell user will join in and post a suggestion or two. Fine with me as I will learn more as well.

I copied my FSX shortcut icon to my desktop and will do some testing as circumstances warrant.
 

pilotguy251

Commendable
Mar 12, 2016
5
0
1,510
Thank you. I'll experiment with the get loop line when I get home. I copied and pasted the wrong version of the text while I was experimenting which had a lot of stray lines everywhere. The AI smooth is in the ai smooth folder in documents because it's somewhat of a self contained program within that folder. Doesn't install as a normal program would.
 
Tinkered around with some scripts.

Get-Process -Name "fsx" will tell you if Flight Simulator X is running or not. (Where the quotes can actually contain any program name.)

[Sidebar: I actually used "notepad" for my testing. Much quicker to start and stop than my retail version of FSX.]

If the program is running, Get-Process will provide information about the program. If not running then you will get an error message.

Another thing happens as well. If successful (running) a variable $? will be set to "True". If unsucessful (not running) the variable $? will be set to "False".

$? simply captures the results of the last executed command. True if successful, False if not sucessful.

So $? can be used as a flag to trigger additional commands. E.g., If "True" FSX is running, If "False" then FSX is no longer running and the desired Taskkills and Switches can be executed. Normally a straightforward IF-THEN-ELSE.

The problem is that an endless loop is needed to keep monitoring the status of FSX.

I tried a couple of variations: WHILE for example, Do Until......

Used Write-Host to watch variables and played around some with other parameters to see if I could establish some clean loop and the application of any CommonParameters. Generally messy.....

Sort of came to the conclusion that you might be better served by having two shortcuts/scripts/batchfiles. The first shortcut would setup and start FSX. The second shortcut would close FSX, do the Taskkills and restore the screen.

That got me thinking about maybe just one batchfile again but after the initial setup that file presents a "Are you done yet" type question and simply waits for you to respond "Yes"or "No". Sits in the background, minimized and waiting. When you are finished playing you simply provide a response.

Either way something will need to be running. Question/Answer batch file would be certainly running, but not running any loops to keep checking program (fsx) status. The loops might be slowed if some /Wait is embedded - thought about that but have not gotten beyond thinking that may be equally messy.

Still felt it best to share where I am on this. May save you some time and effort.
 
Solution

pilotguy251

Commendable
Mar 12, 2016
5
0
1,510
Yeah, that's sort of where I came to it as well, as I had done some experimenting with the things you mentioned and it really never worked. So I'll probably make the other batch file and link to it in the main one and let it work that way. Thanks for your experimentation. Also cool to find another FSX user!