Jump to content

Complete script noob needs help


Recommended Posts

Greetings All

First you should know that I am a complete scripting noob (I just delved into this yesterday) and my question will probably sound dumb to you so I apologize in advance. So I am trying to get a process to end when I press ESC. I am not looking for anything fancy, just have the script watch the process and if ESC is entered the process will end and the script will stop. From looking at other peoples scripts I have cobbled together the below but I believe something is wrong with my loop and I am unsure how to proceed. Currently the script runs but just closes right after it's run without doing anything. Any assistance would be much appreciated. 

HotKeySet("{ESC}", "Terminate")
sleep(100)
Func Terminate()
    ProcessClose ( "notepad.exe" )
    Exit 0
EndFunc
Link to comment
Share on other sites

hello Tweed, welcome to AutoIt and to the forum!

your script does exactly what you asked of it: register a hotkey, wait for 100ms, then exit. you are NOT asking your script to do anything else, like wait in idle, for example:

HotKeySet("{ESC}", "Terminate")

While True
    ; do something here - this is where your main action should be
    sleep(100) ; this is just here to keep the CPU from burning
WEnd

Func Terminate()
    ProcessClose ( "notepad.exe" )
    Exit 0
EndFunc

also, when you post code to the forum, use the code tags - that makes your script much more readable. use the "A" button (tooltip: "Code") on the toolbar menu.

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Hmmm how about how I changed it...would that work?

hello Tweed, welcome to AutoIt and to the forum!

your script does exactly what you asked of it: register a hotkey, wait for 100ms, then exit. you are NOT asking your script to do anything else, like wait in idle, for example:

HotKeySet("{ESC}", "Terminate")

While True
    ProcessExists(notepad.exe)
    sleep(100) ; this is just here to keep the CPU from burning
WEnd

Func Terminate()
    ProcessClose ( "notepad.exe" )
    Exit 0
EndFunc

also, when you post code to the forum, use the code tags - that makes your script much more readable. use the "A" button (tooltip: "Code") on the toolbar menu.

Link to comment
Share on other sites

other than being a syntax error, that won't give you anything. the line you add just computes the existence of notepad.exe, but it does not DO anything with it.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

You did not really change anything by adding the ProcessExists.. What was the intention, exit script if it does not exist?

No...I want it to do nothing at all unless I press ESC...then if I press ESC to end the process "Notepad.exe"

Currently I run it, its jumps up for a split second and ends...looking for it to keep going until I press ESC.

Edited by Tweed
Link to comment
Share on other sites

No...I want it to do nothing at all unless I press ESC...then if I press ESC to end the process "Notepad.exe"

Currently I run it, its jumps up for a split second and ends...looking for it to keep going until I press ESC.

That's exactly what orbs' script does.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

then you don't need it. ProcessClose() will fail if the target process does not exist, but it will not crush your script. your script will exit as planned no matter the result of ProcessClose(), so preliminary check is redundant.

do you mean you want your script to accept the Esc key only when notepad is running?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

HotKeySet("{ESC}", "Terminate")

While True
    ;ProcessExists(notepad.exe)
    sleep(100) ; this is just here to keep the CPU from burning
WEnd

Func Terminate()
    ProcessClose ( "notepad.exe" )
    ;Exit 0
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

That's exactly what orbs' script does.

Oh LOL sorry...I thought he was asking me to insert something into his script hehe spent the last couple hours searching for what to put in it...didn't realize that was what I was looking for. Also just saw your sig...reading it now.

Link to comment
Share on other sites

Oh LOL sorry...I thought he was asking me to insert something into his script hehe spent the last couple hours searching for what to put in it...didn't realize that was what I was looking for. Also just saw your sig...reading it now.

 

sorry for confusing you. the point was that the main action of your script is actually doing nothing in a loop. it is more common that a script is actually doing something, which may be interrupted by a hot key.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

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