AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: Daniel - PDF-XChange, PDF-XChange Support, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

Post Reply
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

Thought I'd let you guys know of this script here; which basically through a keyboard shortcut (that you can change, but in this case it is Win+W and Win+Shift+W) basically copies what you have selected to your clipboard and searches for it on Google or DDG.

You could modify it to your liking by making it go to some dictionary/encyclopedia page instead.

To be honest I've thought about a script that does the opposite, basically takes what you have selected, copies it to your clipboard and searches for it for you in the PDF xchange advanced search section – So I would appreciate it if anyone could help with that (e.g. you select something in your browser, and it searches for that in pdf xce – based on what you've set the search to, a particular window , or all tabs. Would be nice to not have to copy paste it over manually all the time and click search)

I'm also using internet search by, but when you use that what you search does not get copied to your clipboard :)
image.png

Code: Select all

;Some improvements with the help of andreas@ESA:~$ sudo -i
#w::searchOrVisitFromClip("https://www.google.com/search?q=")
#+w::searchOrVisitFromClip("https://duckduckgo.com/?q=")

searchOrVisitFromClip(searchUrl){
    clip := clipboardAll
    clip := ""
    send, ^c
    ClipWait, 0.5
    if(ErrorLevel){
    } else if (clipboard ~= "^https?:\/\/") {   ; if clipboard content is an http or https protocol url
        run, % clipboard
    } else { ;search using the provided url
        run, % searchUrl clipboard
    }
    clipboard := clip ;put the last copied thing back in the clipboard
}

about the idea of a script that searches what you've selected (or the latest thing you've copied) in PDF xce even though it is not the active window – all it would need to do would be to copy the selected text → switch to pdf xce as the active window. → use the active advanced search shortcut→ , paste it in , → search (enter). Seems possible.
Last edited by MedBooster on Sat Jun 17, 2023 9:30 am, edited 1 time in total.
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19826
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: AutoHotKey script for searching the internet for selected phrase

Post by Stefan - PDF-XChange »

Hello MedBooster,

Many thanks for sharing the above!
I hope it is useful for other AHK users out there :)

The same can be achieved with tools of our own editor, but you will need to copy your selection first (Ctrl+C), and then do your internet search (with whatever other shortcut you have assigned to it - I tested with Ctrl + Alt + Shift + F ;) )

Kind regards,
Stefan
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the internet for selected phrase

Post by MedBooster »

Here's one that copies and searches for a selected phrase selected outside PDF xce (in a browser, Anki, Notion, whatever...) and searches for it in pdf-xce, making pdf xce the active window as well

#F is windows key+ F, but you can change it to whatever you'd like

for some reason it uses the ctrl+shift+F (for advanced search) instead of simple search Ctrl+F (^F) like I put in.
Still useful nevertheless.
remove SoundBeep if you find the beeping sound annoying

Code: Select all

;reference https://www.autohotkey.com/boards/viewtopic.php?t=90864



#IfWinExist ahk_exe PDFXEdit.exe
#F::
Clipboard := ""
Send ^c
ClipWait, 0
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
 Return
} Else WinActivate
SetKeyDelay, 25
Send ^F
Send ^v{Enter}
SoundBeep, 1500
Return
#IfWinExist
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the internet for selected phrase

Post by MedBooster »

and here's a modified one that searches for the last thing you copied if you currently don't have any highlighted/selected text
also changed the shortcut to win+shift+F because Win+F annoyingly sometimes opens some Office window

Code: Select all


#IfWinExist ahk_exe PDFXEdit.exe
#+F::
clip := clipboardAll
    clip := ""
    send, ^c
    ClipWait, 0.5
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
 Return
} Else WinActivate
SetKeyDelay, 25
Send ^F
SetKeyDelay, 25
Send ^v{Enter}
SoundBeep, 1500
Return
#IfWinExist

Last edited by MedBooster on Mon Jan 16, 2023 4:39 pm, edited 1 time in total.
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19826
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: AutoHotKey script for searching the internet for selected phrase

Post by Stefan - PDF-XChange »

Hello MedBooster,

Once again - many thanks for sharing those!
I am sure many others would find these useful!

Kind regards,
Stefan
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the internet for selected phrase

Post by MedBooster »

Those of you with multiple displays that you might rotate may also be interested in this script I just fixed today.
Currently it rotates the display your cursor is in, but you can change it to the display the active Window for example if you'd like to (everything you need is in there)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;help received by [+] https://www.autohotkey.com/boards/viewtopic.php?f=76&t=87453&p=492976&sid=643f7444d745cc540978272aece7091f#p492976


#singleInstance force
#noEnv


;put together by MedBooster thank you to everyone that helped!

;the rotation code by masonjar13 ChangeDisplaySettingsExW down below edited in get active monitor down below 




lookup:={"^!down":[0,0],"^!right":[1,1],"^!up":[2,0],"^!left":[3,1]}
return

^!down::
^!right::
^!up::
^!left::

display := GetMonitorName("Point")


if (lookup[a_thisHotkey][2]){ ; rotating to portrait
	sRes:=strSplit((cRes:=screenRes_Get(display)),["x","@"])
	if (sRes[2] < sRes[1]) {
		cRes:=sRes[2] "x" sRes[1] "@" sRes[3]
	}
} else { ; rotating to landscape
	sRes:=strSplit((cRes:=screenRes_Get(display)),["x","@"])
	if (sRes[2] > sRes[1]) {
		cRes:=sRes[2] "x" sRes[1] "@" sRes[3]
	}
 }
screenRes_Set(cRes,display,lookup[a_thisHotkey][1])
return

screenRes_Set(WxHaF, Disp:=0, orient:=0) {       ; v0.90 By SKAN on D36I/D36M @ tiny.cc/screenresolution ; edited orientation in by Masonjar13
	Local DM, N:=VarSetCapacity(DM,220,0), F:=StrSplit(WxHaF,["x","@"],A_Space)
	Return DllCall("ChangeDisplaySettingsExW",(Disp=0 ? "Ptr" : "WStr"),Disp,"Ptr",NumPut(F[3],NumPut(F[2],NumPut(F[1]
	,NumPut(32,NumPut(0x5C0080,NumPut(220,NumPut(orient,DM,84,"UInt")-20,"UShort")+2,"UInt")+92,"UInt"),"UInt")
	,"UInt")+4,"UInt")-188, "Ptr",0, "Int",0, "Int",0)  
}
screenRes_Get(Disp:=0) {              ; v0.90 By SKAN on D36I/D36M @ tiny.cc/screenresolution
	Local DM, N:=VarSetCapacity(DM,220,0) 
	Return DllCall("EnumDisplaySettingsW", (Disp=0 ? "Ptr" : "WStr"),Disp, "Int",-1, "Ptr",&DM)=0 ? ""
		: Format("{:}x{:}@{:}", NumGet(DM,172,"UInt"),NumGet(DM,176,"UInt"),NumGet(DM,184,"UInt")) 
}



;Usage instructions below start of Vieira GetMonitorName

;GetMonitorName("Point") -> Uses the mouse location
;GetMonitorName("Point", [1921, 1]) -> Uses the provided coordinates
;GetMonitorName("Window") -> Uses the active window
;GetMonitorName("Rect", [1921, 1, 1951, 10]) -> Uses the provided rectangle coordinates


;Get monitor cursor by Vieira

GetMonitorName(method := "Window", coords := "", default_to := "Nearest") {
    static default := {"Null": MONITOR_DEFAULTTONULL := 0x0, "Primary": MONITOR_DEFAULTTOPRIMARY := 0x1, "Nearest": MONITOR_DEFAULTTONEAREST := 0x2}
    If (method = "Window")
        hMonitor := DllCall("MonitorFromWindow", "UInt", WinExist("A"), "UInt", default[default_to], "Ptr")
    Else if (method = "Point") {
        if coords
            x := coords[1], y := coords[2]
        Else
            MouseGetPos, x, y
        hMonitor := DllCall("MonitorFromPoint", "Int64", (x&0xFFFFFFFF) | (y<<32), "Int", default[default_to], "Ptr")
    }
    Else if (method = "Rect") {
        if !coords
            Throw % "Rect method requires coordinates"
        VarSetCapacity(RECT, 16, 0)
        NumPut(coords[4], NumPut(coords[3], NumPut(coords[2], NumPut(coords[1], &RECT, "UInt"), "UInt"), "UInt"), "UInt")
        hMonitor := DllCall("MonitorFromRect", "Ptr", &RECT, "UInt", default[default_to], "Ptr")
    }
    Else
        Throw % "Invalid method"
    NumPut(VarSetCapacity(MONITORINFOEX, 40 + (32 << !!A_IsUnicode)), MONITORINFOEX, 0, "UInt")
    DllCall("GetMonitorInfoW", "Ptr", hMonitor, "Ptr", &MONITORINFOEX)
    return StrGet(&MONITORINFOEX + 40, 32)
}
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7358
Joined: Wed Mar 25, 2009 10:37 pm
Contact:

AutoHotKey script for searching the internet for selected phrase

Post by Paul - PDF-XChange »

:)
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

I fixed it a bit so that it does not paste the text into the document itself rather than inserting it into the advanced search bar.
if you want to use simple search, just change
^+F
(ctrl+shift+F) to whatever you have set the simple search tool to Usually it is
^F
(ctrl+F)


Code: Select all

;reference https://www.autohotkey.com/boards/viewtopic.php?t=90864
;https://forum.pdf-xchange.com/viewtopic.php?p=165469#p165469
;sleep delays are added to stop the phrase from being copied into the document rather than the search bar 

#IfWinExist ahk_exe PDFXEdit.exe
#+F::
clip := clipboardAll
    clip := ""
    send, ^c
    ClipWait, 0.5
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
 Return
} Else WinActivate
Sleep 100
Send ^+F
Sleep 100
Send ^v
Sleep 50
Send {Enter}
SoundBeep, 200
Return
#IfWinExist



Note: for it to work properly you have to not have any text selected in the active PDF in PDF-xce. If you do have any text selected in the active PDF, it will be copied over to the advanced search bar along with the text you are copying over from another program (for me it is usually my browser (Firefox), Anki or Notion.
So that's the programs I am using the AutoHotKey script with.
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19826
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by Stefan - PDF-XChange »

Hello MedBooster,

Many thanks for sharing the updated version as well!

Kind regards,
Stefan
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

Actually... although this one is more stable... it turns out the issue of it pasting in the result was not delays, but actually that if anything else in the advanced search panel than the search bar itself is selected – you can not use the Ctrl+Shift+F shortcut to highlight it :)

viewtopic.php?t=40398

So I hope that is a quick fix

Only then the AHK script should work well, so I hope that is a quick fix :))
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7358
Joined: Wed Mar 25, 2009 10:37 pm
Contact:

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by Paul - PDF-XChange »

Hi, MedBooster

I reported both these to the devs. I hope to hear something soon, I think they are otherwise engaged right now. As I see it much depends on their response to the CTRL+Shift+f report.

Kind regards,
Paul - Tracker Supp
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

The issue is that the keyboard shortcut to activate the search box only works when the active window is the main PDF-XCE window, and not when the active "window" is the advanced "Search" pane whether it's docked or undocked.
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10963
Joined: Wed Jan 03, 2018 6:52 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by Daniel - PDF-XChange »

Hello, MedBooster

While we are waiting for that, in regards to your script. You could add an "alt+F" command to your script, to force "close" the search pane if its open, or to open it if closed. This would cause the Ctrl+Shift+F command following it to either open the pane and select the field, or have no effect since alt+F has already done so.

Kind regards,
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

On a similar note
image.png
Ctrl+F to choose the input field does not work when < or > is highlighted
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10963
Joined: Wed Jan 03, 2018 6:52 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by Daniel - PDF-XChange »

Hello, MedBooster

Thank you for bringing that up, at first I was going to pose this as a feature request, but I found that some other hotkeys also do not work while these buttons are in focus, so I have reported this as a bug to the dev team, hopefully they can enable use of hotkey commands like this directly in this pane in the future.

Kind regards,
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

simple search has been fixed

Ctrl+shift+F for highlighting the advanced search field does not work when the last thing you clicked on in PDF-XCE is anything in the advanced search pane other than the search field. Not just the results but also the other menus/buttons
image.png
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10963
Joined: Wed Jan 03, 2018 6:52 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by Daniel - PDF-XChange »

Hello, MedBooster

That one may be more difficult to address, given the multitude of fields, but I can pass it by the dev team to see what can be done.

Kind regards,
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by MedBooster »

Code: Select all

;reference https://www.autohotkey.com/boards/viewtopic.php?t=90864
;https://forum.tracker-software.com/viewtopic.php?p=165469#p165469
;sleep delays are added to stop the phrase from being copied into the document rather than the search bar 

#IfWinExist ahk_exe PDFXEdit.exe
#+F::
clip := clipboardAll
    clip := ""
    send, ^c
    ClipWait, 0.5
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
 Return
} Else WinActivate
Sleep 160
SendEvent {Tab}
Sleep 100
Send !F ;Alt+F keyboard shortcut to open/close the advanced search pane necessary to unselect search result until bug is fixed
Sleep 50
Send ^+F
Sleep 250
Send ^v
Sleep 50
Send {Enter}

SendEvent +{Tab}  ; Corrected way to send Shift+Tab
SendEvent {Down}
SendEvent {Down}
Return
#IfWinExist


#IfWinExist ahk_exe PDFXEdit.exe
#F::
clip := clipboardAll
    clip := ""
    send, ^c
    ClipWait, 0.5
If ErrorLevel {
 MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
 Return
} Else WinActivate
Sleep 160
SendEvent {Tab}
Sleep 100
Send !F ;Alt+F keyboard shortcut to open/close the advanced search pane necessary to unselect search result until bug is fixed
Sleep 50
Send ^+F
Sleep 250
Send ^v
Sleep 50
Send {Enter}

Return
#IfWinExist


Until this is fixed, the added !F ;keyboard shortcut is to open/close the advanced search pane necessary to unselect search result until bug is fixed
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19826
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: AutoHotKey script for searching the selected text in PDF-xchange , and another for program→browser

Post by Stefan - PDF-XChange »

Hello MedBooster,

Glad to hear that you have found a workaround for the time being!

Kind regards,
Stefan
Post Reply