Any way to get element from a webpage?

Hey there! I work as a Support person and love the macro ability of BTT, but was wondering if there was any way to be able to get an HTML element from a webpage and insert that into my macro?

For example, if I'm sending a standard greeting to a customer we have a name field on the form I use that I could parse out a first name for using standard Javascript, but I'd then like to then be able to pipe that into my BTT macro. Is there a way to do this?

You can do that with a "Run Apple Script" action.

The example code writes the text of the reply preview from this page into a BTT variable ("myBTTVariable"):

set myJavascriptCode to "(function(){return document.getElementsByClassName('d-editor-preview')[0].innerText})()"

tell application "Safari"
	tell first window
		tell current tab
			set theResult to do JavaScript myJavascriptCode
		end tell
	end tell
end tell

tell application "BetterTouchTool"
	set_string_variable "myBTTVariable" to theResult
end tell

To run the example:

  • Choose Preferences from the Safari menu, changing to the Advanced panel, and enabling “Show Develop menu in menu bar”, and then choosing Allow JavaScript from Apple Events from the Develop . You can then turn off the Develop menu if desired.

  • Click Reply on this page and write any text

  • Run the Code in Script-Editor or with the BTT "Run Apple Script" action.

  • Check the result under BetterTouchTool Settings:

Then you can use the variable in your BTT macro.

Awesome, thank you! I'll give this a try and report back.

The problem is to access the elements and their properties via JavaScript.

Note: „do JavaScript" requires a function (not a property). If you want to retrieve a property, you have to put it in a closur:

(function(){return el.property})()

Apologies for the super late reply on this, just getting around to it now.

Trying to get this to work in Chrome, but when I try I'm getting console errors. This is what I have:

set myJavascriptCode to "(function(){return document.getElementById('connectFollowWidgetAction').innerText})()"

tell application "Google Chrome"
	tell active tab of front window
		set theResult to execute javascript myJavascriptCode
	end tell
end tell

tell application "BetterTouchTool"
	set_string_variable "myBTTVariable" to theResult
end tell

Even though the document.getElementById('connectFollowWidgetAction').innerText works in the console manually, when I try to execute the script it says that innerText is null

If I use your document.getElementsByClassName here it works fine.

Edit: Yeah, if I do something like getElementsByClassName on my page in the console it returns over a couple dozen objects, but when running through the script it returns nothing. Studff like console logging/alerts work fine when running from the script, so I don't know what's going on.

Edit2: Realized it was because I was inside of a frame. Used window.frames to break out and it's working as expected now. Thanks!

1 Like