Get latitude and longitude of the machine

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Get latitude and longitude of the machine

Post by Grovkillen » Wed Jul 08, 2020 1:22 pm

Just as a proof of concept, this is how you tap into the JavaScript world from MS. Great to get stuff like current position. Would be great if we could start the browser hidden/headless.

Code: Select all

Let>CHROMEDRIVER_EXE=C:\Program Files (x86)\Macro Scheduler 15\chromedriver.exe
LabelToVar>RAW_CODE,JS_CODE
StringReplace>JS_CODE,",\",JS_CODE
ChromeStart>strSessionID
ChromeNavigate>strSessionID,url,https://www.google.com/
ChromeFindElements>strSessionID,css selector,body,strElementID
GoSub>ExecuteJS,strSessionID,JS_CODE
ChromeGetElementData>strSessionID,strElementID_1,text,GeoLocation
ChromeQuit>strSessionID
GoSub>SeparateLocation,GeoLocation

MDL>Your current position:%CRLF%Lat=%Latitude%%CRLF%Long=%Longitude%

SRT>ExecuteJS
  Let>HTTP_POSTJSON=1
  Let>body= { "script": "%ExecuteJS_Var_2%", "args": [] }
  Let>CHROMEDRIVER_PORT=9515
  HTTPRequest>http://localhost:%CHROMEDRIVER_PORT%/session/%ExecuteJS_Var_1%/execute,,POST,body,theResult
END>ExecuteJS

SRT>SeparateLocation
  Separate>SeparateLocation_Var_1,|,Array
  Let>Latitude=Array_1
  Let>Longitude=Array_2
END>SeparateLocation

/*
RAW_CODE:
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
  document.body.innerHTML = position.coords.latitude + "|" + position.coords.longitude;
}
*/
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Get latitude and longitude of the machine

Post by Marcus Tettmar » Thu Jul 09, 2020 11:53 am

Sadly I don't think HTML5 geolocation works when Chrome is in headless mode. So it wouldn't work anyway. Also bear in mind that opt in is required so this will require the user to confirm an opt in pop up. At least once anyway.

If you have access to a web server with SSL you could simplify this by creating your own html page. E.g.:

Code: Select all

<html>

<head>
    <script>

        function getLocation() {
            var x = document.getElementById("pos");
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }

        function showPosition(position) {
            var x = document.getElementById("pos");
            x.innerHTML = position.coords.latitude + "," + position.coords.longitude;
        }
    </script>
</head>

<body onload="getLocation()">
    <div id="pos"></div>
</body>

</html>
Then you don't need to do any javascript injection. Just navigate to the page, then grab the text of the "pos" div.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Get latitude and longitude of the machine

Post by Grovkillen » Thu Jul 09, 2020 12:21 pm

Localhost allows for the full API I think?
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Get latitude and longitude of the machine

Post by Marcus Tettmar » Thu Jul 09, 2020 12:34 pm

You're quite right - just tested it and it works.

So you could just save that file to your PC and then just use the chrome functions to navigate to it locally and pull the value.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Get latitude and longitude of the machine

Post by Marcus Tettmar » Thu Jul 09, 2020 12:45 pm

E.g.:

Code: Select all

Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,file:///C:/Users/user/Documents/location.html
ChromeFindElements>session_id,id,pos,posEl
Let>position={""}
While>position={""}
  ChromeGetElementData>session_id,posEl_1,text,position
EndWhile
ChromeQuit>session_id
MessageModal>position
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Get latitude and longitude of the machine

Post by Marcus Tettmar » Thu Jul 09, 2020 1:04 pm

Self contained version:

Code: Select all

Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
Let>tmpFile=%SCRIPT_DIR%\loc.html
StringReplace>tmpFile,\,/,urlFile
Let>script=<script>navigator.geolocation.getCurrentPosition(function(position) { document.body.innerText=position.coords.latitude + ',' + position.coords.longitude})</script>
WriteLn>tmpFile,r,script
ChromeStart>session_id
ChromeNavigate>session_id,url,file:///%urlFile%
ChromeFindElements>session_id,xpath,//body,body
Let>position={""}
While>position={""}
  ChromeGetElementData>session_id,body_1,text,position
EndWhile
ChromeQuit>session_id
Mdl>position
DeleteFile>tmpFile
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Get latitude and longitude of the machine

Post by Grovkillen » Thu Jul 09, 2020 1:25 pm

Awesome! Thanks Marcus for the feedback and help.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Get latitude and longitude of the machine

Post by Marcus Tettmar » Thu Jul 09, 2020 2:50 pm

Got a little obsessed with making it shorter. How about we remove the need to have an html file or even save one, and how about we make the JS put the position in the clipboard, and change the title of the page so we only need to wait for that. Then we're down to 9 lines including the diagnostic message:

Code: Select all

Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,chrome://version
Let>body= { "script":"navigator.geolocation.getCurrentPosition(function(position) { navigator.clipboard.writeText(position.coords.latitude + ',' + position.coords.longitude); window.document.title='got_location'; })", "args":[] }
HTTPRequest>http://localhost:9515/session/%session_id%/execute,,POST,body,res
WaitWindowOpen>got_location*
GetClipBoard>position
ChromeQuit>session_id
Mdl>position
But it's only one more line to NOT use the clipboard so:

Code: Select all

Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,chrome://version
Let>body= { "script":"navigator.geolocation.getCurrentPosition(function(position) {document.body.innerText=(position.coords.latitude + ',' + position.coords.longitude); window.document.title='got_location'; })", "args":[] }
HTTPRequest>http://localhost:9515/session/%session_id%/execute,,POST,body,res
WaitWindowOpen>got_location*
ChromeFindElements>session_id,xpath,//body,body
ChromeGetElementData>session_id,body_1,text,position
ChromeQuit>session_id
Mdl>position
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Get latitude and longitude of the machine

Post by Grovkillen » Thu Jul 09, 2020 4:49 pm

Great! Love it :D I can relate to the obsession.
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts