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:
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:
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
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.
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.
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.
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
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.
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…