[function] SumatraInvoke() - a wm_command wrapper for SumatraPDF V3.1 & V3.2*

Post your working scripts, libraries and tools
User avatar
Frosti
Posts: 379
Joined: 27 Oct 2017, 14:30
GitHub: Ixiko

[function] SumatraInvoke() - a wm_command wrapper for SumatraPDF V3.1 & V3.2*

12 Jul 2020, 11:25

Similar to this function for FoxitReader [function] FoxitInvoke() - a wm_command wrapper for FoxitReader V9.*], I build a version for Sumatra PDF.
For explanation. The function calls menu commands in Sumatra PDF. You could of course send keyboard shortcuts, but in general, sending wm_commands is a lot more reliable.

Change History:
Update on 2020-11-30 - support for Sumatra PDF V3.2


Code: Select all

SumatraInvoke(command, SumatraID="") {                                                                     	;-- wm_command wrapper for Sumatra PDF V3.1 & 3.2

	/* DESCRIPTION of FUNCTION:  SumatraInvoke()

		                                                                               by Ixiko (version 30.11.2020)
		---------------------------------------------------------------------------------------------------
		a wm_command wrapper for Sumatra Pdf V3.1 & V3.2
		...........................................................
		Remark:
		- Sumatra PDF has changed all wm_command codes from V3.1 to V3.2
		- the script tries to automatically recognize the version of the addressed
		  Sumatra PDF process in order to send the correct commands
		- maybe not all commands are listed !
		---------------------------------------------------------------------------------------------------
		Parameters:
		- command:	the names are borrowed from menu or toolbar names. However,
							no whitespaces or hyphens are used, only letters
		- SumatraID:	by use of a valid handle, this function will post your command to
							Sumatra. Otherwise by use of a version string ("3.1" or "3.2") this
							function returns the wm_command code.
		...........................................................
		Rersult:
		- You have to control the success of postmessage command yourself!
		---------------------------------------------------------------------------------------------------

		---------------------------------------------------------------------------------------------------
		      EXAMPLES - EXAMPLES - EXAMPLES - EXAMPLES - EXAMPLES - EXAMPLES

		SumatraInvoke("ShowToolbar", "3.2")      SumatraInvoke("DoublePage", SumatraID)
		.................................................       ...................................................................
		this one only returns the Sumatra              sends the command "DoublePage" to
        command-code                                      your specified Sumatra process using
												 parameter 2 (SumatraID) as window handle.
													   command-code will be returned too
		---------------------------------------------------------------------------------------------------

	*/

		static	SumatraCmds
		local	SumatraPID

		If !IsObject(SumatraCmds) {

			SumatraCmds := Object()
			SumatraCmds["3.1"] := {  "NewWindow":                            0      	; not available in this version -dummy command
								, 	"Open":                             	400  	; File
								,	"Close":                             	401  	; File
								,	"ShowInFolder":                        	0      	; not available in this version -dummy command
								,	"SaveAs":                             	402  	; File
								,	"Rename":                              	580  	; File
								,	"Print":                               	403  	; File
								,	"SendMail":                           	408  	; File
								,	"Properties":                         	409  	; File
								,	"OpenLast1":                         	510  	; File
								,	"OpenLast2":                         	511  	; File
								,	"OpenLast3":                         	512  	; File
								,	"Exit":                                	405  	; File
								,	"SinglePage":                         	410  	; View
								,	"DoublePage":                       	411  	; View
								,	"BookView":                           	412  	; View
								,	"ShowPagesContinuously":            	413  	; View
								,	"MangaMode":                           	0      	; not available in this version -dummy command
								,	"TurnCounterclockwise":               	415  	; View
								,	"RotateLeft":                        	415  	; View
								,	"TurnClockwise":                    	416  	; View
								,	"RotateRight":                       	416  	; View
								,	"Presentation":                        	418  	; View
								,	"Fullscreen":                        	421  	; View
								,	"Bookmark":                          	000  	; View - do not use! empty call!
								,	"ShowToolbar":                      	419  	; View
								,	"SelectAll":                          	422  	; View
								,	"CopyAll":                           	420  	; View
								,	"NextPage":                           	430  	; GoTo
								,	"PreviousPage":                      	431  	; GoTo
								,	"FirstPage":                         	432  	; GoTo
								,	"LastPage":                            	433  	; GoTo
								,	"GotoPage":                          	434  	; GoTo
								,	"Back":                               	558  	; GoTo
								,	"Forward":                             	559  	; GoTo
								,	"Find":                                	435  	; GoTo
								,	"FitASinglePage":                    	[410, 440] ; to hat V3.2 function (not tested)
								,	"FitPage":                           	440  	; Zoom
								,	"ActualSize":                          	441  	; Zoom
								,	"FitWidth":                          	442  	; Zoom
								,	"FitContent":                          	456  	; Zoom
								,	"CustomZoom":                        	457  	; Zoom
								,	"Zoom6400":                           	443  	; Zoom
								,	"Zoom3200":                           	444  	; Zoom
								,	"Zoom1600":                           	445  	; Zoom
								,	"Zoom800":                          	446  	; Zoom
								,	"Zoom400":                          	447  	; Zoom
								,	"Zoom200":                          	448  	; Zoom
								,	"Zoom150":                          	449  	; Zoom
								,	"Zoom125":                          	450  	; Zoom
								,	"Zoom100":                          	451  	; Zoom
								,	"Zoom50":                            	452  	; Zoom
								,	"Zoom25":                            	453  	; Zoom
								,	"Zoom12.5":                          	454  	; Zoom
								,	"Zoom8.33":                          	455  	; Zoom
								,	"AddPageToFavorites":               	560  	; Favorites
								,	"RemovePageFromFavorites":           	561  	; Favorites
								,	"ShowFavorites":                      	562  	; Favorites
								,	"CloseFavorites":                     	1106  	; Favorites
								,	"CurrentFileFavorite1":              	600  	; Favorites
								,	"CurrentFileFavorite2":              	601  	; Favorites -> I think this will be increased with every page added to favorites
								,	"ChangeLanguage":                   	553  	; Settings
								,	"Options":                          	552  	; Settings
								,	"AdvancedOptions":                  	597  	; Settings
								,	"VisitWebsite":                     	550  	; Help
								,	"Manual":                           	555  	; Help
								,	"CheckForUpdates":                  	554  	; Help
								,	"About":                             	551}  	; Help
			SumatraCmds["3.2"] := { 	"NewWindow":                      	450  	; File
								, 	"Open":                               	400  	; File
								,	"Close":                               	404  	; File
								,	"ShowInFolder":                        	410  	; File
								,	"SaveAs":                             	406  	; File
								,	"Rename":                              	610  	; File
								,	"Print":                              	408  	; File
								,	"SendMail":                           	418  	; File
								,	"Properties":                         	420  	; File
								,	"OpenLast1":                         	570  	; File
								,	"OpenLast2":                         	571  	; File
								,	"OpenLast3":                         	572  	; File
								,	"Exit":                                	412  	; File
								,	"SinglePage":                         	422  	; View
								,	"DoublePage":                       	423  	; View
								,	"BookView":                           	424  	; View
								,	"ShowPagesContinuously":              	425  	; View
								,	"MangaMode":                          	426  	; View
								,	"RotateLeft":                         	432  	; View
								,	"RotateRight":                       	434  	; View
								,	"Presentation":                        	438  	; View
								,	"Fullscreen":                          	444  	; View
								,	"Bookmark":                          	000  	; View - do not use! empty call!
								,	"ShowToolbar":                      	440  	; View
								,	"SelectAll":                           	446  	; View
								,	"CopyAll":                            	442  	; View
								,	"NextPage":                           	460  	; GoTo
								,	"PreviousPage":                      	462  	; GoTo
								,	"FirstPage":                          	464  	; GoTo
								,	"LastPage":                            	466  	; GoTo
								,	"GotoPage":                          	468  	; GoTo
								,	"Back":                              	596  	; GoTo
								,	"Forward":                             	598  	; GoTo
								,	"Find":                                	470  	; GoTo
								,	"FindNext":                          	472  	; Toolbar
								,	"FindPrevious":                        	474  	; Toolbar
								,	"MatchCase":                          	476  	; Toolbar
								,	"FitWithContinuously":               	3026	; Toolbar
								,	"FitASinglePage":                    	3027	; Toolbar
								,	"ZoomIn":                            	3012	; Toolbar
								,	"ZoomOut":                            	3013	; Toolbar
								,	"FitPage":                           	480  	; Zoom
								,	"ActualSize":                          	481  	; Zoom
								,	"FitWidth":                          	482  	; Zoom
								,	"FitContent":                          	496  	; Zoom
								,	"CustomZoom":                        	497  	; Zoom
								,	"Zoom6400":                           	483  	; Zoom
								,	"Zoom3200":                           	484  	; Zoom
								,	"Zoom1600":                           	485  	; Zoom
								,	"Zoom800":                          	486  	; Zoom
								,	"Zoom400":                          	487  	; Zoom
								,	"Zoom200":                          	488  	; Zoom
								,	"Zoom150":                          	489  	; Zoom
								,	"Zoom125":                          	490  	; Zoom
								,	"Zoom100":                          	491  	; Zoom
								,	"Zoom50":                            	492  	; Zoom
								,	"Zoom25":                            	493  	; Zoom
								,	"Zoom12.5":                          	494  	; Zoom
								,	"Zoom8.33":                          	495  	; Zoom
								,	"AddPageToFavorites":                	600  	; Favorites
								,	"RemovePageFromFavorites":             	602  	; Favorites
								,	"ShowCloseFavorites":               	604  	; Favorites
								,	"CurrentFileFavorite1":              	700  	; Favorites
								,	"CurrentFileFavorite2":               	701  	; Favorites -> I think this will be increased with every page added to favorites
								,	"ChangeLanguage":                   	588  	; Settings
								,	"Options":                             	586  	; Settings
								,	"AdvancedOptions":                  	632  	; Settings
								,	"VisitWebsite":                        	582  	; Help
								,	"Manual":                              	592  	; Help
								,	"CheckForUpdates":                  	590  	; Help
								,	"About":                             	584  	; Help
								,	"HighlightLinks":                    	616  	; Debug
								,	"ToggleEBookUI":                     	624  	; Debug
								,	"MuiDebugPaint":                     	626  	; Debug
								,	"MuiDebugPaint":                     	626  	; Debug
								,	"AnnotationFromSelection":           	628  	; Debug
								,	"DownloadSymbols":                   	630}  	; Debug

		}

	; ---------------------------------------------------------------------------------------------------------------------
	; try to determine the version of the running SumatraPDF process from the passed window handle
	; ---------------------------------------------------------------------------------------------------------------------
	; parts of following code was taken from WinSpy
		WinGetClass, class, % "ahk_id " SumatraID
		If InStr(class, "SUMATRA_PDF_FRAME") {

			WinGet SumatraPID, PID, % "ahk_id " SumatraID
			Enum := ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId=" SumatraPID)._NewEnum
			If (Enum[Process])
				FileGetVersion ProgVer, % Process.ExecutablePath

			RegExMatch(ProgVer, "\d\.\d", VSumatra)
			If !RegExMatch(VSumatra, "3\.[12]") || (StrLen(ProgVer) = 0))
				throw Exception("This  version (*" ProgVer "*) of SumatraPDF is not supported!", -1)
			
			If (SumatraCmds[VSumatra][command] = 0)
				return "" 																	;return on dummy command
			else If !SumatraCmds[VSumatra].haskey(command)
				throw Exception("Unknown value in parameter #1 !, -1, [" command "] unknown is in SumatraPDF version " VSumatra)

			If IsObject(SumatraCmds[VSumatra][command]) {
				
				For i, cmd in (wmcs := SumatraCmds[VSumatra][command]) {
					PostMessage, 0x111, % cmd,,, % "ahk_id " SumatraID
					If ( i < wmcs.Count() )
						Sleep, 300                 ; I think a little delay is necessary here
				}

			}
			else
				PostMessage, 0x111, % SumatraCmds[VSumatra][command],,, % "ahk_id " SumatraID

		}
		else {

			If RegExMatch(SumatraID, "\d\.\d", VSumatra) {

				If !RegExMatch(VSumatra, "3\.[12]")
					throw Exception("This  version (*" ProgVer "*) of SumatraPDF is not supported!", -1)

				If (SumatraCmds[VSumatra][command] = 0)
					return "" 																	;return on dummy command
				else If !SumatraCmds[VSumatra].haskey(command)
					throw Exception("Unknown value in parameter #1 !, -1, [" command "] unknown is in SumatraPDF version " VSumatra)
				else
					return SumatraCmds[VSumatra][command]
			}
			else
				throw Exception("Parameter #2 invalid!", -1, "The passed SumatraID was neither a correct window handle nor a valid string for a program version!")

		}

}


** Sorry about the bad code formatting. Now I've already removed all tabs and it still doesn't want to be beautiful.
Last edited by Frosti on 01 Dec 2020, 08:12, edited 3 times in total.
guest3456
Posts: 3163
Joined: 09 Oct 2013, 10:31

Re: [function] SumatraInvoke() - a wm_command wrapper for Sumatra PDF Version 3.1*

12 Jul 2020, 11:36

nice. i use SumatraPDF

how'd you get these values?

swagfag
Posts: 4142
Joined: 11 Jan 2017, 17:59

Re: [function] SumatraInvoke() - a wm_command wrapper for Sumatra PDF Version 3.1*

12 Jul 2020, 14:12

  • fire up spy++
  • filter by WM_COMMAND
  • click stuff

  • i dont see why ure setting the static var at runtime
  • the function could do with some rudimentary error checking(eg HasKey)
User avatar
Frosti
Posts: 379
Joined: 27 Oct 2017, 14:30
GitHub: Ixiko

Re: [function] SumatraInvoke() - a wm_command wrapper for Sumatra PDF Version 3.1*

13 Jul 2020, 00:42

okay, swagfag. I will change that. But I didnt know how to set the static var at runtime. This would speed up the process. Checking for error's is a good option to prevent crashing the process.
User avatar
Frosti
Posts: 379
Joined: 27 Oct 2017, 14:30
GitHub: Ixiko

Re: [function] SumatraInvoke() - a wm_command wrapper for Sumatra PDF Version 3.1*

13 Jul 2020, 08:01

There's a rudimentary error checking now by using .HasKey(). Function will return 0 on missing or wrong typed key. For "initializing var" on runtime I have to take some research.
Last edited by Frosti on 14 Jul 2020, 01:34, edited 1 time in total.
User avatar
SKAN
Posts: 868
Joined: 29 Sep 2013, 16:58

Re: [function] SumatraInvoke() - a wm_command wrapper for Sumatra PDF Version 3.1*

13 Jul 2020, 14:14

Hi @Frosti

Switch could perform better, IMO.
I use Sumatra. Thanks for sharing. :)
swagfag
Posts: 4142
Joined: 11 Jan 2017, 17:59

Re: [function] SumatraInvoke() - a wm_command wrapper for Sumatra PDF Version 3.1*

13 Jul 2020, 14:42

SKAN wrote:
13 Jul 2020, 14:14
Switch could perform better, IMO.
it cant
it is also borderline asinine to optimize a function such as this one for performance
User avatar
Frosti
Posts: 379
Joined: 27 Oct 2017, 14:30
GitHub: Ixiko

Re: [function] SumatraInvoke() - a wm_command wrapper for SumatraPDF V3.1 & V3.2*

01 Dec 2020, 08:13

At SumatraPDF they played the lottery and assigned new number codes for almost all messages. I have given my function the ability to find out the program version of the running SumatraPDF process so that the correct message codes are then sent.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 24 guests