owenG
home learn tableau about
divider
legalific swsom swsom diagram swapps ahk ue








AutoHotKey


Automate mouse functions to the keyboard

AutoHotKey can communicate with other programs and technologies in a number of different ways, from calls that are part of its codebase (e.g. DllCall function) to more ad-hoc methods that have been created by individual users (google for 'AutoHotkey Lisp'). Coming in from the second area is Windows Scripting for AutoHotkey, an .ahk script that enables access to VBScript, JScript and COM. There are apparently other ways of using VBScript in AutoHotKey but this third-party script makes life much easier. Once VBScript is available it is possible to call functions from the Summation API just as if the code was originating from a .vbs file.

After downloading the Windows Scripting for AutoHotKey file (ws4ahk.ahk) from the site above, place that .ahk in the same directory as the primary .ahk file that will be running. Then reference the add-on file from your script:

ahk
    #include ws4ahk.ahk

Follow that with a section holding the VBScript code to be called by selected keystrokes. There is one function for displaying the current documents Next page in the Summation image viewer and another for going to the Previous page:

ahk
    Code = 
    (
	    'On Error Resume Next  'optional
	    Set swObj = CreateObject("Summation.Application")
    	
	    Sub GoToNextPage
		    totalPages = swObj.IMG.NumPages
		    thisPage = swObj.IMG.CurPage
		    If thisPage < totalPages Then
			    targetPage = thisPage + 1
			    swObj.IMG.GoToPage targetPage
		    End If
	    End Sub

	    Sub GoToPreviousPage
		    totalPages = swObj.IMG.NumPages
		    thisPage = swObj.IMG.CurPage
		    If thisPage > 1 Then
			    targetPage = thisPage - 1
			    swObj.IMG.GoToPage targetPage
		    End If
	    End Sub
    )

Next comes the two keystroke combo's that will be monitored by AutoHotKey and in turn call the VBScript functions. In this sample 'Control + PageDown' runs GoToNextPage while 'Control + PageUp' triggers GoToPreviousPage. The specific keystrokes can easily be changed but it is important to confirm beforehand that a given combo is not already assigned to some other action, totally outside of AutoHotKey. Each of the WS_* methods below are calls into the ws4ahk.ahk and are explained on the originating website. Also available at that site is the internal API documentation. Note that the the source code is right there in clear text in the ws4ahk script and can easily be reviewed to see what is going on behind the scenes.

ahk
    ^PGDN::
     WS_Initialize()
        WS_Exec(Code)
        WS_Exec("GoToNextPage")
     WS_Uninitialize()
    return

    ^PGUP::
     WS_Initialize()
        WS_Exec(Code)
        WS_Exec("GoToPreviousPage")
     WS_Uninitialize()
    return

There are some memory concerns when WS_Initialize is called thousands of times and it is quite probable the script above could be made more efficient. Something else to watch out for are the calls into the Summation Scripting Object Model, where errors may be swallowed instead of producing some kind of feedback. For example, if a Summation method or property of the object model is misspelled in the VBScript the executing procedure will silently fail at that point and not display an error dialog.


Additional thoughts:

  • Something like the SummationIsActive ahk function, described in another page, should be used to limit the script's behavior. Generally speaking, you should never use CreateObject on Summation.Application unless Summation is already running - doing so opens Summation into lightweight state in which not everything will work as expected.
    • It may even be possible to re-send the initiating keystrokes to the screen in cases where Summation is not the active window.
  • Could add in some related ahk/Summation functions that would also be helpful, e.g. for going directly to the first or last page of an image
  • May be possible to automate navigation of the edoc view window, by sending a 'PageDown' command to the target window. No doubt a little tricky but there is no reason to believe it cannot be done.
  • Even if the IMG.GoToPage function were not available in the Summation API it might still be possible to automate the Next/Previous page behavior. Since AutoHotKey can send mouse clicks to the screen, anything that can be clicked can be automated given the right circumstances. Assuming a given view/toolbar layout were guaranteed, a keystroke could be captured by AutoHotkey so that instead a click is sent to the exact screen coordinates where the 'Next Page' button is located. Of course complications ensue because the Next/Prev Page buttons don't appear unless the Image View has focus...

This particular example is relatively trivial and in fact could have been implemented on most systems without AutoHotKey. Simply create two .vbs scripts, one with code similar to that of GoToNextPage, and another like GoToPreviousPage. Drag a shortcut from the 'GoToNextPage.vbs' file onto the Windows Desktop, right-click and select Properties. On the Shortcut tab, click in the 'Shortcut key' box and press the PageUp key (or use a different key combo, Windows pre-pends with Ctrl+Alt by default), OK to save changes. Repeat steps for the other .vbs file, using PageDn instead. Now the appropriate .vbs script will be launched whenever the designated keystroke combination is used. Warning: as noted under 'Additional thoughts' a script like this should only be executed when Summation has already been opened manually.