Open selected text in new tab - autohotkey

What I have is this:
; Search Cntl+Shift+c
^+c::
{
Send, ^c
Sleep 50
Run "C:\Program Files\Mozilla Firefox\firefox.exe" "%clipboard%"
return
}
What I get is kind of correct. Basically when I highlight a link I can quickly open it, but it does not work with regular text.
If I mark text and want to do a quick search on the other tab, it actually redirects me to http://text.
What I want is to just past the clipboard on the taskbar at the top and then let Firefox do the work
(decide if it is a link or if it is a normal text, in which case it just redirects it to the search engine).

I don't have/use Firefox, but I'd assume it has shortcuts to open a new tab just like any other browser would. So maybe good to make use to those.
I'd assume CTRL+T creates and activates a new tab and the search field in it, so I'll write an example script for that:
^+c::
Clipboard := "" ;empty clipboard
SendInput, ^c
ClipWait, 2 ;wait (max 2 secs) for clipboard to contain something
if (Clipboard != "") ;if we got something to the Clipboard
SendInput, ^t^v{Enter} ;CTRL+T CTRL+V Enter
return
Used ClipWait instead of Sleeping and switched over to SendInput due to it being faster and more reliable.
Works just fine for me (on Google Chrome). One possible issue I could see, is it being too fast and your computer being slow. If that were to be the case, switch over to using normal Send and use SetKeyDelay.

Related

Automatic translation

im really new to all this and i was trying to make an Autohotkey for translation. i was digging for some time looking for examples that only confused me more, even if the code looked simple, i didn't understand half of it.
So, what I'm trying to do is: select a paragraph and replace it automatically with its translation.
i was hooping it to be somenthing as simple as CTRJ + C, Translate, CTRL + V, but i can't find the command to go to google translate or somenthing similar, it's not on the autohotkey help file so i'm guessing i don't have libraries?
I'm at my wits end, please help.
You came to the right place. Check out AutoHotKey for sure.
First, how to do it by hand? Those are the steps for ahk. So, lets say you have a paragraph of text selected. You will hit the ahk shortcut and that shortcut will:
first ahk figures out what window its in (using WinGetActiveTitle) and then sends the keystrokes Ctrl+c to copy the selection ("send, ^c" and "Clipwait"), then
ahk can access the clipboard containing the text, do a string manipulation or regex to replace all spaces with the html escape sequence %20 (eg, transtext := StrReplace(Clipboard, " ", "%20")) and
construct a URL to do the Google Translate, something like (where sl is source language and tl is translation language, and text is what you want translated): transurl := "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" . transtext
AHK runs that url and opens a browser window showing result (run % transurl).
This part sucks. Now, you need to use a mouse click at a location (or maybe you can find a controlsend or a combination of keystrokes moving the cursor with tabs and such) to land on the "Copy translation" button. Or how bout you do it manually (try sleep, 8000 to wait while you hit the button)
then have ahk close the window (optionally, or you just do it by hand during the sleep time) and
ahk switches back to the application with the original selected paragraph (WinActivate or do it yourself) and
send ctrl+v to paste the translated text over the original (send ^v).
A starter pack of AHK code (edited per user comments):
WinGetActiveTitle, activewin
Clipboard =
SendInput, ^c
ClipWait
transtext := StrReplace(Clipboard, " ", "%20")
transurl := "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" . transtext
Run, % transurl
Sleep, 6000 ; adjust to taste.
SendEvent, {tab 10} ; adjust to taste.
Sleep 1000
SendInput, {enter}
Sleep, 1000
SendInput, ^{F4}
WinActivate, activewin
sleep, 1000
SendInput, ^v
Try it and let us know how else to help.
OKOK, first of all, thank you all, the script works just fine now. I'm able to copy, translate and paste any text now. Only a few questions lingering.
1) i'm not sure i get what the step number 5 is suppose to do. whatever it is, it works so i don't touch it.
2) is there a way to reset google.translate so it dosent open a new window every time? that could save a lot of time.
3) this one doesn't have a chance, but i ask anyway. Is there a way to not open google chrome at all? because i know that u can translate from excel automatically. (i know that if it is possible will be super hard)
This is the code i ended with:
^a::
clipboard := ""
sendinput, ^c
ClipWait [,,Waitforanydata]
transtext := StrReplace(Clipboard, " ", "%20")
transurl := "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" .
transtext
run % transurl
Sleep, 4000
SendEvent, {tab 9}
SendEvent, {enter}
Winactivate, NAME.pdf - PROGRAM
sendinput, ^v

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

Insert Tab character into Word

I'm trying to create a script, which will insert Tab character into Word.
Quick note: I've also tested it in OpenOffice. So if you haven't Word, you may test it in OpenOffice or, probably, LibreOffice Writer.
If you are familiar with Word, you know that if you press Tab key on a blank line, you get the Word-like indentation, instead of inserting real Tab char.
Here is attempt to fix it:
$Tab::
old := ClipboardAll
Sleep, 1000 ; Just for testing. If I remove this line, the error still
; occurs, but much more randomly.
Clipboard := " " ; Tab character
ClipWait
SendInput, ^v
KeyWait, Tab
Clipboard := old
return
The problem is, that sometimes (when I press Tab quickly), it is inserted an old content of the clipboard, instead of Tab.
I've tried to use ClipWait, KeyWait, Sleep, InstallKeybdHook in different combinations.
Maybe someone knows what's the problem here and how it can be solved?
Sends tab without triggering indent in word
SetTitleMatchMode 2
#IfWinActive Microsoft Word
$tab:: sendinput .{tab}{left}{backspace}{right}
If I'm understanding you correctly, a better solution might be to turn off this feature with Options > Proofing > Autoformat as you type > Set left and first indent with tabs and backspaces (see here)

Autohotkey: task to copy from browser and paste on text editor

I'm trying to build a script that uses both a browser and a texteditor. The workflow I can summarize as follows:
Right click on a video for streaming
Click on the option to copy the redirection link
Switch to a text editor (Slickedit in my case)
Paste the copied link
Go back to the browser and await next command.
I want to automate this with a single key press while I am standing on the link with the mouse. This has been
my attempt:
^+!a::
Click Right, 392, 64 ;execute in browser
Click Left, 410, 79 ;
Send, !{Tab} ;switch to text editor
Send, ^V ;paste in text editor
Send, !{Tab} ;switch back to browser
return
The script isn't working correctly because it appears not to be executing the ^V command.
I suspect it's because it is executing it before Slickedit is even active. How can I syncronize
these KeyPreses so that they are executed at the right times? Also is there a nicer way for me to
switch to Slickedit without relying on the alt-tab?
There are a few things you could use to make your script better. WinActivate, clipboard, and improved mouse movement seem to be good ones to add.
^+!a::
clipboard = ; clears clipboard
Click Right ; execute in browser
MouseMove, 18, 15, 50, R ; Moves mouse relative to start location
Click Left
ClipWait, 2 ; Waits 2 seconds for clipboard to contain something
WinActivate, Slickedit ; Switch to text editor
WinWaitActive, Slickedit
Send % clipboard ; paste in text editor
WinActivate, ahk_class Chrome_WidgetWin_1 ; or your browser of choice
Return
Use the included Window Spy to find the correct Window titles or classes to be used in the WinActivate commands.

How to obtain textual contents from a window

I have a window that displays a book. In two smaller boxes below, there is page number and volume information of the book that is open. I can get that information easily as follows:
ControlGetText, volume, ThunderRT6TextBox3
ControlGetText, page, ThunderRT6TextBox2
Then my script makes my mouse pointer move to a button. It clicks it, and a new window pops open. In that window, there is more textual information related to the book, such as publisher, name author, edition etc. I want to retrieve that information. But when I try the same strategy it is not working, eg:
ControlGetText, data, RichTextWndClass3
The only difference between the two cases, is that those two small boxes are editable, you can enter text whereas this window is static.
I tried many other options such as:
SendEvent ^a
Which is equivalent to control + a, which should select everything. I tried putting pauses but it would never select. I tried the script to manually double click on that window. It works, and one word gets select like that. Even then SendEvent ^a doesn't do anything.
However, if I do SendEvent ^{insert}, then the selected word gets copied to my clipboard.
I experimented with more combinations:
ControlSend ahk_class ThunderRT6FormDC, ^a
ControlSend ClassNN RichTextWndClass3, ^a
and
ControlSend ThunderRT6FormDC, ^a
ControlSend RichTextWndClass3, ^a
None of them work. All text selection does not manifest itself in that window.
The only alternative remaining for me is to make the script do a manual selection of the entire text. However, this is slow and very ridiculous. Moreover, in Window Spy under the section: Visible Window Text, the text is all there. I tried many other possibilities and I am at the end of my wits. How am I to harvest that text directly?
EDIT--
The text of the window shows in Window Spy under the heading: TitleMatchMode=slow Visible Text, NOT the heading: Visible Window Text
EDIT--
I spoke to you about two windows. The first one in which i get volume and page number. The second one which needs to pop up by pressing a button.
Both these windows have the same class-name:
ahk_class ThunderRT6MDIForm
Is that problematic in any way?
EDIT--
The conclusion is that it is impossible for me to get that text from the second window directly. As such, I opted for the lame, boring manual method. I send out a {shift down} to the active window and then do a click at the bottom of the window. Then I save the selection to my clipboard. It works, but it is just stupid. Please help me find a more elegant solution than this one.
This is the code I used:
ControlGetText, volume, ThunderRT6TextBox3
ControlGetText, page, ThunderRT6TextBox2
Click, 110, 70
sleep 1000
SendInput {shift down}
click 29, 490
SendInput {shift up}
sleep 1000
SendInput, ^{ins}
sleep 100
It is funny to note that real keyboard keys, such as a b c are not possible. But I am able to send a ctrl, shift and an ins. As I noted above, ^c was also giving issues just like ^a was giving issues.
This routine will do the job of getting and returning from the active window the following text sections:
- EdtWindowTextFastVisible
- EdtWindowTextSlowVisible
- EdtWindowTextFastHidden
- EdtWindowTextSlowHidden
MyGetWindowText(ByRef EdtWindowTextFastVisible, ByRef EdtWindowTextSlowVisible, ByRef EdtWindowTextFastHidden,ByRef EdtWindowTextSlowHidden)
{
; Source: https://code.google.com/p/autohotkey-cn/source/browse/trunk/Source/AHK_Window_Info/AHK_Window_Info_v1.7.ahk?r=6
EdtWindowTextFastVisible =
EdtWindowTextSlowVisible =
EdtWindowTextFastHidden =
EdtWindowTextSlowHidden =
WindowControlTextSize = 32767
VarSetCapacity(WindowControlText, WindowControlTextSize)
WinGet, WindowUniqueID, ID, A
;Suggested by Chris
WinGet, ListOfControlHandles, ControlListHwnd, ahk_id %WindowUniqueID% ; Requires v1.0.43.06+.
Loop, Parse, ListOfControlHandles, `n
{
text_is_fast := true
If not DllCall("GetWindowText", "uint", A_LoopField, "str", WindowControlText, "int", WindowControlTextSize)
{
text_is_fast := false
SendMessage, 0xD, WindowControlTextSize, &WindowControlText,, ahk_id %A_LoopField% ; 0xD is WM_GETTEXT
}
If (WindowControlText <> ""){
ControlGet, WindowControlStyle, Style,,, ahk_id %A_LoopField%
If (WindowControlStyle & 0x10000000)
{ ; Control is visible vs. hidden (WS_VISIBLE).
If text_is_fast
EdtWindowTextFastVisible = %EdtWindowTextFastVisible%%WindowControlText%`r`n
Else
EdtWindowTextSlowVisible = %EdtWindowTextSlowVisible%%WindowControlText%`r`n
} Else
{ ; Hidden text.
If text_is_fast
EdtWindowTextFastHidden = %EdtWindowTextFastHidden%%WindowControlText%`r`n
Else
EdtWindowTextSlowHidden = %EdtWindowTextSlowHidden%%WindowControlText%`r`n
}
}
}
;EdtWindowTextFastVisibleFull := ShowOnlyAPartInGui("EdtWindowTextFastVisible", EdtWindowTextFastVisible, 400)
;EdtWindowTextSlowVisibleFull := ShowOnlyAPartInGui("EdtWindowTextSlowVisible", EdtWindowTextSlowVisible, 400)
;EdtWindowTextFastHiddenFull := ShowOnlyAPartInGui("EdtWindowTextFastHidden", EdtWindowTextFastHidden, 400)
;EdtWindowTextSlowHiddenFull := ShowOnlyAPartInGui("EdtWindowTextSlowHidden", EdtWindowTextSlowHidden, 400)
Return
}
There is an autohotkey script that emulates most of the window spy logic. It is called AHK_Window_Info_v1.7.ahk. The nice thing is... you can run it to see if your second window text if visible to this script and if so... the logic needed to pull the information is available inside the script. Here is a link to the webpage and the script is available through SKANs dropbox link on that page. http://www.autohotkey.com/board/topic/8204-ahk-window-info-17/