Key Features:
- Simple animated Gui for displaying script variables and settings.
- Added the following buttons: Start you're application, Set administrative rights, Clear variables, and Exit script. They should speak for them self.
- A Admin mode for when the game/program is not excepting input from the script.
- The Gui remembers it's position.
- All variables are written to the script it self.
- Only operates (sending key's etc) to the window that you specify.
- Partially compatible with steam.(working on it)
- Tray icon is that of the program to work with. And for the moment a basic menu item added.
Changes:
- Replaced old if(admin = true) for creating RunAssAdmin button with variable Elevate.
- When elevated. A reset back to user credentials tels the user to manually restart.
- Gui starts centered when launched for the first time, ore after a script reset.
- changed some inconsistent ore sloppy code here and there.
- Improved the process to close feature. When the application did not quit within 5 seconds aggressively terminating it.
- Changed the weird way I enabled the script to run as admin. the code is much simpler now.
ToDo:
Gui needs some fixing.Margins are not optimal so they need to be changed to "relative to"
Code: Select all
/*
[Variables]
AppToRun=
Path=
ExeFile=
WinTitle=
WinClass=
SteamID=
Admin=False
Elevate=RunAsAdmin
[GuiPoss]
xPoss=
yPoss=
*/
;--------------------------Auto Execute Section. Ends where first return is encountered--------------------------
#NoEnv ;Empty variables will not be checked if there system environment variables.
#Persistent ;Script may not close until it receives a ExitApp command.
#NoTrayIcon ;Switching tray icon off until the application to work with is started.
#KeyHistory 0 ;Not needed when only running the script.
;#Warn, all, StdOut ;All known error to standard output.
#SingleInstance off ;The script is allowed to run multiple instances of itself.
ListLines, off ;Not needed when only running the script.
SetTitleMatchMode, 3 ;Variables for wintitle must be a exact match to the value in parameter window title. Whatever it's ahk_exe, ahk_class, ahk_id, etc it must exactly match!
SendMode, Input ;Send command uses the input method. The SetKeyDelay is ignored by SendInput and send in this case.
SetKeyDelay, 100, 50 ;Delay for sending keystrokes and press duration time. For use with the SendEvent command.
SetBatchLines, -1 ;Never sleep script in between execution, 'while its running'. Maximum CPU clock.
SetWorkingDir, %A_ScriptDir% ;Set the script to use it's full folder path as it's working directory. In variable A_ScriptDir, the last backslash is excluded.
;--------------------------Place global variables here--------------------------
;--------------------------Reading variables and adding there content--------------------------
IfExist, %A_ScriptName%
{
IniRead, AppToRun, %A_ScriptName%, Variables, AppToRun
IniRead, Path, %A_ScriptName%, Variables, Path
IniRead, ExeFile, %A_ScriptName%, Variables, ExeFile
IniRead, WinTitle, %A_ScriptName%, Variables, WinTitle
IniRead, WinClass, %A_ScriptName%, Variables, WinClass
IniRead, SteamID, %A_ScriptName%, Variables, SteamID
IniRead, Admin, %A_ScriptName%, Variables, Admin, False
IniRead, Elevate, %A_ScriptName%, Variables, Elevate, RunAsAdmin
IniRead, ScriptEnabled, %A_ScriptName%, Variables, ScriptEnabled, No
IniRead, xPoss, %A_ScriptName%, GuiPoss, xPoss
IniRead, yPoss, %A_ScriptName%, GuiPoss, yPoss
}
If(Admin = "True") ;Cheking if admin is enabled.
{
if not A_IsAdmin ;Checking if the script is elevated, if you just do a Run *RunAs then closing the script. You get trapped in a infinite loop!
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
}
;--------------------------Asking for user input when variable 'AppToRun' at the top of this script is blank--------------------------
again:
If(AppToRun = "") ;Checking if AppToRun is empty.
{
InputBox, AppToRun, , Specify path and executable name of the program you want to start. e.g. "x:\folder\file.exe",,600,130,,,,,
If(ErrorLevel = 1) ;The user pressed cancel.
{
Exitapp
}
Else if(ErrorLevel = 0)
{
If(AppToRun = "")
{
MsgBox, , , You pressed ok`, but nothing was specified!`nPlease try again, 5
gosub, Again
return
}
Else if(AppToRun != "")
{
RegExMatch(AppToRun, "[\w](.*\\)", Path) ;RegEx filter for extracting the path to the application from variable 'AppToRun' and putting it in new variable 'Path'
RegExMatch(AppToRun, "[\s\w]+[.][\w]*", ExeFile) ;RegEx filter for extracting the filename and extension from variable 'AppToRun'. Placing it in new variable 'ExeFile'
;This second filter is not depended on type of file/extension.
IniWrite, %AppToRun%, %A_ScriptName%, Variables, AppToRun
sleep, 10
IniWrite, %Path%, %A_ScriptName%, Variables, Path
sleep, 10
IniWrite, %ExeFile%, %A_ScriptName%, Variables, ExeFile
sleep, 10
IniRead, AppToRun, %A_ScriptName%, Variables, AppToRun ;Getting the value for 'AppToRun' once more, otherwise the tray icon might fail at first run.
}
}
}
;--------------------------Creating tray menu and drawing script GUI--------------------------
Menu, tray, Icon
Menu, tray, Icon, %AppToRun%
Menu, tray, add
Menu, tray, add, Quit Script and %WinTitle%, GuiClose
Gui +LastFound
Gui, Add, GroupBox, x3 y40 w316 h216 ,
Gui, Add, Text, x8 y5 w296 h36 vAppToRun, Path and executable name:`n%AppToRun%
Gui, Add, Text, x8 y56 w296 h36 VWinTitle, Window Title: `n%WinTitle%
Gui, Add, Text, x8 y108 w296 h36 VWinClass, WindowClass:`n%WinClass%
Gui, Add, Text, x8 y159 w296 h36, Admin Enabled:`n%Admin%
Gui, Add, Text, x8 y209 w296 h36 vScriptEnabled, Script is curently enabled:`n%ScriptEnabled%
Gui, Add, GroupBox, x2 y256 w316 h76
Gui, Add, Button, x8 y266 w76 h28, Start
Gui, Add, Button, x84 y266 w76 h28, Stop
Gui, Add, Button, x162 y266 w76 h28, Reset GUI
Gui, Add, Button, x239 y266 w76 h28, Exit Script
Gui, Add, Button, x84 y298 w76 h28, Open Folder
Gui, Add, Button, x239 y298 w76 h28, Exit Application
Gui, Add, Button, x8 y298 w76 h28, %Elevate%
If(xPoss = "" || yPoss = ""){
Gui, Show, xCenter yCenter, Application Launcher
}else{
Gui, Show, x%xPoss% y%yPoss%, Application Launcher
}
Script_Gui:=WinExist()
return
;--------------------------Starting the program, getting WindowClass and title--------------------------
ButtonStart:
IfWinNotExist, ahk_exe %ExeFile%
{
If(SteamID != "") ;This code will executed when the SteamID holds some value.
{
run, steam://rungameid/%SteamID%
WinWait, ahk_exe %ExeFile%
WinWaitActive, ahk_exe %ExeFile%
WinGet, Process_ID, PID, ahk_exe %ExeFile%
}Else{ ;Below code is executed when variable SteamID empty.
run, %AppToRun%,%Path%, , Process_ID
WinWait, ahk_pid %Process_ID%
WinActivate, ahk_pid %Process_ID%
WinWaitActive, ahk_pid %Process_ID%
}
If(WinTitle = "") ;When variable WinTitle is empty, getting the window title and writing it back to the script for later use.
{
WinGetTitle, WinTitle, ahk_pid %Process_ID%, , Application Launcher
GuiControl, , WinTitle, Window Title:`n%WinTitle% ;Displaying/writing the new content of variable WinTitle in the GUI.
IniWrite, %WinTitle%, %A_ScriptName%, Variables, WinTitle ;Writing the new content of variable WinTitle back to the script for later use.
sleep, 10
}
If(WinClass = "") ;Again with a blank variable, getting the window class and writing it back to the GUI and script for later use.
{
WinGetClass, WinClass, ahk_pid %Process_ID%, , Application Launcher
GuiControl, , WinClass, WindowClass:`n%WinClass%
IniWrite, %WinClass%, %A_ScriptName%, Variables, WinClass
sleep, 10
}
}else{ ;If it is not true, it is not running. Then it must be running. :D
WinActivate, ahk_exe %ExeFile%
WinWaitActive, ahk_exe %ExeFile%
WinGet, Process_ID, PID, ahk_exe %ExeFile%
If(WinTitle = "")
{
WinGetTitle, WinTitle, ahk_pid %Process_ID%, , Application Launcher
GuiControl, , WinTitle, Window Title:`n%WinTitle%
IniWrite, %WinTitle%, %A_ScriptName%, Variables, WinTitle
sleep, 10
}
If(WinClass = "")
{
WinGetClass, WinClass, ahk_pid %Process_ID%, , Application Launcher
GuiControl, , WinClass, WindowClass:`n%WinClass%
IniWrite, %WinClass%, %A_ScriptName%, Variables, WinClass
sleep, 10
}
}
GroupAdd, WindowGroup, ahk_class %WinClass% ;Creating a window group in case variables mismatch or left blank. Also the context sensitive directive #IfWinActive/exist doesn't allow variables names.
GroupAdd, WindowGroup, ahk_exe %ExeFile% ;With GroupAdd the parameter of the window title can be variable when using the #IfWin directive.
ScriptEnabled=Yes
GuiControl, , ScriptEnabled, Script is curently enabled: `n%ScriptEnabled%
Gui, Submit, NoHide
Process, WaitClose, %Process_ID%
ScriptEnabled=No
GuiControl, , ScriptEnabled, Script is curently enabled: `n%ScriptEnabled%
Gui, Submit, NoHide
return
;--------------------------Section for the program you want the script to work/operate on--------------------------
;Operations will only execute on the window class ore executable name. Opening some other window that happens hold/start with the same name as the window title will be ignored by the script.
#IfWinActive, ahk_group WindowGroup
{
/*
DO SOMETHING HERE, LIKE SENDING SOME INPUT, GETTING APPLICATION RELATED VALUE'S, ORE MINIPULATE MEMORY.
*/
}
Return
;--------------------------Quit the program to work with--------------------------
ButtonExitApplication:
WinClose, ahk_pid %Process_ID%
While(Process_ID != "")
{
WinGet, Process_ID, PID, ahk_exe %ExeFile%
sleep, 1000
If(A_Index >= 5) ;Forcing process to terminate if it's still running after 5 seconds.
{
Process, close, %Process_ID%
break
}else{
continue
}
}
return ;Since the script is waiting for the program to close, return jumps to the line where it's waiting (Process, WaitClose, %Process_ID%)
;--------------------------Stop the script from operating on the program to work with--------------------------
ButtonStop:
ScriptEnabled = No
Process_ID =
GuiControl, , ScriptEnabled, Script is curently enabled:`n%ScriptEnabled%
Gui, Submit, NoHide
return
;--------------------------Clear all variables, resetting the script to its defaults--------------------------
buttonResetGUI:
AppToRun=
Path=
ExeFile=
WinTitle=
WinClass=
SteamID=
Process_ID=
ScriptEnabled=No
Admin=False
Elevate=RunAsAdmin
xPoss=
yPoss=
IniWrite, %AppToRun%, %A_ScriptName%, Variables, AppToRun
sleep, 10
IniWrite, %Path%, %A_ScriptName%, Variables, Path
sleep, 10
IniWrite, %ExeFile%, %A_ScriptName%, Variables, ExeFile
sleep, 10
IniWrite, %WinClass%, %A_ScriptName%, Variables, WinClass
sleep, 10
IniWrite, %WinTitle%, %A_ScriptName%, Variables, WinTitle
sleep, 10
IniWrite, %Admin%, %A_ScriptName%, Variables, Admin
sleep, 10
IniWrite, %Elevate%, %A_ScriptName%, Variables, Elevate
sleep, 10
IniWrite, %SteamID%, %A_ScriptName%, Variables, SteamID
sleep, 10
IniWrite, %xPoss%, %A_ScriptName%, GuiPoss, xPoss
sleep, 10
IniWrite, %yPoss%, %A_ScriptName%, GuiPoss, yPoss
sleep, 10
;Writing new content to the GUI window.
GuiControl, , WinTitle, Window Title: `n%WinTitle%
GuiControl, , WinClass, WindowClass:`n%WinClass%
GuiControl, , AppToRun, Path and executable name:`n%AppToRun%
GuiControl, , ScriptAdmin, Admin Enabled:`n%Admin%
GuiControl, , ScriptEnabled, Script is curently enabled:`n%ScriptEnabled%
Gui, Show, xCenter yCenter, Application Launcher
Return
;--------------------------Run the script with administrative privileges --------------------------
/*
Because of security reasons you should only enable this if the game/application does
not except input from the script. I advice against running scripts as admin by default!
*/
ButtonRunAsAdmin:
WinGetPos, xPoss, yPoss, , , Application Launcher
Admin=True
Elevate=RunAsUser
IniWrite, %Admin%, %A_ScriptName%, Variables, Admin
sleep, 10
IniWrite, %Elevate%, %A_ScriptName%, Variables, Elevate
sleep, 10
IniWrite, %xPoss%, %A_ScriptName%, GuiPoss , xPoss
sleep, 10
IniWrite, %yPoss%, %A_ScriptName%, GuiPoss , yPoss
sleep, 10
DllCall("AnimateWindow","UInt",Script_Gui,"Int",500,"UInt","0x90000")
Run *RunAs "%A_ScriptFullPath%"
ExitApp
return
ButtonRunAsUser:
WinGetPos, xPoss, yPoss, , , Application Launcher
Admin=False
Elevate=RunAsAdmin
IniWrite, %Admin%, %A_ScriptName%, Variables, Admin
sleep, 10
IniWrite, %Elevate%, %A_ScriptName%, Variables, Elevate
sleep, 10
IniWrite, %xPoss%, %A_ScriptName%, GuiPoss , xPoss
sleep, 10
IniWrite, %yPoss%, %A_ScriptName%, GuiPoss , yPoss
sleep, 10
DllCall("AnimateWindow","UInt",Script_Gui,"Int",500,"UInt","0x90000")
Gui, hide
MsgBox, 48, , The script is running with elevated previlages. Therefore`, the script can not restart itself using user credentials. A user is not allowed to do anything in a admin environment. The next time you start the script it will run as user.
ExitApp
Return
;--------------------------Opens the installation directory of the program to opereta on--------------------------
ButtonOpenFolder:
Run, Explorer.exe "%Path%"
Return
;--------------------------Writing variables. Exit script--------------------------
GuiClose:
ButtonExitScript:
WinGetPos, xPoss, yPoss, , , Application Launcher
sleep, 10
IniWrite, %AppToRun%, %A_ScriptName%, Variables, AppToRun
sleep, 10
IniWrite, %Path%, %A_ScriptName%, Variables, Path
sleep, 10
IniWrite, %ExeFile%, %A_ScriptName%, Variables, ExeFile
sleep, 10
IniWrite, %WinClass%, %A_ScriptName%, Variables, WinClass
sleep, 10
IniWrite, %WinTitle%, %A_ScriptName%, Variables, WinTitle
sleep, 10
IniWrite, %SteamID%, %A_ScriptName%, Variables, SteamID
sleep, 10
IniWrite, %xPoss%, %A_ScriptName%, GuiPoss , xPoss
sleep, 10
IniWrite, %yPoss%, %A_ScriptName%, GuiPoss , yPoss
sleep, 10
DllCall("AnimateWindow","UInt",Script_Gui,"Int",500,"UInt","0x90000")
ExitApp