0

I want to create a "Kingsoft Writer" script. Below is the script.

IfWinActive, ahk_class QWidget
{
sleep 2000
Click 486, 15
Click 570, 93
Send (various keystrokes)
Return
}

The purpose of the script is to:

I want to use a program called "Kingsoft Writer".

Kingsoft Writer is a freeware program that has all the functionality of Microsoft Word. But Kingsoft Writer loads quickly and is not bloated with slow loading bells and whistles that are rarely used. Also the interface is much more user friendly.

I want to program "Kingsoft Writer" to open all text files by default.

I want to send mouse clicks and keystrokes to this newly opened "Kingsoft Writer" window so that I can make the interface a really nice looking and simple interface. It will have a background color of my choosing. It will show only 1 toolbar instead of 5 or 6 like with Microsoft Word. In other words, the interface will be a major improvement over the Microsoft Word interface.

The most important feature improvement will be how fast Kingsoft Writer loads compared to Microsoft Word. The difference is huge.

But yet it will still have all the functionality of Microsoft Word. And it is a free download!

So I need to create a script that probably does not have a hotkey defined.

I need to create a script that runs whenever the "Kingsoft Writer" window appears.

I googled all the relevant words for 2 hours. But I'm still lacking any idea as to how to create this script.

Please give me a few ideas to help me figure this out.

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
  • I think your question is "How do I run a script every time I launch a particular program?" The simple solution is to switch from launching the program to launching the script, and having the script launch the program. (If that is indeed your question, then please remove all the extraneous discussion from your question.) – Raymond Chen Mar 24 '13 at 13:23
  • Maybe your are right Raymond. Maybe the question should be "How do I run a script every time I launch a particular program?. But I'm a little confused. I'm just not 100% sure if that's the question I'm really trying to ask. Can someone offer any thoughts about this? – Melvin Tyner Mar 24 '13 at 13:48
  • Glad to see you have not given up, after your previous question was closed before I could answer. – Robert Ilbrink Mar 24 '13 at 13:50
  • B.t.w. "programming KingSoft" to open all types of text files should be done through setting file associations. An easy way is to right click on e.g. a .doc and select open with, then select KingSoft and set as "Default". – Robert Ilbrink Mar 24 '13 at 17:36

1 Answers1

0

Unfortunately, you can't use IfWinActive to trigger a script the way you want it. There are solutions though.

One way is to run a small test script every 100 to 500 ms, to see if KingSoft is active. Alternatively, you can use some system calls to trigger a test script every time the focus to any window changes. Here is an example!

Option 1 with ShellMessage

#SingleInstance
#installKeybdHook
#Persistent
Global SwitchCounter

Gui +LastFound 
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam )
{
    If (wParam = 4)
    { 
        IfWinActive, ahk_class QWidget
        {
            Sleep 2000
            Click 486, 15
            Click 570, 93
            ;Send (various keystrokes)
            Return
        }
    }
}
Return

Option 2 check with fixed time interval

#SingleInstance
#installKeybdHook
#Persistent
SetTimer, CheckWin, 1000 ; This is executed on startup of AutoHotKey 
Return ; Anything below this point is not executed on startup

CheckWin:
WinGetTitle, CurrName, A
If (CurrName = "KingSoft")
{
    if (CurrName = PrevName)
        Return ; Only continue if an other application was activated
    PrevName = %CurrName% ; When new app, then sync CurrName and PrevName
    SoundBeep, 500, 200
    TrayTip, KingSoft, Activated, 2
}
Return

I only generate a soundbeep and show a message in the system tray. The time interval is set to 1000 ms, because the soundbeep alone takes 200 ms. The timer based solution would constantly beep therefore I check for a change in the app name. The ShellMessage one will only beep when you put KingSoft in focus by default.

This script came from move GUI and daughter together, where we trigger an action when the calculator and an other application should behave the same (both minimize or restore at the same time)

If it is just about detecting if an application is running, then the use of settimer and do a ifWinExist every 200 ms might be easier (though the ShellMessage is nicer).

Community
  • 1
  • 1
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • Oh b.t.w. Do I understand that you want to make a kind of "floating" menu. If so, you might check [this](http://stackoverflow.com/questions/15128707/autohotkey-trigger-script-when-a-certain-button-was-clicked-in-a-window/15135710#15135710) out! – Robert Ilbrink Mar 24 '13 at 14:06
  • Thanks Robert, I am studying your solution. I just hope it isn't over my head. I'm a newbie. – Melvin Tyner Mar 24 '13 at 14:08
  • I looked at your solutions. Nice! But I'm wondering. Does anyone have something a little more on the simple side. – Melvin Tyner Mar 24 '13 at 14:18
  • Then, you must write down what it is you want. You have seen what is possible (a floating menu that follows the main application behaviour). I don't know if you want to create a floating menu yourself. If so, then use the GUI maker in SciTE4autohotkey (makes life easier) – Robert Ilbrink Mar 24 '13 at 14:21
  • I need some time to study your solutions. They may be what I am looking for. I just don't realize that this is true at this moment. Your efforts are greatly appreciated. – Melvin Tyner Mar 24 '13 at 14:27
  • Robert, Can you give me some sample code for "If it is just about detecting if an application is running, then the use of settimer and do a ifWinExist every 200 ms might be easier (though the ShellMessage is nicer)."? – Melvin Tyner Mar 24 '13 at 14:49