Jump to content

Event Script To Close Windows...


Recommended Posts

Hey guys,

I have attempted to write a script that will:

1. sit in the system tray / memory until I press a key combination ( Ctrl + R )

2. Then check for a process and certain windows, and if found close the foreground one.

I am new to this programming stuff but thanks to some of you learning more and more each day.

I would like to know:

1. If you swapped out the windows names for windows you have would this / does this script work for you?

2. Is there a way to compact this script even more?

3. How do you minimize cpu load / run the most efficient code possible?

4. Any obvious mistakes ( I'm sure there are ) that you can see correct me on?

I"m never looking for just a "Here you go it's fixed" but more so the help / hints to guide me along to actually do teh fixing for myself.

Here is my proggie:

CODE

;==============================================================================

; Description: Close Remedy Window When Pressing Ctrl+R

; AutoIT Version: 3.1.114 beta

; Author: neoborn

; Creation Date: 30th March 2006

;==============================================================================

#include "E:\Program Files\AutoIt3\Include\GuiConstants.au3"

#include "E:\Program Files\AutoIt3\Include\Array.au3"

#include "E:\Program Files\AutoIt3\Include\File.au3"

Opt ("TrayIconDebug", 1)

Opt ("GuiOnEventMode", 0)

Opt ("GuiCloseOnEsc", 1)

opt ("TrayMenuMode", 0) ; Default tray menu items (Script Paused/Exit) will not be shown.

;opt("TrayOnEventMode", 1) ;TraySetClick (16); right click

Dim $avArray[4]

$avArray[0] = "Enterprise Services Helpdesk - [ES_HD (Modify)]"

$avArray[1] = "Enterprise Services Helpdesk - [ES_HD (New)]"

$avArray[2] = "Enterprise Services Helpdesk - [ES_HD (Search)]"

;-----------------------------------------------------------------------------

; Pseudo Code

; 1. Sits and sleeps in system tray until

; 2. Ctrl + r are pressed causing the function to run

; 3.

;-----------------------------------------------------------------------------

HotKeySet("^r", "ClsWin")

$i = 0

Do

Sleep(250)

Until $i = 1

$title = WinGetTitle($avArray[0], "") Or WinGetTitle($avArray[1], "") Or WinGetTitle($avArray[2], "")

Func ClsWin()

If ProcessExists("aruser.exe") And $title = 0 Then

WinClose($title, "")

EndIf

EndFunc ;==>ClsWin

Thank you, to all that help.

Neoborn.

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

CODE

;==============================================================================

; Description: Close Remedy Window When Pressing Ctrl+R

; AutoIT Version: 3.1.114 beta

; Author: neoborn

; Creation Date: 30th March 2006

;==============================================================================

#include <GuiConstants.au3>

#include <Array.au3>

#include <File.au3>

Opt ("TrayIconDebug", 1)

Opt ("GuiOnEventMode", 0)

Opt ("GuiCloseOnEsc", 1)

opt ("TrayMenuMode", 0) ; Default tray menu items (Script Paused/Exit) will not be shown.

;opt("TrayOnEventMode", 1) ;TraySetClick (16); right click

Dim $avArray[4]

$avArray[0] = "Enterprise Services Helpdesk - [ES_HD (Modify)]"

$avArray[1] = "Enterprise Services Helpdesk - [ES_HD (New)]"

$avArray[2] = "Enterprise Services Helpdesk - [ES_HD (Search)]"

;-----------------------------------------------------------------------------

; Pseudo Code

; 1. Sits and sleeps in system tray until

; 2. Ctrl + r are pressed causing the function to run

; 3.

;-----------------------------------------------------------------------------

HotKeySet("^r", "ClsWin")

$i = 0

Do

Sleep(250)

Until $i = 1

;this won't do anything since it isn't in the function and it outside the do loop

$title = WinGetTitle($avArray[0], "") Or WinGetTitle($avArray[1], "") Or WinGetTitle($avArray[2], "")

Func ClsWin()

;checks to see if the widows exists. If a window does exist, the WinExists function returns a 1. If the total is 3 then all

;the windows exist

$title = WinExists ( $avArray[0]) + WinExists($avArray[1]) + WinExists($avArray[2])

;the process exists all three windows exist

If ProcessExists("aruser.exe") And $title = 3 Then

WinClose($avArray[0])

WinClose($avArray[1])

WinClose($avArray[2])

EndIf

EndFunc ;==>ClsWin

Don't know what you mean by swapping the window names.

Fixed errors.

Wrote code to do what you requested.

How large is your current compiled script? How much RAM is it using?

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

It appears that you're using the do/until loop to be infinite ($i never becomes 1). An easier way to achieve this is to use While 1 / Wend.

Instead of:

$i = 0
Do
    Sleep (250)
Until $i = 1

Do:

While 1
    Sleep (250)
Wend

You can also increase the sleep since you're just waiting for a hotkey.

According to the helpfile:

"Maximum sleep time is 2147483647 milliseconds (24 days)."

Link to comment
Share on other sites

Don't know what you mean by swapping the window names.

Fixed errors.

Wrote code to do what you requested.

How large is your current compiled script? How much RAM is it using?

1. I was saying that you could just swap out the names of those windows and enter something else for a program you may have for testing purposes.

2. Thank you for fixing errors, though your program closes all three windows I want to check the foreground window that is currently open in the program ( aruser.exe ) and if it matches any of those three then close it when I press the hotkey.

Basically the program is a helpdesk ticket program where I may have many ticket windows open and when I press my hotkey I want it to close the foreground window if it matches any of the window titles I have given, then go back to the tray and wait :)

3. I'm not sure on size or ram. How do you make sure a program uses the least resources to do what you want?

Thanks for helping :mellow:

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

It appears that you're using the do/until loop to be infinite ($i never becomes 1). An easier way to achieve this is to use While 1 / Wend.

While 1
    Sleep (250)
Wend

You can also increase the sleep since you're just waiting for a hotkey.

According to the helpfile:

"Maximum sleep time is 2147483647 milliseconds (24 days)."

1. Why is While better than Do in this case ( not sure of the differences between the two even though I have rtfm )?

2. Can I increase the sleep time to 24 days then and still have it work fine?

Thanks for helping :)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

  • Moderators

1. Why is While better than Do in this case ( not sure of the differences between the two even though I have rtfm )?

2. Can I increase the sleep time to 24 days then and still have it work fine?

Thanks for helping :)

I don't think it was necessarily a "better than", but it is at least 1 line shorter in code :mellow:. I also think there was a speed test done with loops quite a few months ago, showing that While/WEnd was faster than Do/Until if I'm not mistaken.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I don't think it was necessarily a "better than", but it is at least 1 line shorter in code :). I also think there was a speed test done with loops quite a few months ago, showing that While/WEnd was faster than Do/Until if I'm not mistaken.

Kewl ....what about question #2 ? :mellow:

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

So then?

CODE

;==============================================================================

; Description: Close Remedy Window When Pressing Ctrl+R

; AutoIT Version: 3.1.114 beta

; Author: neoborn

; Creation Date: 30th March 2006

;==============================================================================

#include <GuiConstants.au3>

#include <Array.au3>

#include <File.au3>

Opt ("TrayIconDebug", 1)

Opt ("GuiOnEventMode", 0)

Opt ("GuiCloseOnEsc", 1)

opt ("TrayMenuMode", 0) ; Default tray menu items (Script Paused/Exit) will not be shown.

Dim $avArray[4]

$avArray[0] = "Enterprise Services Helpdesk - [ES_HD (Modify)]"

$avArray[1] = "Enterprise Services Helpdesk - [ES_HD (New)]"

$avArray[2] = "Enterprise Services Helpdesk - [ES_HD (Search)]"

;----------------------------------------------------------------------------------------------

; Pseudo Code

; 1. Sits and sleeps in system tray until

; 2. Ctrl + r are pressed causing the function to run

; 3. The function checks to see if the process & windows exist then kill the foreground window

;----------------------------------------------------------------------------------------------

HotKeySet("^r", "ClsWin")

While 1

Sleep(2147483647)

Until $i = 1

Func ClsWin()

;checks to see if the widows exists. If a window does exist, the WinExists function returns a 1.

$title = WinExists( $avArray[0]) + WinExists($avArray[1]) + WinExists($avArray[2])

;the process exists any of the three windows exist

If ProcessExists("aruser.exe") And $title = 1 Then

WinClose($title)

EndIf

EndFunc ;==>ClsWin

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

  • Moderators

I think i wrote a script to do #2... Maybe Valuater still has it in scripts and scraps in his autoit wrappers thread.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Ok... I didn't have one in there... This should (haven't tested it) kill the active/visible window from the .exe you give it.

While 1
    ProcessGetWinKillClose('notepad.exe', 1)
WEnd

Func ProcessGetWinKillClose($s_Process, $v_Kill_Close = 1); 1 = Kill, 2 = Close
    Local $i_ProcessPID = ProcessList()
    Local $a_WinList = WinList()
    Local $i_WinPID = ''
    For $x = 1 To $i_ProcessPID[0][0]
        If $s_Process = $i_ProcessPID[$x][0] Then
            For $i = 1 To $a_WinList[0][0]
                $i_WinPID = WinGetProcess($a_WinList[$i][0])
                If $i_WinPID = $i_ProcessPID[$x][1] And $a_WinList[$i][0] <> '' Then
                    If WinActive($a_WinList[$i][0]) Then
                        If $v_Kill_Close = 1 Then
                            WinKill($a_WinList[$i][0])
                            ExitLoop
                            Return 0
                        ElseIf $v_Kill_Close = 2 Then
                            WinClose($a_WinList[$i][0])
                            Return 0
                        Else
                            SetError(1)
                            Return 0
                        EndIf
                    EndIf
                EndIf
            Next
        EndIf
    Next
EndFunc

Edit:

Had a chance to test this one, it works.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

So then?

[...]

While 1

Sleep(2147483647)

Until $i = 1

[...]

That needs to be:

While 1

Sleep(2147483647)

WEnd

Until is only for Do loops.

It doesn't really matter how fast the While loop is vs the Do loop, because you never actually do any looping - you're just sleeping the whole time. The reason I suggested it was because it doesn't require the use of a variable.

Link to comment
Share on other sites

True at the WEND

So then I could use Sleep(1)?

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

I normally use Sleep (10) for loops that actually do something, and Sleep (1000) or higher for loops that aren't doing anything. It really doesn't matter how big the sleep is because the hotkey will interrupt it whenever you decide to press it. The purpose of the sleep is just to help the CPU.

Basically, any sleep above... 100.. should do fine. I'd go for 1000+.

Link to comment
Share on other sites

kewl ty .... wanna allow me add to your aim / msn / message program? Please :)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

I'd rather not hand out my stuff, but you can PM me...

k, sweet.

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

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...