The relaunching has an issue: nothing with command line parameters is being relaunched.
Could you post or PM the ini file, (blank anything you don't want seen) ?Ah, I think my handling of quotes was a little skewiff.
Try this one, it seems to work OK with the specific example you gave above - might need to rework it for more complicated quoting.
Have also removed the option to leave open the output CLI, (just to make the parameters in the ini file a little simpler), but if anyone wants it back, say something.
; Filename: Movie-ize.au3
#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=popcorn-32x32.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Icon_Add=popcorn-32x32.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <_SysTray.au3>
#include <Process.au3>
#include '_OsVersionInfo.au3'
Global $inifile = 'Movie-ize.ini', $tempini = 'Movie-tmp.ini', $_state = False, $_term = False
;--- Start Tray menu stuff
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
$movieon = TrayCreateItem('Movie-ize',-1,-1,1)
TrayItemSetOnEvent(-1, '_movieOn')
$movieoff = TrayCreateItem('De-Movie-ize',-1,-1,1)
TrayItemSetOnEvent(-1, '_movieOff')
TrayCreateItem('')
$prefs = TrayCreateItem('Preferences',-1,-1,0)
TrayItemSetOnEvent(-1, '_prefs')
$about = TrayCreateItem('About',-1,-1,0)
TrayItemSetOnEvent(-1, '_about')
$exit = TrayCreateItem('Exit',-1,-1,0)
TrayItemSetOnEvent(-1,'_Exit')
TraySetIcon('Movie-ize.exe', 4)
TraySetToolTip("Get out the popcorn 'cause it's movie time!")
TrayItemSetState($movieoff, 129)
TrayItemSetState($movieon, 64)
TraySetState()
;--- End Tray menu stuff
If Not FileExists($inifile) Then
_CreateBaseIni()
EndIf
$kill = IniReadSection($inifile, 'Programs')
$kparam = IniReadSection($inifile, 'Parameters')
If $CmdLine[0] > 0 Then $_term = True
If $_term Then
_RunAndExit() ;--- If there's a CLI argument then Run and Exit
Else
_Initialise() ;--- Otherwise, check for Movie-tmp.ini and initialise to movieOn state
EndIf
While 1
Sleep(1000) ;--- Twiddle thumbs
WEnd
Func _Initialise()
If FileExists($tempini) Then
$xterm = IniReadSection($tempini, 'Start')
For $i = 1 To $kill[0][0]
$kill[$i][0] = $xterm[$i][1]
Next
$_state = True
TrayItemSetState($movieon, 129)
TrayItemSetState($movieoff, 68)
TrayItemSetState($prefs, 128)
TraySetToolTip('System Movie-ized')
EndIf
EndFunc
Func _Exit() ;--- If not Run And Exit, checks to see if in MovieOn mode and asks user if they really want to exit
If Not $_term Then
TrayItemSetState($exit, 4)
If $_state And (MsgBox(49, 'Movie-ize', 'System currently Movie-ized!' & @CRLF & 'Do you want to quit in this state?') = 2) Then Return
EndIf
Exit
EndFunc
Func _about()
TrayItemSetState($about, 4)
MsgBox(64, 'Movie-ize', 'Movie-ize v0.22')
EndFunc
Func _prefs() ;--- Load the ini into users default editor and wait for exit, then re-read ini
TrayItemSetState($prefs, 4)
If Not $_state Then ;--- If in MovieOn mode then killed program state will be lost if user edits ini....so don't! >:^(
ShellExecuteWait($inifile)
Sleep(1000)
$kill = IniReadSection($inifile, 'Programs')
$kparam = IniReadSection($inifile, 'Parameters')
Else
MsgBox(48, 'Movie-ize', 'System currently Movie-ized,' & @CRLF & 'Preferences will be unavailable.')
EndIf
EndFunc
Func _movieOn() ;--- Cycles thru program array, if there's a process match it kills it and sets corresponding element to 1
Local $i, $guineapig
If Not $_term Then
TrayItemSetState($movieon, 129)
TrayItemSetState($movieoff, 64)
TrayItemSetState($prefs, 128)
EndIf
$_state = True
For $i = 1 To $kill[0][0]
If $kill[$i][1] <> '' Then
$guineapig = StringMid($kill[$i][1], StringInStr($kill[$i][1], '\', 0, -1) + 1)
If ProcessExists($guineapig) Then
ProcessClose($guineapig)
$kill[$i][0] = 1
Else
$kill[$i][0] = 0
EndIf
EndIf
Next
_RefreshSystemTray()
If Not $_term Then TraySetToolTip('System Movie-ized')
EndFunc
Func _movieOff() ;--- Cycles thru program array looking for 1 element, if found tries to restart the associated program
Local $i, $exec, $wdir, $execcmd
If Not $_term Then
TrayItemSetState($movieon, 64)
TrayItemSetState($movieoff, 129)
TrayItemSetState($prefs, 64)
EndIf
$_state = False
For $i = 1 To $kill[0][0]
If $kill[$i][0] = 1 Then
If StringLeft($kill[$i][1], 1) = '>' Then
$exec = StringMid($kill[$i][1], 2)
$wdir = StringMid($kill[$i][1], 2, StringInStr($kill[$i][1], '\', 0, -1) - 2)
$execcmd = '"' & $exec & '" ' & $kparam[$i][1]
Run(@ComSpec & ' /c "' & $execcmd & '"', $wdir, @SW_HIDE)
Else
Run($kill[$i][1])
EndIf
Sleep(500)
EndIf
Next
If Not $_term Then
TraySetToolTip("Get out the popcorn 'cause it's movie time!")
FileDelete($tempini)
EndIf
EndFunc
Func _CreateBaseIni() ;--- Creates a basic ini file first time it's run
Local $i
IniWrite($inifile, 'Programs', 'Program1', 'C:\Program Files\Skype\skype.exe')
IniWrite($inifile, 'Programs', 'Program2', '>C:\Program Files\xchat\xchat.exe')
IniWrite($inifile, 'Parameters', 'Param1', '')
IniWrite($inifile, 'Parameters', 'Param2', '-d "E:\Programs Installed\xchat-pu70\settings"')
EndFunc
Func _TempIni() ;--- For Run And Exit, writes a temporary ini file with initial program states, (QaD)
Local $i
For $i = 1 To $kill[0][0]
IniWrite($tempini, 'Start', 'Start' & $i, $kill[$i][0])
IniWrite($tempini, 'Programs', 'Program' & $i, $kill[$i][1])
IniWrite($tempini, 'Parameters', 'Param' & $i, $kparam[$i][1])
Next
EndFunc
Func _RunAndExit() ;--- If the temporary ini exists, assumes already run once and refills array with correct values before calling _movieOff
Local $i ;--- otherwise call _movieOn then _TempIni to write list of killed programs
If FileExists($tempini) Then
$xterm = IniReadSection($tempini, 'Start')
$kill = IniReadSection($inifile, 'Programs')
$kparam = IniReadSection($inifile, 'Parameters')
For $i = 1 To $xterm[0][0]
$kill[$i][0] = $xterm[$i][1]
Next
_movieOff()
FileDelete($tempini)
Else
_movieOn()
_TempIni()
EndIf
_Exit()
EndFunc
Func _RefreshSystemTray() ;--- The magic that clears the SysTray of dead icons courtesy of a UDF by Tuape & Erik Pilsits
$count = _SysTrayIconCount()
For $i = $count - 1 To 0 Step -1
$handle = _SysTrayIconHandle($i)
$visible = _SysTrayIconVisible($i)
$pid = WinGetProcess($handle)
$name = _ProcessGetName($pid)
$title = WinGetTitle($handle)
$tooltip = _SysTrayIconTooltip($i)
If $pid = -1 Then _SysTrayIconRemove($i)
Next
If _OsVersionTest($VER_GREATER_EQUAL, 6, 1) Then
$countwin7 = _SysTrayIconCount(2)
For $i = $countwin7 - 1 To 0 Step -1
$handle = _SysTrayIconHandle($i, 2)
$visible = _SysTrayIconVisible($i, 2)
$pid = WinGetProcess($handle)
$name = _ProcessGetName($pid)
$title = WinGetTitle($handle)
$tooltip = _SysTrayIconTooltip($i, 2)
If $pid = -1 Then _SysTrayIconRemove($i, 2)
Next
EndIf
EndFunc
UPDATE: v0.22 - Cleaned up some stupid typos, removed some stuff that I forgot.
- Now sets the tray menu items to disabled/enabled so you don't call things twice, (why didn't I think of that before?).
- Added ability to run and exit, just call it with a command line argument (writes a temp ini when it kills programs, deletes it after relaunching them).
- Added some comments to source, ('cause mouser told me I should).