Fun with AHK

Anyone successful in getting the main notetaking window activated using AHK? The open vault window is the one that’s getting brought up by AHK every time I invoke WinRestore.

; Launch Obsidian Ctrl + Shift + (with consideration to multiple computers)

^+`::

PathToApp = C:\Users%A_UserName%\AppData\Local\Obsidian ; add this workaround because FileExist does not evaluate %A_xxx% variables

Process, Exist, “Obsidian.exe” ; Sets ErrorLevel variable to processID

if(ErrorLevel)

{

if(WinActive("ahk_exe Obsidian.exe"))

{

    ; WinMinimize, ahk_exe "Obsidian.exe"

    WinMinimize, Title, "Obsidian.exe"

}

else

{

    ;WinActivate, ahk_exe "Obsidian.exe"

    ;WinRestore, ahk_exe "Obsidian.exe"

    WinActivate, ahk_exe "Obsidian.exe"

    WinRestore, ahk_exe "Obsidian.exe"

}

}

else

{

if(FileExist(PathToApp))

{

    Run "C:\Users\%A_UserName%\AppData\Local\Obsidian\Obsidian.exe"

}

else

{

    MsgBox Check if Obsidian is installed.

}

}

return
`

I have a script for activating a specific vault, you can check them out, I am not very good with AHK, I can’t at this point explain anything else.

; This will activate obsidian, if not already open, it will open it & run the command for quick add plugin.
; This is one hotkey which I am able to get to work every time in any location whatsoever
^q::
WinActivate, i) Obsidian
Run “obsidian://advanced-uri?vault=my_vault_name_here&commandname=QuickAdd: Run QuickAdd”
return

; It will activate it & run the command for quick capture to daily notes.
; Whenever Chrome is active, this doesn’t seem to work all the time.
^Capslock::
Send, !o
Send, ^+d
return

; It will activate it & run the command for quick capture to workbench
; Whenever Chrome is active, this doesn’t seem to work all the time.
^!x::
Send, !o
Send, ^+x
return

; This will activate the obsidian window & if not open it will open obsidian & activate it
!o::
WinActivate, i) Obsidian
Run “obsidian://advanced-uri?vault=my_vault_name_here”
return

2 Likes

This is great. Thanks so much @Nihit !

1 Like

I like that this thread exists! I’d like to see more

I made a script to add and substract from a selected YYYY-MM-DD date, but if I keep the key down it can get unstable and break, any idea how to make it more stable? I what should I call at the start of the code to help with that?

#NoEnv                      ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn                     ; Enable warnings to assist with detecting common errors.
#SingleInstance FORCE       ; Skip invocation dialog box and silently replace previously executing instance of this script.
SendMode Input              ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
FileEncoding , UTF-8

Capslock & =::
send, ^c
InputVar := Clipboard
send % AddADay(InputVar)
sleep 50
send, {ShiftDown}{left}{left}{left}{left}{left}{left}{left}{left}{left}{left}{ShiftUp}
return

Capslock & -::
send, ^c
InputVar := Clipboard
send % SubADay(InputVar)
sleep 50
send, {ShiftDown}{left}{left}{left}{left}{left}{left}{left}{left}{left}{left}{ShiftUp}
return


AddADay(startdate)
{
StringSplit, splitdate, startdate, -
ymdhms:=splitdate1 splitdate2 splitdate3 "000000"
EnvAdd, ymdhms, 1, days
FormatTime, newdate, %ymdhms%, yyyy-MM-dd
return newdate
}

SubADay(startdate)
{
StringSplit, splitdate, startdate, -
ymdhms:=splitdate1 splitdate2 splitdate3 "000000"
EnvAdd, ymdhms, -1, days
FormatTime, newdate, %ymdhms%, yyyy-MM-dd
return newdate
}