 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
aahkuser
Joined: 01 May 2009 Posts: 6
|
Posted: Fri May 01, 2009 4:27 pm Post subject: Run Script on Program Load |
|
|
I have several programs I open/use every day. Some days I shut the machine off, mostly I use Hibernate. When I open these programs, I now use keyboard shortcuts to (1) Maximize the screen, (2) go to a specific part of the program, and (3) open a prompt, etc.
I cannot figure out how to get AHK to recognize that a program (Microsoft Word for instance) just opened and when that program finishes loading, it runs a script. Can someone let me know if this is possible. I've searched the Community, and searched on Google without any luck. |
|
| Back to top |
|
 |
Phaze
Joined: 14 Sep 2007 Posts: 133 Location: Wichita, Kansas
|
Posted: Fri May 01, 2009 5:13 pm Post subject: |
|
|
Why not show us what you have already?
Also, we need to know what program (I know you said Word, but you also said for example), and what you want it to do. _________________
 |
|
| Back to top |
|
 |
aahkuser
Joined: 01 May 2009 Posts: 6
|
Posted: Mon May 04, 2009 10:26 pm Post subject: |
|
|
This is the example for a program I use at work. I need to (1) cancel a prompt window, (2) align the window to the top left, (3) re-size the program window, (4) press a button to view the main window.
This all works fine, but you requested the code, etc. The problem I have is that whenever I load the program (usually once a day, but some times several times) and it's annoying pressing the key combination each time. I want it to do this when the program is done loading, is it possible to have on_formload or something like that similar to Microsoft Access?
| Code: |
#IfWinActive Winworks AutoShop ahk_class #32770
^!+d::
{
Send, n
Sleep, 275
WinGetPos, InitialWindowPosX, InitialWindowPosY, InitialWindowSizeX, InitialWindowSizeY
If InitialWindowPosX <> 0
{
If InitialWindowPosY <> 0
{
WinMove, 0,0
}
}
MouseClickDrag, Left, 100,749,100,769
Sleep, 100
MouseClick, left, 202, 78
}
return
|
(2) I have the same sort of thing randomly in Windows Media Player where it loads the "Now Playing" window and when the program loads, I want it to automatically do browser back. |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Tue May 05, 2009 4:03 pm Post subject: |
|
|
EDIT:
I have created a kind of template.
Hope it fits your needs.
| Code: | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
; OnLoad/OnClose Script Template
; by MasterFocus
;
; This template is provided containing 4 examples.
; You may remove them after learning how it works.
;
;
; *** HOW TO ADD A PROGRAM to be checked OnLoad/OnClose:
;
; 1. Add a variable named ProgWinTitle# (Configuration Section)
; containing the desired window's title
;
; 2. Add two labels named LabelOnLoad# and LabelOnClose#
; (Custom Labels Section) containing the desired actions
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#Persistent
; ------ ------ CONFIGURATION SECTION ------ ------
; Program Titles
ProgWinTitle1 = ahk_class Notepad
ProgWinTitle2 = ahk_class WhateverClass
ProgWinTitle3 = Windows Title Here
ProgWinTitle4 = Another Program Title
; DO NOT FORGET to add the custom
; labels at the bottom of template
; ( LabelOnLoad# and LabelOnClose# )
; SetTimer Period
CheckPeriod = 750
; ------ END OF CONFIGURATION SECTION ------ ------
Loop
{
if !ProgWinTitle%A_Index%
break
ProgRunning%A_Index% := 0
ProgLoaded%A_Index% := 0
}
SetTimer, LabelCheckLoadClose, %CheckPeriod%
Return
; ------ ------ ------
LabelCheckLoadClose:
Loop
{
if !ProgWinTitle%A_Index%
break
if (!ProgRunning%A_Index% AND WinExist(ProgWinTitle%A_Index%)) OR
(ProgRunning%A_Index% AND !WinExist(ProgWinTitle%A_Index%))
ProgRunning%A_Index% := !ProgRunning%A_Index%
if (ProgRunning%A_Index% != ProgLoaded%A_Index%)
{
if !ProgLoaded%A_Index%
GoSub, LabelOnLoad%A_Index%
else
GoSub, LabelOnClose%A_Index%
ProgLoaded%A_Index% := !ProgLoaded%A_Index%
}
}
Return
; ------ ------ CUSTOM LABEL SECTION ------ ------
LabelOnLoad1:
MsgBox, You just loaded Notepad!
Return
LabelOnClose1:
MsgBox, You just closed Notepad!
Return
LabelOnLoad2:
MsgBox, You opened a WhateverClass window!
Return
LabelOnClose2:
Return
LabelOnLoad3:
Return
LabelOnClose3:
MsgBox, You closed your 3rd customized window.
Return
LabelOnLoad4:
Msgbox, You opened Another Program!
LabelOnClose4:
Return
; ------ END OF CUSTOM LABEL SECTION ------ ------ |
Example 1 - Alerts when loaded and also closed
Example 2 - Alerts when loaded only
Example 3 - Alerts when closed only
Example 4 - Alerts when loaded only using a single "return"
=============================================================
Also, instead of
| Code: | WinGetPos, InitialWindowPosX, InitialWindowPosY, InitialWindowSizeX, InitialWindowSizeY
If InitialWindowPosX <> 0
{
If InitialWindowPosY <> 0
{
WinMove, 0,0
}
} |
you may try this:
| Code: |
WinGetPos, InitialWindowPosX, InitialWindowPosY
If InitialWindowPosX AND InitialWindowPosY
WinMove, 0,0 |
(don't know if you're using InitialWindowPos, but seems unnecessary) |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Wed May 06, 2009 10:22 am Post subject: |
|
|
Nice work, MasterFocus. I've made a few tweaks:
| Code: | ; Original script by MasterFocus. See original post for instructions.
#Persistent
; ------ ------ CONFIGURATION SECTION ------ ------
; Program Titles
ProgWinTitle1 = ahk_class Notepad
ProgWinTitle2 = Calculator
; SetTimer Period
CheckPeriod = 750
; ------ END OF CONFIGURATION SECTION ------ ------
SetTimer, LabelCheckLoadClose, %CheckPeriod%
Return
; ------ ------ ------
LabelCheckLoadClose:
Loop
{
; This type of 'if' is typically more efficient than an 'if !expression':
if ProgWinTitle%A_Index% =
break
; Using '!' on both values allows them to be compared as boolean 0 or 1, where
; WinExist() normally returns 0 or a window ID. It also allows the script to
; skip initializing ProgRunning%A_Index%.
if (!ProgRunning%A_Index% != !WinExist(ProgWinTitle%A_Index%))
{
; Toggle variable and use new value as the 'if' condition.
if ProgRunning%A_Index% := !ProgRunning%A_Index%
GoSubSafe("LabelOnOpen" A_Index)
else
GoSubSafe("LabelOnClose" A_Index)
}
}
return
; Simple helper function for brevity:
GoSubSafe(Sub) {
; Attempt to run subroutine only if label exists. This allows OnOpen
; to be defined without OnClose, or OnClose without OnOpen.
if IsLabel(Sub)
GoSub %Sub%
}
; ------ ------ CUSTOM LABEL SECTION ------ ------
LabelOnOpen1: ; First program was opened.
LabelOnClose1: ; First program was closed.
LabelOnOpen2: ; Second program was opened. Note no closed notification.
MsgBox % A_ThisLabel
return
; ------ END OF CUSTOM LABEL SECTION ------ ------
| Notable differences:
- Shorter.
- Less dynamic variable references. This should prove a little more efficient.
- OnOpen instead of OnLoad. I figure "Open" more naturally opposes "Close".
- OnOpen can be defined without OnClose, and vice versa.
|
|
| Back to top |
|
 |
aahkuser
Joined: 01 May 2009 Posts: 6
|
Posted: Wed May 06, 2009 5:08 pm Post subject: |
|
|
Thanks a lot. This is take some time to go through, lol. But it looks like its exactly what I need.
Also, I have been looking for an Intellisense editor for AHK (more or less a copy of the VBA editor in M-Office). Does anyone have a recommendation? |
|
| Back to top |
|
 |
lilalurl.T32
Joined: 17 May 2007 Posts: 391 Location: Titan
|
|
| Back to top |
|
 |
aahkuser
Joined: 01 May 2009 Posts: 6
|
Posted: Thu May 07, 2009 6:45 am Post subject: |
|
|
I'll have to look at the thread more, but I don't think it's enough, because I also need to be exactly like VBA in that it makes "; comments" green (or some color), reminds me to close a bracket, in addition to the auto-complete.
I looked at the AHK Editor and one another one, but didn't find them similar at all and have just been using Notepad from then on. |
|
| Back to top |
|
 |
bqw371
Joined: 26 Nov 2008 Posts: 32
|
Posted: Thu May 07, 2009 7:16 am Post subject: |
|
|
| aahkuser wrote: | | Also, I have been looking for an Intellisense editor for AHK (more or less a copy of the VBA editor in M-Office). Does anyone have a recommendation? |
Is this what you are looking for?
http://www.autohotkey.com/forum/topic41503.html |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Mon May 11, 2009 7:30 pm Post subject: |
|
|
Lexikos, you pointed out some interesting things I did not know.
Thanks for the modifications. |
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 384
|
Posted: Mon May 11, 2009 10:52 pm Post subject: |
|
|
I use Bill2's Process Manager to control priorities and affinities. It has the option to launch side programs when the desired process starts or ends ans since I'm running it anyway, I use it to launch similar add-on scripts that you described. The program would be overkill for script launching alone, but I'd recommend it to anyone who is also looking for priotity and affinity management for a multi-core machine.
Warning: To be useful, Bill2 needs to be run in learning mode at first, which consists of lots of pop ups each time a new process is detected asking if you want to set a rule for it. The pop ups are annoying, but once you have Bill2 set up, it can be of great value. _________________ Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|