Applescript/Javascript question

Here is my situation in detail.

I have figured out how to login to a website, not using javascript, but using “keystrokes”, “tabs” and “returns” to find the username and password fields. I know this can be unreliable as websites change but for now I am just trying to keep it simple.

After I log in to a site, like a bank account, I want to grab the account balance and import it to Numbers.

I have figured out how to insert a predermined text into a specific cell in Numbers and probably could expand that to dialog boxes ect, but that is not what I am really trying to accomplish.

Basically I don’t know what I need to do to grab the data from the current website after I have logged in. Doing research I realize it is something todo with javascript.

For now and to apply it to the bigger plan, I just want to know how to locate data of a website that I do not have to log into in Safari via javascript and display the data in the results of Applescript Editor. And I am trying to figure out how I could use that result to display it in another application (ei Numbers). I then would apply this to other sites, so I would like to know exactly where to look to find the data.

I am thinking that stock websites would be a great example but I am having a tough time finding one that fits this example.

Thank you for your help!

Hello.

You may not be able to grab all of the text you see, if it is sent with AJAX for instance, just in order to hinder you in that. Wolfram Alpha being one such example.

However, most sites lets you grab the text you see. Then a snippet like below, can be used to grab all of the text with Safari. You’ll have to look into the scripting dictionaries from other browser to find simili.

Having the text, you may query it with sed, grep or xpath/xquery, or simply use text item delimiters in AppleScript to get whatever you are after. It may take some work to figure it all out. The Inspector of Safari may be of some help to reveal the tag-hierarcy.

tell application "Safari"
	set thet to source of its front document
end tell

This may not be elegant enough for your needs, but I have to frequently obtain data from websites that I have Applescript log me in to. My strategy is to then select the particular data I want on the screen then copy this to the clipboard. A separate Applescript is then run to process the information, open the appropriate software, say Numbers, and paste in the data. Works like a charm.:slight_smile:

McUsrII-

Thank you for that info,

I confirmed that the data was not in the regular Source of the HTML, however was in the DOM tree. I am able to click (UI) on the account and then on the account balance is in the Source.

Do you know if the data I want to display HAS to be in the Source or can I access it from the DOM tree? If not, the above is a work around I can use. But I need todo more searching on how to EXACTLY locate the button via Javascript and click it

Something like
do JavaScript “document.getElementsByID(‘acct-checking’)[0].click()” in document 1

—But this doesn’t seem to work

Do you know what is the javascript command to display the information from the Source or DOM tree? I get the “.click()” but what is the display data from that line command?

Thank you again!

Hello.

I think you should really google for missing pieces, it will save you a lot of time. Sometimes however, I use the innerHTML property to get the contents of a node ( the item you got to by getElementById ) in the domtree. The Web inspector of Safari should come in handy while you work on this.

I must say, that Fondomatic’s solution, is what I think is generally the easier solution, and the better one. Because his way of working, is a much faster one to get something to work. :slight_smile: There is a lot of tweaking with scripting web-pages, like if a page slows down in loading and the like, and God forbid, redesigning of the page you have just scripted.

Ok, well to get started could you possibly share with me what needs to be fixed from this script? I think this fix of this example would help me tremendously.

The goal is to display the word “Advertising” (located in the bottom right) of Google.com in a dialog box?
I would like to figure out how todo this via Javascript.


tell application "Safari"
	activate
	open location "https://www.google.com"
	delay 3
	set selectedText to (do JavaScript "document.getElementByID(\"fblf\").childNodes[0]" in document 1)
	display dialog selectedText
end tell
--->returns "msng" in Dialog Box

Thanks!!! :slight_smile:

Hello.

The snippet below works, and is just to get you started. The element you are looking for is a class, so you’ll have to use GetClassById, (notice case of Id!). And from there onwards, you are on your own! :slight_smile:

tell application "Safari"
	activate
	#open location "https://www.google.com"
	#delay 3
	tell its front document
		set selectedText to do JavaScript "document.getElementById('cst').innerHTML"
	end tell
	display dialog selectedText
end tell

If the website is completely driven by AJAX you should use JavaScript. The source of the document, in safari, isn’t updated into AppleScript, nor in the “show source” window, so what you need to do is tell JavaScript to return it’s dom object that has been created by it.

Code should look something like this to get the contents of the body:


tell application "Safari"
   tell its front document
      return do JavaScript "document.body.innerHTML"
end tell
end tell

Hello.

This snippet may take you a little further, it opens the link named Advertising.

set myJavaScript to "

function GetLinkIndexNamed(theLinkName)
{
   for (i=0; i<document.links.length; i++)
   {
       var thisLinkContent = document.links[i].innerText;
           
       // Convert all non-breaking space to plain for matching
       thisLinkContent = thisLinkContent.replace(/\\xA0/g, ' ');

       // IS it the next link?
       if (thisLinkContent.toLowerCase() == theLinkName.toLowerCase())
       {        
           return i;
       }
   }
   return -1;
}
var foundLinkIndex = -1;
foundLinkIndex = GetLinkIndexNamed('Advertising'); 
if (foundLinkIndex != -1)
{
   document.location=document.links[foundLinkIndex];
}

"
tell application "Safari"
	activate
  open location "https://www.google.com"
   delay 10
	do JavaScript myJavaScript in document 1
end tell

Thank you all for your help. I have learned great new things. I have been able to confirm that the data I am trying to access is indeed in the source. But still I cannot access it. For now, I am just trying to use the tools I have learned and then apply it to the bigger picture.

The data is just text, not a button or link. It does not have an ID… or rather it says “” in the Web inspector where the ID should be. The class is className: “details yui3-u-2-5 amount” and the data is located in the innerText.

When I run the following script, I just get Missing Value or MSNG in the Dialog. I am still confused on how I can display this value in the dialog as this script doesn’t seem to work. :expressionless:

tell application "Safari"
	activate
	delay 3
	do JavaScript
	set selectedText to do JavaScript "document.getElementByClassName(\"details yui3-u-2-5 amount\").outerText:.[0]" in document 1
	display dialog selectedText
end tell

This is the Source of the what I am trying to grab info from. I altered some info in it so it would not reveal any personal info.

BANK SAVINGS *****
Available Balance: $500.00

//****NOTE FROM PAULMC49255: $500 IS A FAKE VALUE, But for purposes of this, this is what I am trying to display

Hello.

It may be hard to get info out via javascript when you have to do it by class Id.
But if you want to follow that road, then I suggest you go and google for a solution in javascript.

This is a problem, which is easily solved by using the source of the document as raw material for the text item delimiters of AppleScript.

You can really read up on text item delimiters here by searching on them. In the Unscripted section of this Site, there is a fairly good tutorial about text item delimiters that you should read.

When you have read that you will be competent to extract the info you want from the source of the document.

I hope this helps, and good luck! :slight_smile: