Keyboard Launcher - a KDE style run dialog for windows

Post your working scripts, libraries and tools
roraparooza
Posts: 3
Joined: 18 Oct 2019, 08:27

Keyboard Launcher - a KDE style run dialog for windows

18 Oct 2019, 09:08

This is my first ahk script: my rendition of KDE Linux's Alt+F1 run dialog but ported for windows and with a few common sense improvements.

This is still a WIP and i'm still an ahk noob so i'd love some suggestions and feedback. Downloads at the bottom.

  • Extract both files to script directory and run
  • Hit Alt+F1 or Win+` to show the launcher
  • type "7z" to open 7z file manager
  • type "amz" to open amazon.com
  • type "amz rubber duckie" to search amazon.com for rubber duckies
  • type "cmd dir" to run "cmd /k dir"

Here's a portion of the CSV file:

Code: Select all

;shortcut1,shortcut2,defaultModeAction,webModeAction,searchAction
7z,7zip,7zFM,hxxp://www.7-zip.org,
ahk,ahkey,autohotkey,hxxp://www.autohotkey.com/download,hxxp://duckduckgo.com/?q={@}+site:hxxp://www.autohotkey.com/docs/
amz,amazon,,hxxp://www.amazon.com,hxxp://www.amazon.com/s?k={@}
ff,firefox,firefox,hxxp://www.firefox.com,hxxp://addons.mozilla.org/en-US/firefox/search/?q={@}
And here's the main code (the ahk file attachment is more liberally commented):

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;; Keyboard Launcher v1.0 ;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Notes ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; * the O button (Alt+O) opens the shorcuts csv file (to do: options gui)
; * the + button (Alt++) appends the typed string to the shortcuts csv file (todo: add/remove with browse gui)
; * the default browser is set as firefox. set "Browser := path\to\browser" below to specify a different one
; * shortcuts that contain a Space will be assumed to be a search (or a terminal command if it begins with "cmd ")
; * the context of the shortcut (the dropdownlist) can be toggled with the Tab key
; * one shortcut (e.g. ff) can have 3 contexts:
; ** "ff" opens the firefox application
; ** "ffTab" or "Tabff" opens the firefox.com website
; ** "ff ublock origin" searches addons.mozilla.org for "ublock origin"
; * context modes are not strict. if a context's action is not defined it will try a different action.
; * you can point shortcuts to scripts in the csv if you want to script an application or website launch
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Code ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#NoEnv
#SingleInstance force
#Persistent
; #NoTrayIcon
SetBatchLines -1
SendMode Input
SetWorkingDir, %A_ScriptDir%

global csvobj := {}
loop read, klauncher-shortcuts.csv
{
	line := A_index, csvobj[A_index] := []
	loop parse, A_loopreadline, csv
		csvobj[line].push(A_loopfield)
}

!F1::
#`::
if (WinExist("kL"))
{
	Gui, kL:Destroy
	Return
}
else
{
	Browser := "C:\Program Files\Mozilla Firefox\firefox.exe"
;	Browser := "C:\Program Files\Ungoogled Chromium\chrome.exe"
;	Browser := "C:\Program Files\Google Chrome\chrome.exe"
;	Browser := "C:\Program Files\Waterfox\waterfox.exe"
;	Browser := "C:\Program Files\Basilisk\basilisk.exe"
;	Browser := "C:\Program Files\Pale Moon\palemoon.exe"

	gosub showKLGui
	Return

	calcYPos() {
		Sysget, currentHeight, 1
		Return currentHeight - (0.12 * currentHeight)
	}

	matchSCAction(param,mode) {
		if (param[2] != "")
		{
			actionIsSearch := true
		}
		
		for i, value in csvobj
		{
			if ((value[1] = param[1]) || (value[2] = param[1]))
			{
				inputIsMatch := true
				if (mode = 1)
				{
					if (actionIsSearch = true)
					{
						if (value[5] = "")
						{
							Return value[4]
						}
						else
						{
							Return value[5]
						}
					}
					else
					{
						if (value[3] = "")
						{
							return value[4]
						}
						else
						{
							return value[3]
						}
					}
				}
				if (mode = 2)
				{
					if (actionIsSearch = true)
					{
						if (value[5] = "")
						{
							Return value[4]
						}
						else
						{
							Return value[5]
						}
					}
					else
					{
						if (value[4] = "")
						{
							return value[3]
						}
						else
						{
							Return value[4]
						}
					}
				}
			}
		}
	}

	showKLGui:
	kLYPos := calcYPos()
	resolution
	global actionIsSearch := false, global inputIsMatch := false, global isCMD := false
	kLModes := "default|Web"
	Gui, kL:New
	Gui, kL:+AlwaysOnTop +Owner -SysMenu -Caption +HWNDkLHWND
	Gui, kL:Color, 171717, 282828
	Gui, kL:Font, S11 CWhite, Verdana
	Gui, kL:Add, Edit, x88 y4 w380 h26 vkLEditText WantTab +HWNDkLEditTextHWND
	Gui, kL:Add, DDL, r3 x+-464 yp w80 cWhite AltSubmit vkLMode HWNDModeHWND Choose1, % kLModes
	Gui, kL:Add, Button, x+388 yp w26 h26, &+
	Gui, kL:Add, Button, x+3 yp w26 h26, &O
	Gui, kL:Add, Button, w0 h0 default, OK
	Gui, kL:Show, xCenter y%kLYPos% h34 w531, kL
	Winset, Style, -0xCF0000, A
	Return

	kLButtonOK:
		Gui +OwnDialogs
		Gui, kL:Submit
		actionIsCMD := % RegExMatch(kLEditText, "^cmd ")
		if (actionIsCMD)
		{
			GoSub runCMDCommand
		}
		else
		{
			kLAction := matchSCAction(strsplit(kLEditText, A_Space),kLMode)
			actionIsHttp := (RegExMatch(kLAction, "^http") || RegExMatch(kLAction, "^www"))
			if (inputIsMatch = true)
			{
				if (kLAction != "")
				{
					if (actionIsHttp = true)
					{
						if (actionIsSearch = true
						{ 
							stringSplit := % RegExMatch(kLEditText, "^([^ ]*)\ (.*)", searchString)
							searchString2 := % RegExReplace(kLAction, "\{\@\}", searchString2)
							searchString2 := % RegExReplace(searchString2, " ", "+")
							Run %Browser% %searchString2%
							Gui, kL:Destroy
						}
						else
						{
							if (RegExMatch(kLAction, "\{\@\}"))
							{
								kLAction := RegExReplace(kLAction, "\{\@\}", "")
							}
							Run %Browser% %kLAction%
							Gui, kL:Destroy
						}
					}
					else
					{
						if (actionIsSearch = true)
						{
							MsgBox Cannot search applications!`nTry launching a search script instead.
							Gosub, restartKLGui
						}
						else
						{
							Run %kLAction%
							Gui, kL:Destroy
						}
					}
				}
				else
				{
					MsgBox, 4,, %kLEditText% Action not defined!`nOpen Shortcuts File?
					IfMsgBox Yes
					{
						Gosub, kLButtonO
					}
					else
					{
						Gosub, restartKLGui
					}
				}
			}
			else
			{	
				MsgBox, Shortcut "%kLEditText%" not found!
				Gosub, restartKLGui
			}
		}
	Return
		
	kLGuiClose:
	kLGuiEscape:
		Gui, kL:Destroy
	Return
	
	kLButton+:
		Gui, kL:Submit
		if ((kLEditText != "") && (RegExMatch(kLEditText, "\,.*\,")))
		{
			FileAppend, %kLEditText%`n, web-shortcuts.csv
			MsgBox, Shortcut appended to CSV.
;			TrayTip, Success, Shortcut appended to CSV, 3 ; requires #NoTrayIcon to be disabled
			Gosub, restartKLGui
		}
		else
		{
			MsgBox, Shortcut is invalid!
;			TrayTip, Invalid Shortcut, ShortCut not saved!, 3 ; requires #NoTrayIcon to be disabled
			Gosub, restartKLGui
		}
	Return

	kLButtonO:
		Run, klauncher-shortcuts.csv
		Gui, kL:Destroy
	Return
	
	runCMDCommand:
		stringSplit := % RegExMatch(kLEditText, "^([^ ]*)\ (.*)", cmdString)
		Run cmd /k %cmdString2%
	Return

	restartKLGui:
		Gui, kL:Destroy
		Gosub showKLGui
	Return

	#IF WinActive("ahk_id" kLHWND) AND ControlIsFocused(kLEditTextHWND, kLHWND)
		:?*:`t::
		GuiControlGet, modeIndex,, % ModeHWND
		if (modeIndex = 2)
		{
			modeIndex :=1
		}
		else
		{
			modeIndex += 1
		}
		GuiControl, Choose, % ModeHWND, % modeIndex
		Return
	#IF

	ControlIsFocused(kLEditControlHWND, kLGuiHWND){
			ControlGetFocus, focusedControlClassID, % "ahk_id" kLGuiHWND
			ControlGet, focusedControlHWND, Hwnd,, % focusedControlClassID, % "ahk_id" kLGuiHWND
			Return (focusedControlHWND == kLEditControlHWND)
	}
}

Download:
klauncher-v1.0.zip
klauncher-v1.0
(5.98 KiB) Downloaded 23 times
User avatar
twitter SeX
Posts: 1
Joined: 19 Oct 2019, 00:59
Facebook: streetart
Google: streetart

Re: Keyboard Launcher - a KDE style run dialog for windows

21 Oct 2019, 08:50

nice idea

TIP:
browser + searchterm you can trigger without your initial ALT+F1 via an advanced bookmark also in opera

URL: http google.com /search?q=%s (NOTE: URL parts : // removed )
keyword: g
Browser AddressBar: g searchterm


would love your launcher always on top in my ~400pxl symbol* area @ the right screen edge,

→ my toolbar area 17'' laptop → 11m.de/tb+

maybee you can offer such a version mainly 4 widescreen laptops
Last edited by twitter SeX on 01 Nov 2019, 02:43, edited 2 times in total.
User avatar
Thoughtfu1Tux
Posts: 77
Joined: 31 May 2018, 23:26

Re: Keyboard Launcher - a KDE style run dialog for windows

22 Oct 2019, 18:58

Great stuff! I've been using a launcher very similar to this that i found on github last year (https://github.com/plul/Public-AutoHotKey-Scripts), i'm really looking forward to reading through your code more in depth and seeing if there's any features that i can port to my modded version of the above script. or maybe port my script to your script for my daily use.

Also, I absolutely love how much comments you've left in your code!! For someone with intermediate AHK skills, it's not always obvious what a block of code does, and all your comments will make my life so much easier. THANK YOU! :D :D
roraparooza
Posts: 3
Joined: 18 Oct 2019, 08:27

Re: Keyboard Launcher - a KDE style run dialog for windows

24 Oct 2019, 01:41

twitter SeX wrote:
21 Oct 2019, 08:50
nice idea

TIP:
browser + searchterm you can trigger without your initial ALT+F1 via an advanced bookmark also in opera

URL: http google.com /search?q=%s
keyword: g
Browser AddressBar: g searchterm


would love your launcher always on top in my ~400pxl symbol* area @ the right screen edge,

→ my toolbar area 17'' laptop → 11m.de/tb+

maybee you can offer such a version mainly 4 widescreen laptops

thanks for the feedback. currently working on a settings GUI where you can specify, among other things, custom sizes and locations or a location based on the current screen resolution (x% from top/bottom or from left/right). dragging is also in the works.

about the advanced bookmarks: need to find a better, more accessible solution for people who use opera as default tbh.

regarding always on top, wouldn't it interfere for e.g. when you're trying to watch videos?
roraparooza
Posts: 3
Joined: 18 Oct 2019, 08:27

Re: Keyboard Launcher - a KDE style run dialog for windows

24 Oct 2019, 01:56

Thoughtfu1Tux wrote:
22 Oct 2019, 18:58
Great stuff! I've been using a launcher very similar to this that i found on github last year (https://github.com/plul/Public-AutoHotKey-Scripts), i'm really looking forward to reading through your code more in depth and seeing if there's any features that i can port to my modded version of the above script. or maybe port my script to your script for my daily use.

Also, I absolutely love how much comments you've left in your code!! For someone with intermediate AHK skills, it's not always obvious what a block of code does, and all your comments will make my life so much easier. THANK YOU! :D :D
yeah the comments are something i've got in the habit of writing with beginners in mind (currently being and having been one myself a lot of the time). sadly it's also necessary rn as a few sections are an ugly, confusing mesh of if-else statements, at least until i figure out the correct syntax of ternary operators for multi line code.

it's still a work in progress and development is slower than i'd like due to insufficient info in the documentation (at least for an ahk beginner like me) but feel free to provide some suggestions on what you'd like to see incorporated.

one of the features i'm adding (more or less complete as of now) is media control via the launcher. e.g. "mb que boh" will find and play queen's bohemian rhapsody in musicbee if it's in your library, or simply "mb p" / "mb play" to start playing where you left, etc. also for foobar although it is highly layout dependant in its current form.

thanks for the link! i've always found it easier to learn from real code than from reference manuals.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 47 guests