Skip to content

Instantly share code, notes, and snippets.

@Far-Se
Last active May 28, 2022 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Far-Se/b112a812b6b52832cc8290cb2dd3ddc9 to your computer and use it in GitHub Desktop.
Save Far-Se/b112a812b6b52832cc8290cb2dd3ddc9 to your computer and use it in GitHub Desktop.
Windows Powertoys Run Shortcuts Alias Plugin

Windows PowerToys Run Shortcuts plugin

The issue:

Shell run only runs .exe processes , ignores .cmd and .bat files. With this tiny .ahk script you can compile a exe that runs aliases from a .json file. For example if you want to open a link to a documentation for a framework that you use, you can add it in commands.json and then open PowerToys Run and type s react

Installation:

  1. If you dont want to install AHK, you can download the exe directly from HERE But the browser will show you a warning.
  2. Install AHK
  3. Find/make a folder that you will add in System Environment Variables under Path, for example C:\ProgramData\alias\
  4. Make a copy of JSON.ahk
  5. Make a file with a short name like s.ahk and paste the code below
  6. Make a file called commands.json paste the code below and add your shortcuts/aliases there
  7. Compile s.ahk, AHK automatically adds Compile in Context Menu for .ahk files

Now you are set, test your aliases/shortcuts. Your can edit your shortcuts by typing s edit and Notepad should open with your commands.json

Trigger hotkeys

You can send keys if you type Send keys, ex "t": "Send {Media_Play_Pause}" then s t to toggle music. Here is a list of all the keys

Passing Arguments

{args} for all the text after alias

{argsURL} replaces space with +

{argsURI} Encodes text to URI

Pro tip:

Open PowerToys Settings, go to PowerToys Run press expand on Shell and click Include in global result so you dont have to type > each single time

{
"edit": "notepad .\\commands.json",
"win32": "start https://pub.dev/documentation/win32/latest/",
"n": "Send {Media_Next}",
"pr": "Send {Media_Prev}",
"p": "Send {Media_Play_Pause}",
"g": "start \"https://github.com/search?q={argsURI}\""
}
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#Include JSON.ahk
UriEncode(Uri, Enc = "UTF-8")
{
StrPutVar(Uri, Var, Enc)
f := A_FormatInteger
SetFormat, IntegerFast, H
Loop
{
Code := NumGet(Var, A_Index - 1, "UChar")
If (!Code)
Break
If (Code >= 0x30 && Code <= 0x39 ; 0-9
|| Code >= 0x41 && Code <= 0x5A ; A-Z
|| Code >= 0x61 && Code <= 0x7A) ; a-z
Res .= Chr(Code)
Else
Res .= "%" . SubStr(Code + 0x100, -1)
}
SetFormat, IntegerFast, %f%
Return, Res
}
StrPutVar(Str, ByRef Var, Enc = "")
{
Len := StrPut(Str, Enc) * (Enc = "UTF-16" || Enc = "CP1200" ? 2 : 1)
VarSetCapacity(Var, Len, 0)
Return, StrPut(Str, &Var, Enc)
}
FileRead jsonString, commands.json
Data := JSON.Load(jsonString)
StringLower, parameter, % A_Args[1]
for n, param in A_Args
{
if(n > 1)
customArgs = %customArgs% %param%
}
if(Data.haskey(parameter))
{
cmdx := Data[parameter]
If(InStr(cmdx,"{args}") > 0)
StringReplace, cmdx, cmdx, {args}, %customArgs%, All
If(InStr(cmdx,"{argsURL}") > 0)
{
newArgs := StrReplace(customArgs, A_Space, "+")
StringReplace, cmdx, cmdx, {argsURL}, %newArgs%, All
}
If(InStr(cmdx,"{argsURI}") > 0)
{
newArgs := UriEncode(customArgs)
StringReplace, cmdx, cmdx, {argsURI}, %newArgs%, All
}
If(InStr(cmdx,"Send") == 1)
{
StringReplace, cmdx, cmdx, Send, , All
Send %cmdx%
}else {
; Run,%comspec% /c %cmdx%,,hide
Run, powershell -Command "%cmdx%",,hide
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment