Request for an update of TheBrain integration scripts (easy fix to enhance link stability)

Hi,

as a new user I’m discovering the value of Hook and would like to use it with TheBrain.

Based on some older forum threads I understand that it requires UI scripting but so far everything is working quite smoothly, with one exception:

Links generated with TheBrain contain the thought name, so if the thought name is changed the link changes as well. This leads to any hooked items no longer being displayed if the context window is invoked after renaming a thought.

I wanted to make you aware of what I believe might be a simple fix. TheBrain links have the following format:

brain://api.thebrain.com/hUZzP6CPhkWLdyC_w1559g/4Uc0YtxK8Fabmi3BhGmXCQ/RespectingRenewalCyclesForSustainableLongtermOutput

As you can see, in the end there is the thought name as described. However, actually the link also works if everything after the slash character is removed:

brain://api.thebrain.com/hUZzP6CPhkWLdyC_w1559g/4Uc0YtxK8Fabmi3BhGmXCQ/

If Hook could do this automatically, I’m quite certain it would fix the issue with broken links after renaming a thought.

Thanks for considering.

1 Like

Welcome to the Hook Productivity Forum , @AW2307 . Thanks for letting us know about this. We’ll have a look.

1 Like

I wanted to share an initial updated script I came up with that appears to be working reliably.

The script is based on the assumption that the stable part of the thought link, without the thought name at the end, seems to always have a length of 71 characters. So the script only includes characters 1-71 of the thought link.

I contacted TheBrain to verify that this is indeed the case at all times (awaiting response) but that’s what I’m seeing.

Probably the most future-proof solution would be a Regex that removes everything after the last slash, but that’s beyond my current abilities to implement and the current script does fulfill its purpose for the time being.

try
    tell application "System Events" to tell process "TheBrain"
        click menu item "Copy Local Thought URL" of menu 1 of menu bar item "Edit" of menu bar 1
        delay 0.05
        repeat 50 times -- poll clipboard for ~2.5 seconds
            try
                set theUrl_Unabbreviated to the clipboard
                if theUrl_Unabbreviated is not equal to "" then
                    exit repeat
                end if
            end try
            delay 0.05
        end repeat
        if theUrl_Unabbreviated is equal to "" then
            return
        end if
Set theURL to ((characters 1 thru 71 of theURL_Unabbreviated))
        click menu item "Copy As Text Outline" of menu 1 of menu bar item "Edit" of menu bar 1
        delay 0.05
        repeat 50 times -- poll clipboard for ~2.5 seconds
            try
                set theTitle to the clipboard
                if theTitle is not equal to "" then
                    exit repeat
                end if
            end try
            delay 0.05
        end repeat
    end tell
    return "[" & theTitle & "](" & theUrl & ")"
    
on error msg
    display dialog msg
end try
1 Like

Just a quick update regarding the link format: TheBrain support confirmed that the stable part of the thought links always has exactly 71 characters and there are currently no plans to change this.

1 Like

Thanks for doing all this investigation and development, @AW2307.

I’ve moved this out of the ‘bug report’ category because Hook is meeting the basic requirement. It’s up to the third-party app developer to honour their links. A lot software (SaaS and apps) ensures that links are robust even if items are renamed (for instance DEVONthink and OmniFocus; Discourse SaaS also handles renamed topics). Hook often goes above and beyond that when it has additional reliable information, which you have provided (thank you again). In this case, we intend to improve the integration script accordingly.

Hopefully The Brain’s developer will provide some AppleScript for getting the URL so that we don’t need to rely on UI scripting, and so that this can more easily work across different languages.

1 Like

Fully agree. The issue here was not Hook but the fact that links in TheBrain change to reflect the thought name.

In fairness though, it’s not that TheBrain is not “honoring their links”. Even if the dynamic part of the link (thought name) has changed, the link is robust based on the stable part. It still opens the correct item even if the dynamic part doesn’t reflect the current thought name.
So I would say that there is simply an incompatibility between this design decision on the side of TheBrain and Hook using the linked URL in its entirety to display linked items (which of course makes sense). Here, since the dynamic part has changed Hook can’t find the linked items.

And yes, if TheBrain simply offered basic AppleScript support for getting and updating the URL this wouldn’t be an issue. As someone else said, TheBrain is sometimes not a good “MacOS citizen” because they prioritize feature parity on Windows / Mac.

1 Like

Thank you for clarifying, @AW2307 . I’m glad to see their links are robust.

Here is the revised script which will take out thought name. Could you please give it a try?

Summary
try
	tell application "System Events" to tell process "TheBrain"
		click menu item "Copy Local Thought URL" of menu 1 of menu bar item "Edit" of menu bar 1
		delay 0.05
		repeat 50 times -- poll clipboard for ~2.5 seconds
			try
				set theUrl to the clipboard
				if theUrl is not equal to "" then
					exit repeat
				end if
			end try
			delay 0.05
		end repeat
		if theUrl is equal to "" then
			return
		end if
		
		set AppleScript's text item delimiters to "/"
		set theResults to every text item of theUrl
		set theResults to reverse of rest of reverse of theResults
		set theUrl to theResults as string
		click menu item "Copy As Text Outline" of menu 1 of menu bar item "Edit" of menu bar 1
		delay 0.05
		repeat 50 times -- poll clipboard for ~2.5 seconds
			try
				set theTitle to the clipboard
				if theTitle is not equal to "" then
					exit repeat
				end if
			end try
			delay 0.05
		end repeat
	end tell
	return "[" & theTitle & "](" & theUrl & ")"
	
on error msg
	display dialog msg
end try

Much appreciated. Happy to test the script.
Could you perhaps provide an email address, to which I can send any observations?

Thank you very much!

You can send the email to support@cogsciapps.com .

Thank you very much! We’ve published the updated TheBrain integration script. Great to have more robust processing of the The Brain info.

1 Like

While the address gets fetched perfectly in the current version of the script (thanks again for offering the updated version), I found there is some trouble getting the title: 75% of the time it works correctly, but often it doesn’t and only displays the link instead of the title.

The joys of UI scripting :sweat_smile:

Could you please change the delay time and repeat times for getting title in the Get URL script and see if it helps? Thank you

This is the code for getting title:
click menu item “Copy As Text Outline” of menu 1 of menu bar item “Edit” of menu bar 1
delay 0.05
repeat 50 times -

From what I’m seeing, the code you posted is already contained in the current version of the integration scripts. And using the current scripts, I’m encountering the behavior described above, where the title is not fetched consistently…

Sorry for the confusion.

What I meant is you can change the number after delay, e.g. current it is
delay 0.05

You can change it a different number, such as 0.5 or 1:
delay 0.5

Also you can change the number after repeat to a different number, such as 60, 70, etc.
repeat 50 times

Thanks for clarifying, I will give this a shot.

1 Like