Autohotkey -- click link, alter link, launch link

Viewed 354

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.

1 Answers

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
Related