Copy to clipboard some characters from an URL Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Primokorn
Posts: 9
Joined: 19 Oct 2020, 03:47

Copy to clipboard some characters from an URL

Post by Primokorn » 19 Oct 2020, 04:17

Hello,
When I click on this kind of URL: https intranet.my-company.com /sites/search/pages/peopleresults.aspx?k=12345 Broken Link for safety
12345 is the ID of a people working in my company
I'd like to copy the ID to the clipboard.

Clicking on the URL opens a new tab in my web browser so it would be nice to be able to close it (via ^w aka CTRL+W for example after a sleep 1000 maybe) and I could copy the URL to my clipboard instead of opening the link if it can help.

I'm very new to AHK but after wathcing videos and reading documentations I'm sure it can be done. I don't have enough knowledge to do it for the moment though.

Coudl you please help me?
Thank you.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Copy to clipboard some characters from an URL

Post by teadrinker » 19 Oct 2020, 05:13

Primokorn wrote: after wathcing videos and reading documentations I'm sure it can be done.
This is a vain sureness. :) To solve such a task, you cannot do without knowledge of Javascript. The best choice is using Tampermonkey. However, you can run Javascript from the address bar.

Primokorn
Posts: 9
Joined: 19 Oct 2020, 03:47

Re: Copy to clipboard some characters from an URL

Post by Primokorn » 19 Oct 2020, 06:55

I'm optimistic :)
I don't have time to check the extension for now but I'll see later how it can help. However I'm not sure if it's worth installing a new extension for a specific need. Maybe it can help for other tasks...

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Copy to clipboard some characters from an URL

Post by haichen » 19 Oct 2020, 07:05

Can't you just right-click on the link and copy it?

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Copy to clipboard some characters from an URL

Post by teadrinker » 19 Oct 2020, 07:11

Primokorn wrote: Maybe it can help for other tasks...
Tampermonkey by itself couldn't help, it is just a user script manager (I mean javascript by «user script»). You should be able to write an appropriate javascript which will do magic.

Primokorn
Posts: 9
Joined: 19 Oct 2020, 03:47

Re: Copy to clipboard some characters from an URL

Post by Primokorn » 19 Oct 2020, 07:18

I'll get the whole URL: https intranet.my-company.com /sites/search/pages/peopleresults.aspx?k=12345
While I only need "12345" (which will be used somewhere else)

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Copy to clipboard some characters from an URL

Post by garry » 19 Oct 2020, 09:53

here an example with youtube link ( in chrome copy link with ctrl+l )
if you have only one '=' , so can use this as delimiter

Code: Select all

;--- for test --- get > fT-tYh3OSUQ
url1:="https://www.youtube.com/watch?v=fT-tYh3OSUQ"
run,%url1%           ;- run first for test
;----------------------------------
y:="v="              ;- delimiter for strsplit

*~$!h::              ;- use alt+h 
clipboard=
url:=""
a:=""
send,^l
sleep,200
send,^c 
clipwait,1,1
if errorlevel=0
   url:=clipboard
sleep,500
a:=StrSplit(url,y)
xx:=a[2]
msgbox, 262208, ,%xx%   ;- get > fT-tYh3OSUQ
return
;==============================================================
EDIT : second example , maybe play with sleep etc

Code: Select all

;--- for test --- get > fT-tYh3OSUQ
url1:="https://www.youtube.com/watch?v=fT-tYh3OSUQ"
run,%url1%           ;- run first for test
;----------------------------------
y:="v="              ;- delimiter for strsplit in youtube

*~$!h::              ;- use alt+h 
clipboard=
url:=""
a:=""
send,^l
sleep,500
send,^c 
clipwait
url:=clipboard
sleep,500
a:=StrSplit(url,y)
xx:=a[2]
msgbox, 262208, ,%xx%   ;- get > fT-tYh3OSUQ
return
;==============================================================
Last edited by garry on 19 Oct 2020, 10:52, edited 1 time in total.

Primokorn
Posts: 9
Joined: 19 Oct 2020, 03:47

Re: Copy to clipboard some characters from an URL

Post by Primokorn » 19 Oct 2020, 10:07

Thank you but I don't see how to proceed.
Once the url opened, pressing the hotkeys only highlight the address bar (= CTRL + L).

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Copy to clipboard some characters from an URL

Post by garry » 19 Oct 2020, 10:13

maybe problem with sleep or clipwait , maybe try

Primokorn
Posts: 9
Joined: 19 Oct 2020, 03:47

Re: Copy to clipboard some characters from an URL

Post by Primokorn » 19 Oct 2020, 10:14

I wrote a simple and basic workaround. As soon as the tab is opened:

Code: Select all

^q::
Send ^l
Sleep 100
Send, {Right}
Sleep 50
Send, {Left 5}
Sleep 50
Send, {Shift down}
;KeyWait, w
Send, {Right 5}
Send, {Shift up}
Send ^c
Sleep 100
Send ^w
return

4fsi2XwboEulzOxm
Posts: 4
Joined: 17 Oct 2020, 08:57

Re: Copy to clipboard some characters from an URL

Post by 4fsi2XwboEulzOxm » 20 Oct 2020, 03:56

In Palemoon you can right click on a link and then press "A" at the context menu to copy the the link's url (worked in old Firefox too). This code would do what you need:

Code: Select all

#IfWinActive, ahk_class MozillaWindowClass

^RButton::		; ctrl+rightclick
Clipboard := ""
SendInput, {RButton}
sleep, 100
SendInput, a
sleep, 400
RegExMatch(Clipboard, "peopleresults\.aspx\?k\=(.*)$", txt)
if (txt1 != "") 
{
Clipboard := txt1
; msgbox % txt1
}
return

#If
I don't know about other browsers.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Copy to clipboard some characters from an URL

Post by haichen » 20 Oct 2020, 06:28

Here yet another idea. Works for me with Firefox and Chrome. With CTRL+LBUTTON the link is copied first and then opened. Limitation for Chrome: uBlock Origin must not run:

Code: Select all

#IfWinActive ahk_exe chrome.exe 
^LButton::Getlink()
#IfWinActive ahk_class MozillaWindowClass
^LButton::Getlink()
#IfWinActive
return

Getlink(){
IDChrome  := WinActive("ahk_exe chrome.exe")
IDMozilla := WinActive("ahk_class MozillaWindowClass")
MouseGetPos,,, IDMouseWindow 
if (IDMozilla = IDMouseWindow) and (A_Cursor != "Arrow") 
   sendinput,{RButton}k{esc}{LButton}
else if (IDChrome = IDMouseWindow) and (A_Cursor != "Arrow")    
   send,{RButton}e{esc}{LButton} ; does not work with uBlock Origin
else 
   sendinput, {LButton}
return
}

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Copy to clipboard some characters from an URL  Topic is solved

Post by haichen » 20 Oct 2020, 07:07

If you only want the number at the end of the link :

Code: Select all

#IfWinActive ahk_exe chrome.exe 
^LButton::Getlink()
#IfWinActive ahk_class MozillaWindowClass
^LButton::Getlink()
#IfWinActive
return

Getlink(){
IDChrome  := WinActive("ahk_exe chrome.exe")
IDMozilla := WinActive("ahk_class MozillaWindowClass")
MouseGetPos ,,, IDMouseWindow 

if (IDMozilla = IDMouseWindow) and (A_Cursor != "Arrow") 
   sendinput,{RButton}k{esc}{LButton}
else if (IDChrome = IDMouseWindow) and (A_Cursor != "Arrow")    
   send,{RButton}e{esc}{LButton} ;does not work with uBlock-origin
else 
   sendinput, {LButton}

RegExMatch(Clipboard, "\d.*", txt)
Clipboard :=txt
return 
}

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Copy to clipboard some characters from an URL

Post by malcev » 20 Oct 2020, 07:11

Depending on site it can be done with acc help.
You can download ahkspy and look at value of acc object under mouse.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Copy to clipboard some characters from an URL

Post by haichen » 20 Oct 2020, 07:18

Isn't that "cracking a nut with a sledgehammer" ?

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Copy to clipboard some characters from an URL

Post by malcev » 20 Oct 2020, 07:39

No, it isn't.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Copy to clipboard some characters from an URL

Post by haichen » 20 Oct 2020, 08:02

If you only want the number at the end of the link without opening the link:
(I changed SendInput to Send, seems to work better)

Code: Select all

#IfWinActive ahk_exe chrome.exe 
^LButton::Getlink()
#IfWinActive ahk_class MozillaWindowClass
^LButton::Getlink()
#IfWinActive

return

Getlink(){
clipboard :=""
SetTimer, RemoveToolTip, -2000
IDChrome  := WinActive("ahk_exe chrome.exe")
IDMozilla := WinActive("ahk_class MozillaWindowClass")
MouseGetPos ,,, IDMouseWindow 

if (IDMozilla = IDMouseWindow) and (A_Cursor != "Arrow") 
   send,{RButton}k 
else if (IDChrome = IDMouseWindow) and (A_Cursor != "Arrow")    
   send,{RButton}e  ;does not work with uBlock-origin
ClipWait, 2
RegExMatch(Clipboard, "\d.*", txt)
Clipboard :=txt
tooltip, % Clipboard " copied to Clipboard"
return 
}

RemoveToolTip:
ToolTip
return
edit: added ClipWait

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Copy to clipboard some characters from an URL

Post by malcev » 20 Oct 2020, 09:09

Code: Select all

setbatchlines -1

f11::
Acc := Acc_ObjectFromPoint(child)
msgbox % Acc.accValue(child)
return


; http://www.autohotkey.com/board/topic/77303-acc-library-ahk-l-updated-09272012/
; https://dl.dropbox.com/u/47573473/Web%20Server/AHK_L/Acc.ahk
;------------------------------------------------------------------------------
; Acc.ahk Standard Library
; by Sean
; Updated by jethrow:
; 	Modified ComObjEnwrap params from (9,pacc) --> (9,pacc,1)
; 	Changed ComObjUnwrap to ComObjValue in order to avoid AddRef (thanks fincs)
; 	Added Acc_GetRoleText & Acc_GetStateText
; 	Added additional functions - commented below
; 	Removed original Acc_Children function
; last updated 2/25/2010
;------------------------------------------------------------------------------

Acc_Init()
{
	Static	h
	If Not	h
		h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}
Acc_ObjectFromEvent(ByRef _idChild_, hWnd, idObject, idChild)
{
	Acc_Init()
	If	DllCall("oleacc\AccessibleObjectFromEvent", "Ptr", hWnd, "UInt", idObject, "UInt", idChild, "Ptr*", pacc, "Ptr", VarSetCapacity(varChild,8+2*A_PtrSize,0)*0+&varChild)=0
	Return	ComObjEnwrap(9,pacc,1), _idChild_:=NumGet(varChild,8,"UInt")
}

Acc_ObjectFromPoint(ByRef _idChild_ = "", x = "", y = "")
{
	Acc_Init()
	If	DllCall("oleacc\AccessibleObjectFromPoint", "Int64", x==""||y==""?0*DllCall("GetCursorPos","Int64*",pt)+pt:x&0xFFFFFFFF|y<<32, "Ptr*", pacc, "Ptr", VarSetCapacity(varChild,8+2*A_PtrSize,0)*0+&varChild)=0
	Return	ComObjEnwrap(9,pacc,1), _idChild_:=NumGet(varChild,8,"UInt")
}

Acc_ObjectFromWindow(hWnd, idObject = -4)
{
	Acc_Init()
	If	DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr", -VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
	Return	ComObjEnwrap(9,pacc,1)
}

Acc_WindowFromObject(pacc)
{
	If	DllCall("oleacc\WindowFromAccessibleObject", "Ptr", IsObject(pacc)?ComObjValue(pacc):pacc, "Ptr*", hWnd)=0
	Return	hWnd
}

Acc_GetRoleText(nRole)
{
	nSize := DllCall("oleacc\GetRoleText", "Uint", nRole, "Ptr", 0, "Uint", 0)
	VarSetCapacity(sRole, (A_IsUnicode?2:1)*nSize)
	DllCall("oleacc\GetRoleText", "Uint", nRole, "str", sRole, "Uint", nSize+1)
	Return	sRole
}

Acc_GetStateText(nState)
{
	nSize := DllCall("oleacc\GetStateText", "Uint", nState, "Ptr", 0, "Uint", 0)
	VarSetCapacity(sState, (A_IsUnicode?2:1)*nSize)
	DllCall("oleacc\GetStateText", "Uint", nState, "str", sState, "Uint", nSize+1)
	Return	sState
}

Acc_SetWinEventHook(eventMin, eventMax, pCallback)
{
	Return	DllCall("SetWinEventHook", "Uint", eventMin, "Uint", eventMax, "Uint", 0, "Ptr", pCallback, "Uint", 0, "Uint", 0, "Uint", 0)
}

Acc_UnhookWinEvent(hHook)
{
	Return	DllCall("UnhookWinEvent", "Ptr", hHook)
}
/*	Win Events:
	pCallback := RegisterCallback("WinEventProc")
	WinEventProc(hHook, event, hWnd, idObject, idChild, eventThread, eventTime)
	{
		Critical
		Acc := Acc_ObjectFromEvent(_idChild_, hWnd, idObject, idChild)
		; Code Here:
	}
*/

; Written by jethrow
Acc_Role(Acc, ChildId=0) {
	try return ComObjType(Acc,"Name")="IAccessible"?Acc_GetRoleText(Acc.accRole(ChildId)):"invalid object"
}
Acc_State(Acc, ChildId=0) {
	try return ComObjType(Acc,"Name")="IAccessible"?Acc_GetStateText(Acc.accState(ChildId)):"invalid object"
}
Acc_Location(Acc, ChildId=0, byref Position="") { ; adapted from Sean's code
	try Acc.accLocation(ComObj(0x4003,&x:=0), ComObj(0x4003,&y:=0), ComObj(0x4003,&w:=0), ComObj(0x4003,&h:=0), ChildId)
	catch
		return
	Position := "x" NumGet(x,0,"int") " y" NumGet(y,0,"int") " w" NumGet(w,0,"int") " h" NumGet(h,0,"int")
	return	{x:NumGet(x,0,"int"), y:NumGet(y,0,"int"), w:NumGet(w,0,"int"), h:NumGet(h,0,"int")}
}
Acc_Parent(Acc) { 
	try parent:=Acc.accParent
	return parent?Acc_Query(parent):
}
Acc_Child(Acc, ChildId=0) {
	try child:=Acc.accChild(ChildId)
	return child?Acc_Query(child):
}
Acc_Query(Acc) { ; thanks Lexikos - www.autohotkey.com/forum/viewtopic.php?t=81731&p=509530#509530
	try return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
Acc_Error(p="") {
	static setting:=0
	return p=""?setting:setting:=p
}
Acc_Children(Acc) {
	if ComObjType(Acc,"Name") != "IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	else {
		Acc_Init(), cChildren:=Acc.accChildCount, Children:=[]
		if DllCall("oleacc\AccessibleChildren", "Ptr",ComObjValue(Acc), "Int",0, "Int",cChildren, "Ptr",VarSetCapacity(varChildren,cChildren*(8+2*A_PtrSize),0)*0+&varChildren, "Int*",cChildren)=0 {
			Loop %cChildren%
				i:=(A_Index-1)*(A_PtrSize*2+8)+8, child:=NumGet(varChildren,i), Children.Insert(NumGet(varChildren,i-8)=9?Acc_Query(child):child), NumGet(varChildren,i-8)=9?ObjRelease(child):
			return Children.MaxIndex()?Children:
		} else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
	if Acc_Error()
		throw Exception(ErrorLevel,-1)
}
Acc_ChildrenByRole(Acc, Role) {
	if ComObjType(Acc,"Name")!="IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	else {
		Acc_Init(), cChildren:=Acc.accChildCount, Children:=[]
		if DllCall("oleacc\AccessibleChildren", "Ptr",ComObjValue(Acc), "Int",0, "Int",cChildren, "Ptr",VarSetCapacity(varChildren,cChildren*(8+2*A_PtrSize),0)*0+&varChildren, "Int*",cChildren)=0 {
			Loop %cChildren% {
				i:=(A_Index-1)*(A_PtrSize*2+8)+8, child:=NumGet(varChildren,i)
				if NumGet(varChildren,i-8)=9
					AccChild:=Acc_Query(child), ObjRelease(child), Acc_Role(AccChild)=Role?Children.Insert(AccChild):
				else
					Acc_Role(Acc, child)=Role?Children.Insert(child):
			}
			return Children.MaxIndex()?Children:, ErrorLevel:=0
		} else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
	if Acc_Error()
		throw Exception(ErrorLevel,-1)
}
Acc_Get(Cmd, ChildPath="", ChildID=0, WinTitle="", WinText="", ExcludeTitle="", ExcludeText="") {
	static properties := {Action:"DefaultAction", DoAction:"DoDefaultAction", Keyboard:"KeyboardShortcut"}
	AccObj :=   IsObject(WinTitle)? WinTitle
			:   Acc_ObjectFromWindow( WinExist(WinTitle, WinText, ExcludeTitle, ExcludeText), 0 )
	if ComObjType(AccObj, "Name") != "IAccessible"
		ErrorLevel := "Could not access an IAccessible Object"
	else {
		StringReplace, ChildPath, ChildPath, _, %A_Space%, All
		AccError:=Acc_Error(), Acc_Error(true)
		Loop Parse, ChildPath, ., %A_Space%
			try {
				if A_LoopField is digit
					Children:=Acc_Children(AccObj), m2:=A_LoopField ; mimic "m2" output in else-statement
				else
					RegExMatch(A_LoopField, "(\D*)(\d*)", m), Children:=Acc_ChildrenByRole(AccObj, m1), m2:=(m2?m2:1)
				if Not Children.HasKey(m2)
					throw
				AccObj := Children[m2]
			} catch {
				ErrorLevel:="Cannot access ChildPath Item #" A_Index " -> " A_LoopField, Acc_Error(AccError)
				if Acc_Error()
					throw Exception("Cannot access ChildPath Item", -1, "Item #" A_Index " -> " A_LoopField)
				return
			}
		Acc_Error(AccError)
		StringReplace, Cmd, Cmd, %A_Space%, , All
		properties.HasKey(Cmd)? Cmd:=properties[Cmd]:
		try {
			if (Cmd = "Location")
				AccObj.accLocation(ComObj(0x4003,&x:=0), ComObj(0x4003,&y:=0), ComObj(0x4003,&w:=0), ComObj(0x4003,&h:=0), ChildId)
			  , ret_val := "x" NumGet(x,0,"int") " y" NumGet(y,0,"int") " w" NumGet(w,0,"int") " h" NumGet(h,0,"int")
			else if (Cmd = "Object")
				ret_val := AccObj
			else if Cmd in Role,State
				ret_val := Acc_%Cmd%(AccObj, ChildID+0)
			else if Cmd in ChildCount,Selection,Focus
				ret_val := AccObj["acc" Cmd]
			else
				ret_val := AccObj["acc" Cmd](ChildID+0)
		} catch {
			ErrorLevel := """" Cmd """ Cmd Not Implemented"
			if Acc_Error()
				throw Exception("Cmd Not Implemented", -1, Cmd)
			return
		}
		return ret_val, ErrorLevel:=0
	}
	if Acc_Error()
		throw Exception(ErrorLevel,-1)
}

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Copy to clipboard some characters from an URL

Post by haichen » 21 Oct 2020, 02:54

Hi malcev,
I have only waited for this. :D
Thanks a lot for the well working code.
Shouldn't it work for Chrome too?

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Copy to clipboard some characters from an URL

Post by malcev » 22 Oct 2020, 03:18

Yes.
Chrome should be run with --force-renderer-accessibility flag
Or You should send WM_GETOBJECT by Yourself.

Post Reply

Return to “Ask for Help (v1)”