Playnite Forums
  • Playnite Web
  • Rules
  • Members
  • Team
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Playnite Forums Extension Database Generic Extensions Tutorial: Run an executable before starting game and stop it after closing it

Tutorial: Run an executable before starting game and stop it after closing it
darklinkpower
Offline

Super Moderator
#1
11-23-2019, 10:07 PM (This post was last modified: 12-13-2020, 09:43 PM by darklinkpower.)
This tutorial will show you how to to run an executable when launching a game and close it after exiting a game.

Tutorial on Playnite Wiki: https://github.com/JosefNemec/Playnite/w...s#examples

Instructions
                                                                                                                                                                             
1a. If you want the executable to run only for a particular game: Open the Edit Game Details (With F3 or right click+Edit...) and go to the Scripts tab
1b. If you want the executable to run only for all your games: Open the Scripts submenu in the Settings Window (With F4 or Main menu --> Settings... --> Scripts)

2. Select Powershell in the Script Runtime dropwdown menu

3. Paste the following in the "Execute before starting game" text box and replace the path and executable name for the one you want to use
Start-Process "c:\path\to\your\executable\Executable.exe"

3. Paste the following in the "Execute after exiting game" text box and replace the ProcessName for the process name of your executable.
Stop-Process -Name ProcessName

You can easily check the process name in Task Manager:
[Image: N8CVyvC.png]

In case you start the process with elevated rights, use the following instead replacing the name of the executable:
(Get-WmiObject -Class Win32_Process -Filter "name = 'Executable.exe'").Terminate()

4. Save and exit


Example on how it should look
                                                                                                                                                                             

[Image: dkun3zC.png]
yoshinatsu
Offline

#2
11-24-2019, 12:11 AM (This post was last modified: 11-24-2019, 12:12 AM by yoshinatsu.)
Hi! I wanted exactly this script for Borderless Gaming.
But I get this error after quitting the game!

[Image: exJKo5K.png]

Maybe it's because Borderless Gaming requires administrator privileges?
darklinkpower
Offline

Super Moderator
#3
11-24-2019, 01:52 AM
Yes, that's the reason. I tested with the same app as my example and it happens too, but clicking Ok closes it anyway. You can always disable the run as admin option for the executable, I think Borderless Gaming should work anyway for most games that way
CriticalComposer
Offline

#4
11-24-2019, 05:15 AM
I was using Batch since I'm more familiar with it but for some reason it wasn't accepting sleep. Does Powershell have a wait/pause/sleep function that Playnite will acknowledge?
Crow
Offline

Administrator
#5
11-24-2019, 10:00 AM
(11-24-2019, 05:15 AM)CriticalComposer Wrote: I was using Batch since I'm more familiar with it but for some reason it wasn't accepting sleep. Does Powershell have a wait/pause/sleep function that Playnite will acknowledge?

PowerShell has Start-Sleep cmdlet.
Crow
Offline

Administrator
#6
11-24-2019, 10:04 AM
If you want to be able to shutdown processes that runs with elevated rights then you need to start Playnite as an admin. Game scripts (extensions in general) run under the same privileges as main Playnite process, there's no way around that.
yoshinatsu
Offline

#7
11-24-2019, 11:37 PM (This post was last modified: 11-24-2019, 11:37 PM by yoshinatsu.)
(11-24-2019, 10:04 AM)Crow Wrote: If you want to be able to shutdown processes that runs with elevated rights then you need to start Playnite as an admin. Game scripts (extensions in general) run under the same privileges as main Playnite process, there's no way around that.
Haha, is that all?!
You guys should have said that earlier, there are only a handful of apps that I trust as much as Playnite, so no problem running it as admin Heart
Crow
Offline

Administrator
#8
11-27-2019, 03:32 PM
I've also added this to our FAQ.
Gildum
Offline

#9
03-24-2020, 09:28 AM (This post was last modified: 03-24-2020, 09:32 AM by Gildum.)
I had issues to get the script running with the gamepad tool Xpadder when a profile had spaces, and now found a solution.

An example of the issue:
Start-Process "D:\gamepad\Xpadder.exe" "Ski Racing"


The string Ski Racing gets recognized as just Ski. One solution is to just rename the profile and remove the spaces: SkiRacing

So this would start the profile properly:
Start-Process "D:\gamepad\Xpadder.exe" "SkiRacing"


In case you want to keep the spaces in the profile name, the solution is:
Start-Process "D:\gamepad\Xpadder.exe" "`"Ski Racing`""


I'm glad I finally found the solution after some googling, as several suggested solutions didn't work, e.g.:
'Ski Racing'
& Ski` Racing
-file "Ski Racing"
and a few more complex ones...
slymeasy
Offline

#10
04-13-2020, 07:08 AM
Thanks for this. With this MultiMonitor Tool I was able to switch my main display each time I start up a game.

[Image: E1Anqi7.jpg]

Start-Process "C:\Program Files\multimonitortool-x64\MultiMonitorTool.exe" "MultiMonitorTool.exe /SetPrimary 1"
Start-Process "C:\Program Files\multimonitortool-x64\MultiMonitorTool.exe" "MultiMonitorTool.exe /SetPrimary 2"
SeeDborg
Offline

#11
05-05-2020, 08:15 PM (This post was last modified: 05-05-2020, 08:16 PM by SeeDborg.)
(11-24-2019, 12:11 AM)yoshinatsu Wrote: Hi! I wanted exactly this script for Borderless Gaming.
But I get this error after quitting the game!

[Image: exJKo5K.png]

Maybe it's because Borderless Gaming requires administrator privileges?

If anyone is interested, I had the exact same issue and resolved it with launching this instead :

wmic process where "name='BorderlessGaming.exe'" delete
plW0lf
Offline

#12
06-24-2020, 09:27 PM
In topic of scripts - script for changing resolution of desktop before and after running a game.
Function Set-ScreenResolution {

<#
    .Synopsis
        Sets the Screen Resolution of the primary monitor
    .Description
        Uses Pinvoke and ChangeDisplaySettings Win32API to make the change
    .Example
        Set-ScreenResolution -Width 1024 -Height 768        
    #>
param (
[Parameter(Mandatory=$true,
           Position = 0)]
[int]
$Width,

[Parameter(Mandatory=$true,
           Position = 1)]
[int]
$Height
)

$pinvokeCode = @"

using System;
using System.Runtime.InteropServices;

namespace Resolution
{

    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE1
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;

        public short dmOrientation;
        public short dmPaperSize;
        public short dmPaperLength;
        public short dmPaperWidth;

        public short dmScale;
        public short dmCopies;
        public short dmDefaultSource;
        public short dmPrintQuality;
        public short dmColor;
        public short dmDuplex;
        public short dmYResolution;
        public short dmTTOption;
        public short dmCollate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmFormName;
        public short dmLogPixels;
        public short dmBitsPerPel;
        public int dmPelsWidth;
        public int dmPelsHeight;

        public int dmDisplayFlags;
        public int dmDisplayFrequency;

        public int dmICMMethod;
        public int dmICMIntent;
        public int dmMediaType;
        public int dmDitherType;
        public int dmReserved1;
        public int dmReserved2;

        public int dmPanningWidth;
        public int dmPanningHeight;
    };



    class User_32
    {
        [DllImport("user32.dll")]
        public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
        [DllImport("user32.dll")]
        public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);

        public const int ENUM_CURRENT_SETTINGS = -1;
        public const int CDS_UPDATEREGISTRY = 0x01;
        public const int CDS_TEST = 0x02;
        public const int DISP_CHANGE_SUCCESSFUL = 0;
        public const int DISP_CHANGE_RESTART = 1;
        public const int DISP_CHANGE_FAILED = -1;
    }



    public class PrmaryScreenResolution
    {
        static public string ChangeResolution(int width, int height)
        {

            DEVMODE1 dm = GetDevMode1();

            if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
            {

                dm.dmPelsWidth = width;
                dm.dmPelsHeight = height;

                int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);

                if (iRet == User_32.DISP_CHANGE_FAILED)
                {
                    return "Unable To Process Your Request. Sorry For This Inconvenience.";
                }
                else
                {
                    iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
                    switch (iRet)
                    {
                        case User_32.DISP_CHANGE_SUCCESSFUL:
                            {
                                return "Success";
                            }
                        case User_32.DISP_CHANGE_RESTART:
                            {
                                return "You Need To Reboot For The Change To Happen.\n If You Feel Any Problem After Rebooting Your Machine\nThen Try To Change Resolution In Safe Mode.";
                            }
                        default:
                            {
                                return "Failed To Change The Resolution";
                            }
                    }

                }


            }
            else
            {
                return "Failed To Change The Resolution.";
            }
        }

        private static DEVMODE1 GetDevMode1()
        {
            DEVMODE1 dm = new DEVMODE1();
            dm.dmDeviceName = new String(new char[32]);
            dm.dmFormName = new String(new char[32]);
            dm.dmSize = (short)Marshal.SizeOf(dm);
            return dm;
        }
    }
}

"@

Add-Type $pinvokeCode -ErrorAction SilentlyContinue
[Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height)
}

Set-ScreenResolution -Width 1920 -Height 1080
You only need to change last line by setting correct resolution.
It's been useful for me in some games that I play in full screen on 21:9 screen (some games that can handle max 16:9 can sometimes do some weird things...) so I decided to share it with you.

This script isn't mine. Source: https://superuser.com/questions/1493334/...-win10-pro
vannessqn
Offline

#13
11-14-2020, 12:24 PM (This post was last modified: 11-14-2020, 12:49 PM by vannessqn.)
Can you guys help me I want to launch RivaTuner Statistics Server (I use it for FPS limiter) with spefific profile for games, Idk much about tech stuff so if anyone can try to help thank you!!
[Image: nGURrWO.png]
slymeasy
Offline

#14
04-03-2021, 03:30 PM
What about running a program when Playnite is closed?
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread

© Designed by D&D - Powered by MyBB

Linear Mode
Threaded Mode