atom-launcher/Atom-Launcher.ahk

384 lines
10 KiB
AutoHotkey
Raw Normal View History

2023-08-18 22:32:02 -04:00
;---ENVIRONMENT---------------------------------------------------------------------
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn ; Enable warnings to assist with detecting common errors.
;DetectHiddenWindows, On
#SingleInstance, Force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode, ToolTip, Screen
CoordMode, Mouse, Screen
; #NoTrayIcon
Menu, Tray, Icon, %A_scriptDir%\Graphics\Atom.ico
AtomIconFP = %A_scriptDir%/Graphics/Atom.png
;---Notes/Extra Info/#Includes------------------------------------------------------
; Original Script: https://github.com/plul/Public-AutoHotKey-Scripts
;---VARIABLES-----------------------------------------------------------------------
Script_Name = Atom Launcher
2023-08-18 22:43:31 -04:00
A_ICODir = %A_scriptDir%\Graphics
2023-08-18 22:32:02 -04:00
; Read User Settings from .ini file
; IniRead, SaveUsageStats, %A_scriptdir%\Lib\Settings.ini, AtomSearch, SaveUsageStats
2023-08-19 22:22:29 -04:00
SaveUsageStats=0
2023-08-18 22:32:02 -04:00
ScreenWidth := A_ScreenWidth
ScreenDPI := A_ScreenDPI
GUIWidth := Round(ScreenWidth/3)
; GUI COLORS
cBackground := "c" . "1d1f21"
cCurrentLine := "c" . "282a2e"
cSelection := "c" . "373b41"
cForeground := "c" . "c5c8c6"
cComment := "c" . "969896"
cRed := "c" . "cc6666"
cOrange := "c" . "de935f"
cYellow := "c" . "f0c674"
cGreen := "c" . "b5bd68"
cAqua := "c" . "8abeb7"
cBlue := "c" . "81a2be"
cPurple := "c" . "b294bb"
MarginSpace := 15 ; Margin size to be used in the gui
; Set Global Variables for ease of use and accessibility
global SaveUsageStats ; usage stats setting
global search_url
global gui_state
global gui_search_title
global ExpandedMenu
; -E0x200 removes border around Edit controls
; gui_control_options := "x57" . "xm" . GUIWidth . cForeground . " -E0x200"
Guiwidth = 550
Icon_Size = 125
; Icon_XPosition := Guiwidth / 2 - (Icon_Size / 2) + 10
Icon_XPosition := 10
gui_control_options := "x+10 y55 w" . Guiwidth . cForeground . " -E0x200"
; gui_control_options = x+%MarginSpace% xm w%guiwidth% %cForeground% -E0x200
; Initialize variable to keep track of the state of the GUI
gui_state = closed
;---MAIN SCRIPT---------------------------------------------------------------------
;---LAUNCH GUI----------------------------------------------------------------------------
; !Space:: ; Alt + Space
^E:: ; CapsLockeE
; ^Q::
gui_spawn:
if gui_state != closed
{
; If the GUI is already open, close it.
gui_destroy()
return
}
; Otherwise create the menu
gui_state = main
Gui, Add, Picture, vIcon x%Icon_XPosition% w%Icon_Size% h%Icon_Size% +Center ,%AtomIconFP%
Gui, Margin, 10, 10 ;Default was: 16, 16
Gui, Color, 1d1f21, 282a2e ; Background and edit box color
Gui, Font, cWhite ; Color of typed in letters
Gui, +AlwaysOnTop -SysMenu +ToolWindow -caption +Border
Gui, Font, s30, Segoe UI
Gui, Add, Text,center x145 y10 vgui_main_title w%Guiwidth% vGUITitle +Center, %Script_Name% ; Uncomment if you want a GUI title
; Gui, Add, Text,center x80 y80 vgui_main_title, Atom ; Uncomment if you want a GUI title
; Gui, Add, Text,center x300 y100 vgui_main_title2, Search ; Uncomment if you want a GUI title
Gui, Font, s20, Segoe UI
;gui, Add, Text,Center x250,Atom Search
2023-08-19 22:22:29 -04:00
Gui, Add, Edit, x145 y80 w%Guiwidth% %cForeground% -E0x200 +Center vAtomSearchTerm gFindus
2023-08-18 22:32:02 -04:00
;Gui, Add, Picture, x+1 w45 h45,RMSearch_R.ico
Gui, Show,y25, %Script_Name%
if(A_ThisHotkey = "^q"){
ControlSend, Edit1, t, %Script_Name%
ControlSend, Edit1, {Space}, %Script_Name%
}
return
; GUI FUNCTIONS AND SUBROUTINES
;------------------------------------------------
; Automatically triggered on Escape key:
GuiEscape:
2023-08-19 22:22:29 -04:00
AtomSearchTerm :=
2023-08-18 22:32:02 -04:00
CustomTool :=
gui_destroy()
return
; The callback function when the text changes in the input field.
Findus:
Gui, Submit, NoHide
2023-08-19 22:22:29 -04:00
; Msgbox % "AtomSearchTerm: " AtomSearchTerm
2023-08-18 22:32:02 -04:00
; msgbox, herere
2023-08-19 22:22:29 -04:00
if AtomSearchTerm = nothinghere%A_Space% ; Search Amazon
2023-08-18 22:32:02 -04:00
{
; do nothing for this one so the #includes can be organized in whichever order the user wants
}
#Include %A_ScriptDir%\Modules\Atom-Programs.ahk
#Include %A_ScriptDir%\Modules\Atom-Folders.ahk
#Include %A_ScriptDir%\Modules\Atom-Web-Searches.ahk
#Include %A_ScriptDir%\Modules\Atom-Misc-Commands.ahk
return
;
; gui_destroy: Destroy the GUI after use.
; Skips the gentle method of activating a window and goes straight to the forceful method.
#WinActivateForce
gui_destroy() {
gui_state = closed
; Forget search title variable so the next search does not re-use it
; in case the next search does not set its own:
gui_search_title =
; Clear the tooltip
ToolTip
; Hide GUI
Gui, Destroy
; Bring focus back to whatever window was active before Atom got activated
WinActivate
}
gui_change_title(message,color = "") {
; If parameter color is omitted, the message is assumed to be an error
; message, and given the color red.
If color =
{
global cRed
color := cRed
}
GuiControl,, gui_main_title, %message%
Gui, Font, s11 %color%
GuiControl, Font, gui_main_title
Gui, Font, s10 cffffff ; reset
}
;-------------------------------------------------------------------------------
; SEARCH ENGINES
;-------------------------------------------------------------------------------
;
; Expands the GUI search display to show whatever specific search tool the user is going to search with
gui_search_add_elements:
GuiControl,, Icon, %gui_search_icon%
GuiControl, Text, GUITitle, %gui_search_title%
Gui, Add, Edit, x145 y80 w%Guiwidth% -E0x200 %cYellow% +Center vgui_SearchEdit -WantReturn
Gui, Add, Button, x-10 y-10 w1 h1 +default gSearchEnter ; hidden button gets activated when user hits [Enter]
2023-08-19 22:22:29 -04:00
; GuiControl,, AtomSearchTerm,
GuiControl, Disable, AtomSearchTerm
2023-08-18 22:32:02 -04:00
; Gui, Show, AutoSize
; msgbox
return
GuiSearch(url) {
search_url := url
gui_state := "search"
Gosub, gui_search_add_elements ; Add elements for search field
}
SearchEnter:
Gui, Submit
gui_destroy() ; destroy the gui and release all variables for next run
; encode and replace characters to browser format
QuerySafeText := uriEncode(gui_SearchEdit)
; convert typed string into search engine URL using the usercommands.ahk commands
FinalURl := StrReplace(search_url, "REPLACEME", QuerySafeText)
; Msgbox % "CustomTool: " CustomTool
; execute the file with whatever the default program for the extension is
if(CustomTool = ""){
run, %FinalURl%
}
; otherwise use custom tool
else if(CustomTool != "")
{
; Msgbox % "CustomTool: " CustomTool
; Open Command Prompt
if(CustomTool = "CMD_KeepOpen"){
; msgbox % FinalURl
Run, %ComSpec% /k %FinalURl%
}
; OTHERWISE IFs
; Otherwise run using custom tool as a filepath
else if(CustomTool = "OpenFile"){
run, "%gui_SearchEdit%"
}
else if(CustomTool = "JoplinNote"){
run, "%gui_SearchEdit%"
}
else if(CustomTool = "Sublime"){
run, "%A_sublimefilepath%" "%gui_SearchEdit%"
}
else if(CustomTool = "GIMP"){
run, "%A_gimpfilepath% " "%gui_SearchEdit%"
}
else if(CustomTool = "RamMultiplier"){
RAM := 1024 * GUI_SearchEdit
Clipboard := RAM
}
else if(CustomTool = "AHKTimeMultiplier"){
if(InStr(gui_SearchEdit, "H")){
gui_SearchEdit := StrReplace(gui_SearchEdit, "H", "")
MSTime := gui_SearchEdit*3600000
MSTime := MSTime . " `; " . gui_SearchEdit . " Hours"
Clipboard := MSTime
}
if(InStr(gui_SearchEdit, "M")){
gui_SearchEdit := StrReplace(gui_SearchEdit, "M", "")
MSTime := gui_SearchEdit*60000
MSTime := MSTime . " `; " . gui_SearchEdit . " Minutes"
Clipboard := MSTime
}
}
else, {
; msgbox here
; Msgbox % "customtool: " customtool . "FinalURl" . FinalURl
; Clipboard := customtool . " " . FinalURl
run, %CustomTool% %FinalURl%
}
}
; Msgbox % "CustomTool: " CustomTool
; Msgbox % CustomTool = "CMD_KeepOpen"
; reset custom tool if one was used
CustomTool =
return
;
;
; Parse out User Commands and display them in sublime
gui_commandlibrary:
CoordMode, Tooltip, Screen ; To make sure the tooltip coordinates is displayed according to the screen and not active window
Gui, 2:Font,s10, Lucida Console
Gui, 2:Add, Text, HwndhwndStatic
tooltiptext =
maxpadding = 0
StringCaseSense, Off ; Matching to both if/If in the IfInString command below
Loop, read, %A_ScriptDir%/AtomUserCommands.ahk
{
2023-08-19 22:22:29 -04:00
; search for the string If AtomSearchTerm =, but search for each word individually because spacing between words might not be consistent. (might be improved with regex)
2023-08-18 22:32:02 -04:00
If Substr(A_LoopReadLine, 1, 1) != ";" ; Do not display commented commands
{
If A_LoopReadLine contains if
{
2023-08-19 22:22:29 -04:00
IfInString, A_LoopReadLine, AtomSearchTerm
2023-08-18 22:32:02 -04:00
IfInString, A_LoopReadLine, =
{
StringGetPos, setpos, A_LoopReadLine,=
StringTrimLeft, trimmed, A_LoopReadLine, setpos+1 ; trim everything that comes before the = sign
StringReplace, trimmed, trimmed, `%A_Space`%,{space}, All
tooltiptext .= trimmed
tooltiptext .= "`n"
; The following is used to correct padding:
StringGetPos, commentpos, trimmed,`;
if (maxpadding < commentpos)
maxpadding := commentpos
}
}
}
}
tooltiptextpadded =
Loop, Parse, tooltiptext,`n
{
line = %A_LoopField%
StringGetPos, commentpos, line, `;
spaces_to_insert := maxpadding - commentpos
Loop, %spaces_to_insert%
{
StringReplace, line, line,`;,%A_Space%`;
}
tooltiptextpadded .= line
tooltiptextpadded .= "`n"
}
Sort, tooltiptextpadded
FormatTime, TodayDate , YYYYMMDDHH24MISS, yyyyMMdd_hhmmss
Append_Filepath = %A_scriptDir%\Lib\Atom_Commands.txt
FileAppend, %tooltiptextpadded%,%Append_Filepath%
; Msgbox % "tooltiptextpadded: " tooltiptextpadded
run, %Append_Filepath%
;ToolTip %tooltiptextpadded%, 3, 3, 1
return
;---FUNCTIONS-----------------------------------------------------------------------
; A function to escape characters like & for use in URLs.
uriEncode(str) {
f = %A_FormatInteger%
SetFormat, Integer, Hex
If RegExMatch(str, "^\w+:/{0,2}", pr)
StringTrimLeft, str, str, StrLen(pr)
StringReplace, str, str, `%, `%25, All
Loop
If RegExMatch(str, "i)[^\w\.~%/:]", char)
StringReplace, str, str, %char%, % "%" . SubStr(Asc(char),3), All
Else Break
SetFormat, Integer, %f%
Return, pr . str
}
; Export usage statistics to .txt file within \Lib\ folder
StatExport(SearchTitle) {
if(!SaveUsageStats) ; quit early if usage stats are disabled
Return
FormatTime, CurrentTime,,yyyyMMdd_hhmmss
StatsFile = %A_Scriptdir%\Lib\UsageStats_%A_ComputerName%.txt
FileAppend, `n%CurrentTime% - %SearchTitle% - %A_ComputerName%,%StatsFile%
}