I have an issue with finding the correct field from the webpage below. The dropdown menu doesn't provide any fields. Is it possible to write the field I want?
The objective is to set the highlighted word to a variable, for later use. The link to the webpage is below.
I hope you don't mind but I wasn't absolutely sure what exactly you were asking so I've created a macro for you that lets you enter a French word and then returns the English translation using the website you provided. The macro does not need you to have Safari open to work properly.
I hope that by looking at the macro you will be able to figure out how to achieve what you want.
Test Get Translation.kmmacros (4.4 KB)
And here's a screenshot of the actual macro:
Thank you very much @tiffle
You solved my issue which I greatly appreciate. However you solved it alternatively, which I think is cool!
Cool is good! You’re welcome!
I am curious - @tiffle - could you elaborate a bit on how you made the regex key?
I just read up on regex, and your use is really cool!
I'll give it a go...
I very often take this approach to pulling stuff off web pages without having to open them in a browser. I'll describe what I did in your case, but it can be applied generally. It is particularly useful in this case because to get the translation from Reverso, you can easily construct the URL for the appropriate page - (1) on the screenshot below.
Loading up the Reverso page in Safari I then used the Inspect Element developer facility to find out what the HTML looks like for the translated word (2). The inspector revealed the HTML (3) and you can see how there is a similarity between the HTML for each of the translations.
To actually develop the regex needed I use an app called RegExRX but there are free web-based ones that are similar. A good one is regex101.com. Anyway, I loaded up the text of the Reverso web page into RegExRX and used its tools to pick out the HTML for the translations - (1), (2) and (3) etc in the screenshot below. The search pattern (4) I could easily create since I am very familiar with regular expressions, but in RegExRX you can use the little dropdown helper to assist you. It's very straightforward. The result is (5) which shows how "mountain" is pulled out.
The first bit
<a href="/translation/ matches literally character-for-character in the source text.
.*/ matches any number of characters up to the following /
(\w*)" matches any number of word characters up to the following quote, " and because it's in parentheses it's what's called a capturing group. In other words, the regex remembers what's matched and allows you to use it; so in this case, this capturing group is stored in the KM variable Local_Translation
This regex gets just the first translation, but you can see that using this regex repeatedly, it's possible to pull out all the other translations too, so you could extend the macro to get all the translations and then allow you to choose the one you want. I'll leave that as an interesting learning exercise for you!
I hope this helps a bit. Cheers!
I can't thank you enough - more than satisfactory explanation.
I get why you would use it for other purposes - I'll begin practicing
Typically, when the dropdown menu does not list any fields it is because:
- Keyboard Maestro does not have Automation permission for the browser.
- The web page has frames.
- The web page has cross origin frames.