Let's say I have a list of nouns
List =
(
cat
lion
cheetah
tiger
)
that I want to translate into another language using Google translate page:
The following steps need to be done:
1) pasting a noun into the left field of the page;
2) clicking on "Translate" button;
3) copying the translated noun from the right field;
Of course, I could do all of that using Send and Click commands, but I wonder if it's possible to be done using ComObject.
I am not asking to write a complete script for me, but could you, please, direct me as to what I should start with here. I know almost nothing about ComObject.
In case it matters, I am using FireFox.
How to paste, click and copy parts of webpage with ComObject
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: How to paste, click and copy parts of webpage with ComOb
It matters a lot. COM is a Microsoft thing and is not supported by most non-Microsoft software which means you would have to be willing to use Internet Explorer for this. "Use" is a loose term though, Internet Explorer can be used through COM with out ever being visible to the user.Benny-D wrote: In case it matters, I am using FireFox.
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: How to paste, click and copy parts of webpage with ComOb
Interesting idea.. Apparently, the google translate API is not free.
Here's a start.. working on it right now
Here's a start.. working on it right now
Code: Select all
;google translate
#SingleInstance, off
PID_Self := DllCall("GetCurrentProcessId")
GroupAdd, Self_ID, ahk_pid %PID_Self%
SourceLang := "auto" ;country code of the source language
DestLang := "de" ;country code of the destination language
Word := "pomme" ;word to be translated
GoogleTranslateURL := "http://translate.google.com/#" SourceLang "/" DestLang "/" Word
Gui Add, ActiveX, w980 h640 vWB, Shell.Explorer ; The final parameter is the name of the ActiveX component.
ComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.
WB.Navigate(GoogleTranslateURL) ; This is specific to the web browser control.
Gui Show
return
GuiClose:
ExitApp
class WB_events
{
NavigateComplete2(wb, NewURL)
{
ToolTip %NewURL%
}
}
#IfWinActive ahk_group Self_ID
^h::
MsgBox % WB.document.getElementById("result_box").innerHTML
return
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: How to paste, click and copy parts of webpage with ComOb
small Update:
Code: Select all
;google translate
#SingleInstance, off
SourceLang := "auto" ;country code of the source language
DestLang := "de" ;country code of the destination language
Words := "Je mange une pomme rouge. J'aime bien le chocolat au lait." ;words to be translated
GoogleTranslateURL := "http://translate.google.com/#" SourceLang "/" DestLang "/" Words
;StringReplace,GoogleTranslateURL,GoogleTranslateURL,%A_Space%,`%20,All
Gui Add, Edit, w200 h150 vResultBox, Loading...
Gui Add, ActiveX, x0 y0 w0 h0 vWB, Shell.Explorer ; The final parameter is the name of the ActiveX component.
Gui, Add, StatusBar,, Loading...
ComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.
WB.Navigate(GoogleTranslateURL) ; This is specific to the web browser control.
Gui Show
return
GuiClose:
ExitApp
class WB_events
{
;NavigateComplete2(wb, NewURL)
;DownloadComplete(wb, NewURL)
DocumentComplete(wb, NewURL)
{
if (WB.ReadyState == 4) { ; see http://stackoverflow.com/a/9835755/883015
t_s:=A_TickCount
while (StrLen(WB.document.getElementById("result_box").innerHTML)==0) {
;wait
}
SB_SetText("Done. JS Loaded in " (A_TickCount-t_s) " ms.")
GuiControl,,ResultBox, % WB.document.getElementById("result_box").innerHTML
}
return
}
}
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: How to paste, click and copy parts of webpage with ComOb
Here is my version that works for me:
joedf's way of sending the information via the address is probably better than sending it into the input box as that allows control of input and output languages.
We are both basically getting the information back out the same way with a little different syntax. It is good to see two different ways you can get innertext.
FG
Code: Select all
IE := ComObjCreate("InternetExplorer.Application")
;~ IE.Visible := true
IE.Navigate("translate.google.com")
While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy
Sleep 50
Word := "Hello"
DetectHiddenWindows, On
ControlSend, Internet Explorer_Server1, %Word%, ahk_class IEFrame
While !(StrLen(IE.document.all.result_box.innertext))
Sleep 50
Result := IE.document.all.result_box.innertext
IE.Quit
MsgBox % Result
We are both basically getting the information back out the same way with a little different syntax. It is good to see two different ways you can get innertext.
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: How to paste, click and copy parts of webpage with ComOb
Ok updated
Full blown GUI app
EDIT: FanaticGuru! cool, hmm lol... i have a size 0px control... I forgot about IE := ComObjCreate("InternetExplorer.Application")
Full blown GUI app
Code: Select all
;google translate - test joedf - 2014/07/02
#SingleInstance, off
Gui Add, Text, x6 w40, Input:
Gui Add, ComboBox, yp+1 x+4 w80 vCBSource, auto||en|de|fr|es
Gui Add, Edit, w300 h80 vInputBox,
Gui Add, Text, x6 w40, Result:
Gui Add, ComboBox, yp x+4 w80 vCBDest, en||de|fr|es
Gui Add, Edit, w300 h100 vResultBox,
Gui Add, Button, gBtnTranslate vBtnTranslate +Default, Translate
Gui Add, ActiveX, x0 y0 w0 h0 vWB, Shell.Explorer ; The final parameter is the name of the ActiveX component.
WB.silent := true ;Surpress JS Error boxes
ComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.
Gui, Add, StatusBar,, Idle.
Gui Show
return
GuiClose:
ExitApp
BtnTranslate:
GuiControlGet,Words,,InputBox
if Words is Space
return
GuiControl,Disable,BtnTranslate
GuiControlGet,SourceLang,,CBSource
GuiControlGet,DestLang,,CBDest
if SourceLang is Space
GuiControl,ChooseString,CBSource, % SourceLang:="auto"
if DestLang is Space
GuiControl,ChooseString,CBDest, % DestLang:="en"
/* SourceLang := "auto" ;country code of the source language
* DestLang := "de" ;country code of the destination language
* Words := "Je mange une pomme rouge. J'aime bien le chocolat au lait." ;words to be translated
*/
GoogleTranslateURL := "http://translate.google.com/#" SourceLang "/" DestLang "/" Words
;StringReplace,GoogleTranslateURL,GoogleTranslateURL,%A_Space%,`%20,All
SB_SetText("Loading...")
WB.Navigate(GoogleTranslateURL) ; This is specific to the web browser control.
return
class WB_events
{
;NavigateComplete2(wb, NewURL)
;DownloadComplete(wb, NewURL)
DocumentComplete(wb, NewURL)
{
if (WB.ReadyState == 4) { ; see http://stackoverflow.com/a/9835755/883015
SB_SetText("Downloaded. Loading JS...")
t_s:=A_TickCount
while (StrLen(WB.document.getElementById("result_box").innerHTML)==0) {
;wait
}
t_e:=A_TickCount
;Quick-Remove Html Tags
data:=RemoveHtmlTags(WB.document.getElementById("result_box").innerHTML)
GuiControl,,ResultBox, % data
ResultLang:=GetGoogleTranslateLanguage(WB.document.getElementById("gt-sl-sugg").innerHTML)
SB_SetText("Done. JS Loaded in " (t_e-t_s) " ms. Input Language: " ResultLang)
GuiControl,Enable,BtnTranslate
}
return
}
}
RemoveHtmlTags(data) {
return RegExReplace(data,"<(\/)?[^<>]*(\/)?>")
}
GetGoogleTranslateLanguage(langbar_data) {
langs:=StrSplit(langbar_data,"</div>")
Main_lang:="Unknown"
for each, lang in langs
if InStr(lang,"aria-pressed=""true""")
Main_lang:=Trim(RemoveHtmlTags(lang))
if k:=InStr(Main_lang," - ") {
StringLeft,Main_lang,Main_lang,%k%
Main_lang := Trim(Main_lang) " [Detected]"
}
return Main_lang
}
EDIT: FanaticGuru! cool, hmm lol... i have a size 0px control... I forgot about IE := ComObjCreate("InternetExplorer.Application")
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: How to paste, click and copy parts of webpage with ComOb
Oh my God!!! FanaticGuru and joedf, you guys are just too generous! Thank you so much!
I am studying your scripts now.
I am studying your scripts now.
Re: How to paste, click and copy parts of webpage with ComObject
I know that I am coming at this a few years later, but I am trying to use the code as a template for what I wish to do. When I copied all of the text that Jodef posted, and saved it as GoogleTranslate.ahk, when I tried to run it I got the following error message:
Error: Call to nonexistent function.
Specifically: StrSplit(langbar_data, "</div>")
Then it references line 76, and states that the program will exit.
I assume that something changed since the first version was released. Could anyone tell me what it is?
Error: Call to nonexistent function.
Specifically: StrSplit(langbar_data, "</div>")
Then it references line 76, and states that the program will exit.
I assume that something changed since the first version was released. Could anyone tell me what it is?
Re: How to paste, click and copy parts of webpage with ComObject
Never Mind. I replaced the content of line 76 with. Somehow, that did the trick
StringSplit, langs, langbar_data, "</div>"
StringSplit, langs, langbar_data, "</div>"
Re: How to paste, click and copy parts of webpage with ComObject
clina1j wrote:Never Mind. I replaced the content of line 76 with. Somehow, that did the trick
StringSplit, langs, langbar_data, "</div>"
The function Strsplit was introduced in AHK version 1.1.13 ...
Re: How to paste, click and copy parts of webpage with ComObject
That would do it. I have only AutoHotKey 1.1.09.04. (Stupid Company App Store with out of date software ...)
Who is online
Users browsing this forum: No registered users and 378 guests