Guest User

Save current web url to pre-selected folder

a guest
Jun 29th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires AutoHotkey v2.0
  2.  
  3. F1::{
  4.     ;replace the following placeholder with the directory that should contain the links:
  5.     path := "D:\your_folder\"
  6.    
  7.     filename := RegExReplace(WinGetTitle("A"),'[>:\"\/\\|?]',"") ".url"          ;these characters are not allowed in file names
  8.    url := getUrl()
  9.    if (!url) {
  10.        MsgBox("Couldn't retrieve an URL from the active window.", "Error")
  11.        return
  12.    }
  13.    try
  14.        FileAppend("[InternetShortcut]`nURL=" url, path filename)
  15.    catch
  16.        FileAppend("[InternetShortcut]`nURL=" url, path filename:=A_Now ".url")  ;if the regex doesn't cut it: file name is current time
  17.  
  18.    if FileExist(path filename)
  19.        TrayTip('Saved link as `n"' filename '"')
  20. }
  21.  
  22.  
  23. ;Source:         mikeyww        https://www.autohotkey.com/boards/viewtopic.php?t=129379
  24. ;Adapted from:   anonymous1184  https://gist.github.com/anonymous1184/7cce378c9dfdaf733cb3ca6df345b140
  25.  
  26. getUrl() {  ; Get URL of the active Web browser window
  27. Static TreeScope_Descendants     := 4
  28.      , UIA_ControlTypePropertyId := 30003
  29.      , UIA_DocumentControlTypeId := 50030
  30.      , UIA_EditControlTypeId     := 50004
  31.      , UIA_ValueValuePropertyId  := 30045
  32. IUIAutomation := ComObject('{FF48DBA4-60EF-4201-AA87-54103EEF594E}'
  33.                          , '{30CBE57D-D9D0-452A-AB13-7AC5AC4825EE}')
  34. eRoot         := ComValue(13, 0)
  35. If HRESULT    := ComCall(6, IUIAutomation, 'Ptr', WinGetID('A'), 'Ptr*', eRoot)
  36.  Throw Error('IUIAutomation::ElementFromHandle()', -1, HRESULT)
  37. ctrlTypeId    := WinGetClass('A') ~= 'Chrome' ? UIA_DocumentControlTypeId : UIA_EditControlTypeId
  38. value         := Buffer(8 + 2 * A_PtrSize, 0)
  39. NumPut('UShort', 3, value, 0), NumPut('Ptr', ctrlTypeId, value, 8)
  40. condition     := ComValue(13, 0)
  41. If HRESULT    := A_PtrSize = 8
  42.  ? ComCall(23, IUIAutomation, 'UInt', UIA_ControlTypePropertyId, 'Ptr', value, 'Ptr*', condition)
  43.  : ComCall(23, IUIAutomation
  44.     , 'UInt'  , UIA_ControlTypePropertyId
  45.     , 'UInt64', NumGet(value, 0, 'UInt64')
  46.     , 'UInt64', NumGet(value, 8, 'UInt64')
  47.     , 'Ptr*'  , condition
  48.    )
  49.  Throw Error('IUIAutomation::CreatePropertyCondition()', -1, HRESULT)
  50. eFirst := ComValue(13, 0)
  51. If HRESULT := ComCall(5, eRoot, 'UInt', TreeScope_Descendants, 'Ptr', condition, 'Ptr*', eFirst)
  52.  Throw Error('IUIAutomationElement::GetRootElement()', -1, HRESULT)
  53. propertyValue := Buffer(8 + 2 * A_PtrSize)
  54. If HRESULT := ComCall(10, eFirst, 'UInt', UIA_ValueValuePropertyId, 'Ptr', propertyValue)
  55.  Throw Error('IUIAutomationElement::GetCurrentPropertyValue()', -1, HRESULT)
  56. ObjRelease(eFirst.Ptr), ObjRelease(eRoot.Ptr)
  57. Try {
  58.  pProperty := NumGet(propertyValue, 8, 'Ptr')
  59.  Return StrGet(pProperty, 'UTF-16')
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment