Assign Bookmarklet to Touchbar button

Hello
I want to assign a Safari bookmarklet to a touchbar button using BTT on an M1 Macbook. Using the Real Javascript option doesn't work since, presumably, the bookmarklet code isn't real javascript. The code works 100% using the bookmarklet but zero % as a BTT button. Here's the code. Thanks in advance:

javascript:%20(function()%7Bvar%20title=encodeURIComponent(window.document.title);var%20url=encodeURIComponent(window.location.href);var%20ua=navigator.userAgent;var%20ver=%222.5%22;if%20(isM())%20mAdd();%20else%20dAdd();function%20dAdd()%7Bvar%20br=getBrowser();var%20w=579;var%20h=467;if%20(br=='firefox')%7Bif%20(isMac())%20h=462;else%20h=495;%7Delse%20if%20(br=='opera')%7Bw=600;h=554;%7Delse%20if(br=='safari')%7Bh=488;%7Delse%20if%20(br=='chrome'%20&&%20isMac())%7Bh=467;%7Delse%20if%20(br=='ie')%7Bh=452;%7Dvar%20left=(screen.width-w)/2;var%20tops=(screen.height-h)/3;win=window.open('Bookmark Ninja - Login';%7Dfunction%20isMac()%7Bif%20(ua.match(/Macintosh/i))%20return%20true;else%20return%20false;%7D%7D)();

What is this script supposed to do? It contains multiple errors.

In general you would need to use Apple Script to execute Java Script in Safari, e.g.

tell application "Safari"
    delay 0.1
    do JavaScript "document.getElementById('accountEnabledSelection').value=1" in current tab of window 1
    do JavaScript "document.getElementsByName('demo')[0].click();" in current tab of window 1


end tell

Or if you are on Monterey you can use the Shortcuts app to create such an action (which can then be called from BTT):

Thanks, Andreas.

I'm trialling a cloud bookmark facility so that my bookmarks are available across devices and different browsers. The one I'm using is Bookmark Ninja https://www.bookmarkninja.com

It's quite nice and could work for me. Anyway, the company provides extensions for browsers like Chrome, Opera, Firefox, etc but not for Safari. For the latter it only provides bookmarklets which rely on Javascript - about which I am totally ignorant. These bookmarklets sit on the bookmarks bar unlike the extensions which appear in the toolbar

The code I posted relates to one of the bookmarklets which, when clicked, will add a bookmark to my Bookmark Ninja dashboard in the tab and category I select. In other words it acts like Cmd-D that you use to add a bookmark in your local browser. It works perfectly as a bookmarklet but I have to have the bookmarks bar showing in order to use it. I'd like to hide the bar and use the touch bar to initiate the action.

It's not a big deal and I'm not that much of a real estate freak that I have to hide the bookmarks bar but, if it was easy, then I'd put it on the touch bar.

BTT is indispensable to me for so many reasons but if the coding skills to make it work with this are beyond me, which they undoubtedly are, I won't lose much sleep.

Thanks again.

Mick

Thanks Andreas.

I cracked it by throwing the javascript in the bin and achieved the same goal with a straight Apple script. It needs a bit of cosmetic stuff but works perfectly.

How were you able to do this? I'm still struggling with how to do this. I have the javascript function and bookmarklet running and working but when I try to put it into BTT as an apple script with Firefox it doesn't work. Keeps giving me errors when trying to compile it :disappointed:

Hey there Andreas,

I have a bookmarklet just like OP's question. It works via javascript and runs fine when I click it in Firefox.

When trying to make it into an Apple script however, I keep getting some silly errors. Here's one example:
image
and this is the error (that it gives for the word JavaScript)
image

On MacOS Ventura (unfortunately) and using it with Firefox so shortcuts doesn't provide anything for Firefox.

What does the bookmarklet do? Just clips selected text and imports it into Obsidian. It works fine in the browser, but difficulty with BTT.

Thanks in advance.

@Andreas_Hegenberg
Other method I tried? The one below complies fine with no errors, but when activated (via a click of a touchbar button)...nothing happens.

(I shortened the script just to keep this comment short here in the forum, it's much longer but you get the gist of it I think)

set clipthis to "(function()%7Bjavascript%3A%20Promise.all(%5Bimport('https%3A%2F%2Funpkg.com%2Fturndown%406.0.0%3Fmodule')%2C%20import('https%3A%2F%2Funpkg.com%2F%40tehshrike%2Freadability%400.2.0')%2C%20%5D).then(async%20(%5B%7B%0A"

tell application "Firefox"
delay 0.1
execute
clipthis in front window
delay 5
end tell