WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System_CodeDom

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Grab URL from Chrome

 Keywords:  

;***************************************************************************
;**  Grab URL from Chrome
;**
;** Purpose:  Loops displaying the current URL loaded in Chrome.
;** Inputs:
;** Outputs: Results in Display
;**
;** Developer: JTaylor 2014.06.18
;************************************************************************************************************
;**  This retrieves the Current URL from Chrome Version 35.
;**  I know it won't work with 32 (it will with a minor tweak) due to changes somewhere along the way to 35.
;**  Guessing it will break again soon but offers an approach that could be modified as versions change.
;************************************************************************************************************

  If !AppExist("chrome.exe",0, 0) Then
    Display(2,"Note","Chrome is not running.")
    Exit
  EndIf

   ;Tell WIL not to complain when box is closed
   IntControl(12, 1+4, 0, 0, 0)
   ;Enable Close button
   IntControl(1008, 1, 0, 0, 0)

  BoxOpen("Chrome URL","")

  GoSub Load_Routines
  Load_GetMyURL()
  url_save = ""

  While 1
    url   = sha1.GetMyURL()
    If url != url_save
       ;Display(2,"URL",url)
       BoxDataClear(1,"TOP")
       BoxText(url)
    EndIf
    url_save = url
    TimeDelay(3)
  EndWhile

Exit


:LOAD_ROUTINES

#DefineSubRoutine Load_GetMyURL()

;***************************************************************************
;**  Run C# in memory using WinBatch - Use Namespace.Class defined in CSharpeSource
;**
;** Purpose:  Run C# in memory using WinBatch - Using Namespace.Class defined in CSharpeSource
;** Inputs:
;** Outputs: Results in message
;**
;** Developer: Deana Falk 2014.04.14
;***************************************************************************

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; mscorlib assembly is automatically loaded by WinBatch when the CLR is loaded.
; ObjectClrOption ('use','mscorlib, version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

; ObjectClrOption("version", "v2.0.50727")
; ObjectClrOption("use","System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
  ObjectClrOption ( 'use', 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Compiles the c# code in Memory
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')
  objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
  objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ;TRUE

  objParams.ReferencedAssemblies.Add("System.dll")
  objParams.ReferencedAssemblies.Add("C:\Windows\Microsoft.NET\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll")
  objParams.ReferencedAssemblies.Add("C:\Windows\Microsoft.NET\assembly\GAC_MSIL\UIAutomationClient\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationClient.dll")

;Make sure the cSharp code includes both namepspace and class that can be later used by ObjectCLRNew
  cSharpSource =               `namespace URLEngine {`:@LF
  cSharpSource = cSharpSource :`using System;`:@LF
  cSharpSource = cSharpSource :`using System.Text;`:@LF
  cSharpSource = cSharpSource :`using System.Threading;`:@LF
  cSharpSource = cSharpSource :`using System.Runtime.InteropServices;`:@LF
  cSharpSource = cSharpSource :`using System.Text.RegularExpressions;`:@LF
  cSharpSource = cSharpSource :`using System.Collections.Generic;`:@LF
  cSharpSource = cSharpSource :`using System.Windows.Automation;`:@LF
  cSharpSource = cSharpSource :`using System.Diagnostics;  `:@LF

  cSharpSource = cSharpSource :`public class MyURLEngine {`:@LF


  cSharpSource = cSharpSource :`public string GetMyURL() `:@LF
  cSharpSource = cSharpSource :`  { `:@LF
  cSharpSource = cSharpSource :`    `:@LF

  cSharpSource = cSharpSource :`  Process[] procsChrome = Process.GetProcessesByName("chrome");  `:@LF

  cSharpSource = cSharpSource :`  int myint = procsChrome.Length;  `:@LF
  cSharpSource = cSharpSource :`  string mystring = myint.ToString();  `:@LF
; cSharpSource = cSharpSource :`  return mystring;  `:@LF

  cSharpSource = cSharpSource :`  foreach (Process chrome in procsChrome) {  `:@LF
  cSharpSource = cSharpSource :`  if (chrome.MainWindowHandle == IntPtr.Zero) {  `:@LF
  cSharpSource = cSharpSource :`    continue;  `:@LF
  cSharpSource = cSharpSource :`  } `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);  `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  AutomationElement elmUrlBar = null;  `:@LF
  cSharpSource = cSharpSource :`  try {  `:@LF
  cSharpSource = cSharpSource :`        var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));  `:@LF
  cSharpSource = cSharpSource :`        if (elm1 == null) { continue; } // not the right chrome.exe  `:@LF
  cSharpSource = cSharpSource :`        var elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding :(  `:@LF
  cSharpSource = cSharpSource :`        var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));  `:@LF
  cSharpSource = cSharpSource :`        var elm4 = TreeWalker.RawViewWalker.GetNextSibling(elm3); // I don't know a Condition for this for finding :(  `:@LF
  cSharpSource = cSharpSource :`        var elm7 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));  `:@LF
  cSharpSource = cSharpSource :`        elmUrlBar = elm7.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));  `:@LF
  cSharpSource = cSharpSource :`  } catch {  `:@LF
  cSharpSource = cSharpSource :`             continue;  `:@LF
  cSharpSource = cSharpSource :`  } `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  if (elmUrlBar == null) {  `:@LF
  cSharpSource = cSharpSource :`    continue;  `:@LF
  cSharpSource = cSharpSource :`  }  `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`     if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty)) {  `:@LF
  cSharpSource = cSharpSource :`     continue;  `:@LF
  cSharpSource = cSharpSource :`  } `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();  `:@LF
  cSharpSource = cSharpSource :`  if (patterns.Length == 1) {  `:@LF
  cSharpSource = cSharpSource :`  try {  `:@LF
  cSharpSource = cSharpSource :`        string ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;  `:@LF
  cSharpSource = cSharpSource :`        return ret;  `:@LF
  cSharpSource = cSharpSource :`  } catch { }  `:@LF
  cSharpSource = cSharpSource :`        continue;  `:@LF
  cSharpSource = cSharpSource :`  }  `:@LF
  cSharpSource = cSharpSource :`  }  `:@LF

  cSharpSource = cSharpSource :` return "";  `:@LF

  cSharpSource = cSharpSource :`     } `:@LF

  cSharpSource = cSharpSource :` }`:@LF
  cSharpSource = cSharpSource :`}`:@LF

  objResult = objCSharp.CompileAssemblyFromSource(objParams,cSharpSource)

  ;Compiler Output
  If objResult.Output.Count > 0 Then
    strOutput = ''
    For x = 0 To objResult.Output.Count-1
       If strOutput == "" Then
         strOutput = objResult.Output.Item(x)
       Else
         strOutput = strOutput:@LF:objResult.Output.Item(x)
       EndIf
    Next
    Pause('Compiler Output',strOutput)
  EndIf

  ; Compiler Errors
  If objResult.Errors.Count > 0 Then
    strErrors = ''
    ForEach ce In objResult.Errors
      ;Pause("Error", ce.ToString())
      If strErrors == "" Then
        strErrors = ce.ToString()
      Else
        strErrors = strErrors:@LF:ce.ToString()
      EndIf
    Next
    Pause('Compiler Errors',strErrors)
    Exit
  EndIf

  sha1 = ObjectClrNew( 'URLEngine.MyURLEngine' )

#EndSubRoutine

Return

Article ID:   W18515
Filename:   Grab URL from Chrome.txt
File Created: 2017:07:28:12:59:38
Last Updated: 2015:05:08:10:03:38