Read and open URLs from a Google Doc

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Read and open URLs from a Google Doc

24 Apr 2022, 15:51

Hi all,

I have searched online for a bit, but I have a feeling this may not be simple.

I am trying to:
  • read the contents of a file (could be a *.docx or *.odt)
  • Search for all hyperlinks within that text
  • Open these hyperlinks in a browser

Is this possible with AHK?
Last edited by submeg on 24 Apr 2022, 18:10, edited 1 time in total.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Read and open URLs from a file

24 Apr 2022, 16:03

If in a word document:

Code: Select all

oWord := ComObjActive("word.application")
loop % oWord.ActiveDocument.hyperlinks.count
     run % oWord.ActiveDocument.hyperlinks(a_index).address
14.3 & 1.3.7
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a file

24 Apr 2022, 16:25

flyingDman wrote:
24 Apr 2022, 16:03
If in a word document:

Code: Select all

oWord := ComObjActive("word.application")
loop % oWord.ActiveDocument.hyperlinks.count
     run % oWord.ActiveDocument.hyperlinks(a_index).address
Wow @flyingDman, that's so simple with COM. Will go test on a machine that has Word, one moment.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a file

24 Apr 2022, 17:04

Can report that it works perfectly! Went to tell the wife...tells me the documents are all google docs!!

:roll:

Can the same be done in google docs? Will begin searching now...
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Read and open URLs from a file

24 Apr 2022, 17:24

submeg wrote:
24 Apr 2022, 17:04
Can report that it works perfectly! Went to tell the wife...tells me the documents are all google docs!!

:roll:

Can the same be done in google docs? Will begin searching now...
save the google doc as word
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a file

24 Apr 2022, 17:34

AHKStudent wrote:
24 Apr 2022, 17:24
save the google doc as word
I would, but on both the PC I'm writing the script on and the PC that will be using this script, they do not have Office installed, so being able to read in directly from the currently active document would be best.

I'm currently watching @geek's webinar with @Joe Glines on getting data from Chrome. Will take me a little bit, but I think it's in there.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a Google Doc

24 Apr 2022, 22:53

Okay, I have watched that entire video and did some testing to see if I could at least find the hyperlinks on a web page, as opposed to a google document. From @geek's explanation, I was able to work out the code to pull the URL from the "Read this before posting!" post at the top of the page on https://www.reddit.com/r/AutoHotkey/. The code used to pull the first URL in the main section of the Reddit page is:

Code: Select all


[].filter.call( document.querySelectorAll("div.content a:link.title.may-blank.loggedin"), (e) => e.	href.startsWith("http"))[0].href

I then try to use this in my AHK, but I receive an error when including the Chrome.ahk file:

Code: Select all

D:\Users\...\Downloads\Chrome-master\lib\AutoHotkey-JSON\Jxon.ahk (174) : ==> Call to nonexistent function.

It specifically points to line 174 in Jxon.ahk:

Jxon_Dump(k) : q . k . q ) ;// key
. ( indent ? ": " : ":" ) ; token + padding

Any ideas?

This is referenced in line 355 of Chrome.ahk:

#Include %A_LineFile%\..\lib\WebSocket.ahk\WebSocket.ahk



I pulled the Chrome.ahk from HERE. Is this the correct location?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Read and open URLs from a file

25 Apr 2022, 01:21

submeg wrote:
24 Apr 2022, 17:34
AHKStudent wrote:
24 Apr 2022, 17:24
save the google doc as word
I would, but on both the PC I'm writing the script on and the PC that will be using this script, they do not have Office installed, so being able to read in directly from the currently active document would be best.

I'm currently watching @geek's webinar with @Joe Glines on getting data from Chrome. Will take me a little bit, but I think it's in there.
how about copy the whole thing to clipboard and parse the links using regex?
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a file

25 Apr 2022, 02:21

AHKStudent wrote:
25 Apr 2022, 01:21
submeg wrote:
24 Apr 2022, 17:34
AHKStudent wrote:
24 Apr 2022, 17:24
save the google doc as word
I would, but on both the PC I'm writing the script on and the PC that will be using this script, they do not have Office installed, so being able to read in directly from the currently active document would be best.

I'm currently watching @geek's webinar with @Joe Glines on getting data from Chrome. Will take me a little bit, but I think it's in there.
how about copy the whole thing to clipboard and parse the links using regex?
Could do that, I've not used RegEx before, but can it find the URL of hyperlinked text? What I mean is that the text may show "I found this HERE", but the word HERE has a hyperlink attached to it. Will it see that hyperlink?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Read and open URLs from a file

25 Apr 2022, 02:56

submeg wrote:
25 Apr 2022, 02:21
AHKStudent wrote:
25 Apr 2022, 01:21
submeg wrote:
24 Apr 2022, 17:34
AHKStudent wrote:
24 Apr 2022, 17:24
save the google doc as word
I would, but on both the PC I'm writing the script on and the PC that will be using this script, they do not have Office installed, so being able to read in directly from the currently active document would be best.

I'm currently watching @geek's webinar with @Joe Glines on getting data from Chrome. Will take me a little bit, but I think it's in there.
how about copy the whole thing to clipboard and parse the links using regex?
Could do that, I've not used RegEx before, but can it find the URL of hyperlinked text? What I mean is that the text may show "I found this HERE", but the word HERE has a hyperlink attached to it. Will it see that hyperlink?
no
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a Google Doc

25 Apr 2022, 03:33

Okay so I checked, my Chrome was not running in debug mode. I have since enabled this.

EDIT: Okay, I think I still wasn't in debug mode....my target of my Chrome shortcut is now:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" –remote-debugging-port=9222

With this now set, I tried to access the website that G33kDude is talking about here, it doesn't want to open? when I type the following into the Chrome address bar:



I receive the following error:

This site can’t be reached

127.0.0.1 refused to connect.

I'm assuming that's why I receive the error below?




The code will now compile and it lets me press the hotkey, but I receive a different error now. See my code below:

[code]

#NoEnv
SetBatchLines, -1
#SingleInstance, force

;viewtopic.php?f=6&t=42890

#Include D:\Users\LOCALUser\Downloads\Chrome-master\Chrome.ahk


#F1::

page := Chrome.GetPageByURL("https://www.reddit.com/")

if !IsObject(page) {
Msgbox % "That wasn't an object"
ExitApp
}

PageURL = [].filter.call( document.querySelectorAll("div.content a:link.title.may-blank.loggedin"), (e) => e.href.startsWith("http"))[0].href


;Msgbox % "first URL is: " Chrome.Jxon_Dump(page.Evaluate(PageURL))

Msgbox % "first URL is: " page.Evaluate(PageURL).value

Return

[/code]

It returns the errors as per below when I press the hotkey.

Image


EDIT 26/4/2022: My Chrome was still not in debugging mode!

I have no knowledge of the Chrome.ahk script, so don't know where to start....I'll check out Joe's videos and pages in the meantime, but if anyone can help with this, it would be greatly appreciated.
Last edited by submeg on 26 Apr 2022, 07:29, edited 2 times in total.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: Read and open URLs from a Google Doc

26 Apr 2022, 07:28

After watching more of the series between Joe and G33kdude, I was able to get the following to work - It can go to a currently open subreddit page and return the URL that sits behind the heading of the first post on the subreddit:

Code: Select all


#SingleInstance,Force
#Include D:\Users\LOCALUser\Downloads\Chrome.ahk_v1.2\Chrome.ahk
;**************************************


#F2::

page:=Chrome.GetPage()

If !IsObject(page){
	MsgBox % "That wasn' t object / the page wasn't found"
	ExitApp
}
Else {
	
	;MsgBox % "This is an object"

}

page.Evaluate("alert('this is AHK!');")

;The below line pulls the first hyperlink in the main section of the reddit main pane
PageURL = [].filter.call( document.querySelectorAll("div.content a:link.title.may-blank.loggedin"), (e) => e.href.startsWith("http"))[0].href

Msgbox % "first URL is: " page.Evaluate(PageURL).value

Return

I am now trying to do the same with a Google Docs document, and looking in the inspector, I'm trying to work out which element I'm supposed to be working with? Please see the image below; any ideas / hints? I have the text "Link #1" highlighted and the hyperlink is there, but I just can't figure the syntax at the moment...

Image
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Read and open URLs from a Google Doc

26 Apr 2022, 08:52

When I look at the html source of a google doc I see that all my hyperlinks are stored in the format

ulnk_url":"http://www.ahk.com"

Can you get the whole html? IF you can, you could run a regex loop and get everything between ulnk_url":" AND " and that will be a list of all urls in the document.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], macromint and 252 guests