Jump to content

Run AU3 file from within script and force close the script later on


 Share

Recommended Posts

I have been trying to figure out how to get this working all night but I have been unsuccessful. Wondering if anyone else has had any luck or knows how. I have a main script, and a secondary script within the same folder... I manually run the first script and at a specific point within my first script, I want it to open and start running the second AU3 file script (I do not want to compile this). I am able to get the script open with _RunAu3 but I am not able to close the script. I have also tried to open script with ShellExecute and Run but no luck with either of those. 

 

Does anyone have any ideas?

 

_RunAU3("2NDSCRIPT.AU3", '"This is a commandline example!"')

Func _RunAU3($sFilePath,  $sCommandLine = "", $sWorkingDir = "", $iShowFlag = @SW_SHOW, $iOptFlag = 0)
    $PID = Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sFilePath & '" ' & $sCommandLine, $sWorkingDir, $iShowFlag, $iOptFlag)
EndFunc   ;==>_RunAU3

 

 

The code above works to open the file, but WinKill($PID) does not close it 

Link to comment
Share on other sites

You should post both scripts so we're not guessing?  How to you pass the $PID to the other script?  Since you haven't declared the variable and it's within the function, it will automatically become a Local variable so won't be seen out side that function.

Link to comment
Share on other sites

@Subz the 2 scripts are not related in anyway or called anywhere other than what I posted above.. from my understanding, when I run 2ndscript.au3, I’m capturing the process ID which I should then be able to use to close the script at any given point within my other script, but that doesn’t seem to be the case 

Link to comment
Share on other sites

:blink: Sorry that doesn't make sense.  If you ran the script above it would just start the other script and exit, it doesn't pass anything on to the other script, the two scripts run independently of each other.  If you want to close the current script you would just use @AutoItPid basic example:

HotKeySet("{ESC}", "_ExitScript") ;~ Press Escape to exit

$iPID = Run(@ComSpec & " /k ") ;~ Start Command Prompt

While 1
    Sleep(100)
WEnd

Func _ExitScript()
    ProcessClose($iPID) ;~ Closes Command Prompt
    ProcessClose(@AutoItPID) ;~ Closes the current script
    ;~ Or
    Exit ;~ Closes the current script
EndFunc

 

Edited by Subz
Link to comment
Share on other sites

@Subz sorry I’m not sure I understand your response. 

 

The 2 scripts are independent on my 2ndscript I’m basically looking for conditions, when the first script is running, if there are certain conditions (that are not looked for in the first script) then I want the 2ndscript to notify me. And I want the 2ndscript to run at a specific point of the first script and stop when the first script ends

Link to comment
Share on other sites

@Somerset it’s 2 autoit scripts I am trying to run, neither are compiled (I’d prefer not to have to compile them). They both have different names... main is Script1.AU3 and second is 2ndscript.AU3 

 

on script1 I have a GUI and when I hit start on it, it runs the first script. When I hit start I also want it to run a secondary script (2ndscript.au3) until the first script stops or until I hit escape key which is the hotkey for first script to close. The 2ndscript.au3 file does not have a GUI...

 

reason I created a second script is because I am trying to look for specific conditions the majority of time that the first script is running. The first script has many functions and I have not been able to do this within 1 script 

Edited by Nick3399
Link to comment
Share on other sites

Post your code for both scripts, otherwise you'll get nothing but a bunch of guesses because we don't have a clue what you're doing now.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@Nick3399 As BrewManNH and I have stated, you should post your scripts or at least relevant parts of your scripts. The snippet you posted doesn't tell us which script is suppose to close the other, I was under the impression you were trying to close the first script from the second script.  But re-reading you just want to close the second script from the first when it is exited.  As I mentioned, this won't work with code you posted because $PID is in the local scope meaning as soon as _RunAU3 function is closed $PID is no longer available, you would need something like the following for it to work:

Global $iPID
HotKeySet("{ESC}", "_Exit")
_RunAU3("2ndScript.au3", '"This is a commandline example!"')

While 1
    Sleep(100)
WEnd

Func _RunAU3($sFilePath,  $sCommandLine = "", $sWorkingDir = "", $iShowFlag = @SW_SHOW, $iOptFlag = 0)
    $iPID = Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sFilePath & '" ' & $sCommandLine, $sWorkingDir, $iShowFlag, $iOptFlag)
EndFunc   ;==>_RunAU3

Func _Exit()
    If ProcessClose($iPID) Then MsgBox(4096, "Process Closed", "Process id: " & $iPID & " closed successfully.")
    Exit
EndFunc

 

Link to comment
Share on other sites

@Subz - I cannot post the codes because it violates the rules but yes you understand what I am trying to do. I want to open and close the 2nd script from within the first one. I just ran your code but it is still not killing the process when I add winkill and processclose

 

 

Global $iPID
HotKeySet("{ESC}", "_Exit")
_RunAU3("2ndScript.au3", '"This is a commandline example!"')

While 1
    Sleep(100)
WEnd

Func _RunAU3($sFilePath,  $sCommandLine = "", $sWorkingDir = "", $iShowFlag = @SW_SHOW, $iOptFlag = 0)
    $iPID = Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sFilePath & '" ' & $sCommandLine, $sWorkingDir, $iShowFlag, $iOptFlag)
EndFunc   ;==>_RunAU3

Func _Exit()

sleep(2000)
winkill($iPID)

    If ProcessClose($iPID) Then MsgBox(4096, "Process Closed", "Process id: " & $iPID & " closed successfully.")
    Exit
EndFunc
Link to comment
Share on other sites

Add some error checking to see why it doesn't close.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...