2

I'm trying to create an .ahk script that will make life easier with Notion. My colleagues toss Notion links everywhere, and I'd like a script to force these links to open in the app and not in the browser.

I know that I need to somehow automatically alter the link from 'https' to 'notion', but my script doesn't work like I expected.

Can anyone help me avoid this extra step? Thanks in advance. Code below:

!LButton::
Send, ^c
Clipboard := StrReplace(Clipboard, "https", "notion", 1)
SendInput, %Clipboard%
return

Edit: I can add that the second part of the code works as I want if the link is already in the clipboard. I seem not to be able to correctly automate copying the link to the clipboard when I run this hot key script.

ancepsinfans
  • 123
  • 1
  • 5

1 Answers1

1

I don't know what Notion is, so I'll have a bit of trouble giving the best answer, but this advice will hopefully fix your problem:

Firstly, it's good to use ClipWait(docs) when sending ctrl+c and then working with the copied stuff.

Then you seem to be missing one arguments from the StrReplace()(docs) function. You're specifying 1 to the OutputVarCount parameter.

Then you should specify the text send mode for your send command to avoid problem with ^+!#{} characters.
Or a bit better, you could set the text to the clipboard and send ctrl+v.
Or even better, launch the app and pass the link in as an argument, assuming it supports that.

Code:

!LButton::
    Clipboard := ""
    SendInput, ^c
    ClipWait
    Clipboard := StrReplace(Clipboard, "https", "notion", , 1)
    SendInput, % "{Text}" Clipboard         ;send as input
    SendInput, ^v                           ;or paste
    Run, % "notion.exe """ Clipboard """"   ;or pass in as commandline argument
return

EDIT:
Another approach

!LButton::
    Clipboard := ""
    Click, Right    ;open right click menu
    Sleep, 50       ;wait a bit so the menu opens
    SendInput, e    ;shortcut for "copy to clipboard"
    ClipWait
    Clipboard := StrReplace(Clipboard, "https", "notion", , 1)
    MsgBox, % Clipboard
return
0x464e
  • 5,948
  • 1
  • 12
  • 17
  • Interesting. Thanks for the corrections, but this one also doesn't work. The problem seems to lie in the click-to-clipboard. !LButton activates the hotkey, but doesn't initialize the act of copying the hyperlink to the clipboard. – ancepsinfans May 30 '21 at 07:27
  • 1
    [It works as expected](https://giant.gfycat.com/DependentCloudyDeermouse.mp4), ctrl+c copies the selected text to the clipboard. Are you trying to do something else? You mention a hyperlink? Do you want to hover over a link and then somehow get that link? That would be much more involved. – 0x464e May 30 '21 at 19:24
  • Ideally, yes, that's what I had in mind. Basically changing the behavior of a click from "open in browser" to "open in app" via "click, copy, alter, run". I guess it's a pipe dream? – ancepsinfans May 31 '21 at 12:00
  • Sounds like you'd either want to hook into your browser with AHK and process the clicks to links. Could maybe be doable with "Chrome.ahk". But really it sounds like this should be a browser side script. Anyway, I added an other approach that should work for copying the hyperlink under your cursor to clipboard. – 0x464e Jun 01 '21 at 04:00
  • Thanks for your help. I also thought about something browser-side, but the problem there is that we use several different channels of communication, and I run most of them inside desktop applications rather than in the browser. Your second version might be the thing I'm looking for. Thank you again. – ancepsinfans Jun 02 '21 at 03:01