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

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.

Related

Open selected text in new tab

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.

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

Menu and submenu selection on specific app via Autohotkey

I have a dictionary application in which Ctrl+C does not work, so the only way to copy is to go from the menu, Edit > Copy. I have tried using the following code and variations of it, but it did not work.
!l::
WinActivate, MAGENTA - Dictionaries Explorer II
WinMenuSelectItem, ahk_class TMainForm,, Edit, Copy
return
WinSpy data:
MAGENTA - Dictionaries Explorer II
ahk_class TMainForm
ahk_exe MgDE2.EXE
ahk_pid 2580
Forgetting AHK for a minute, if you just press and release the Alt key (all by itself) what happens?
If the File menu activates (blue box), then you can just send the ALT keystroke followed by directional movement as needed and finally sending ENTER to activate the item
Like this
SendInput {Alt}{Right}{Down}{Enter}
If you needed to go right 4 elements and then down 2 entries you would use this way:
SendInput {Alt}{Right 4}{Down 2}{Enter}

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/

How does this AHK script work?

The following AHK script automatically brings up the menu that you would normally get when you right-click on the uTorrent icon in the system task-bar tray:
DetectHiddenWindows, On
Process, Exist, utorrent.exe
WinGet, W, List, ahk_pid %ErrorLevel%
Loop %W%
{
WinGetClass, Class, % "ahk_id" W%A_Index%
If InStr( Class, "µTorrent" ) {
hWnd := W%A_Index%
Break
}}
PostMessage, 0x8001, 0,0x204,, ahk_id %hWnd% ; Right Click down
PostMessage, 0x8001, 0,0x205,, ahk_id %hWnd% ; Right Click Up
I understand most of it except the last two lines. 0x8001 is WM_APP+1. Why is a message in the WM_APP range used, and what exactly happens when these two messages are issued? Many thanks for the explanation.
According to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx#system_defined)
If your application is marked version 4.0, you can use message-identifier values in the range 0x8000 (WM_APP) through 0xBFFF for private messages.
So this would indicate that 0x8001 is an identifier that utorrent has chosen for a message. The 0x204 and 0x205 values are for the Right-Click Down and Up events respectively. My guess is that this code is intended to simulate a Right-Click on utorrent's icon in the Windows tray.
Also, if you're using Autohotkey_L the code can probably be simplified to three lines and perform the same:
DetectHiddenWindows, On
PostMessage, 0x8001, 0,0x204,, ahk_exe utorrent.exe ahk_class µTorrent ; Right Click down
PostMessage, 0x8001, 0,0x205,, ahk_exe utorrent.exe ahk_class µTorrent ; Right Click Up
When you right click on something, the context menu is shown. The context menu can also be shown by pressing the appskey button on the keyboard. When you press it, Windows sends the application a message, i.e., the WM_APP message. Most applications respond by showing a context menu. In the case of the tray icon, the response is the same as the response to a right-click.
Seems that the WM_APP+1 message number is a choice made in the development of the application, in this case utorrent. Could have been another message #, nothing magic. I believe the code I presented (which is from the AHK forums) figured out the message to send through reverse engineering.