Skip to content

Instantly share code, notes, and snippets.

@thalesmello
Last active July 22, 2022 17:51
Show Gist options
  • Save thalesmello/5594967 to your computer and use it in GitHub Desktop.
Save thalesmello/5594967 to your computer and use it in GitHub Desktop.
This is the Autohotkey script I use on a daily basis. I use it to play/pause my webplayers, move and resize my windows, add new tasks to Any.do and toggle my windows in fullscreen mode.
#NoEnv
#Include %A_ScriptDir%
#Include VA.ahk
#SingleInstance force
SetTimer,UPDATEDSCRIPT,1000
OnExit, EXITSCRIPT
HomePath = C:\Users\ThalesErnesto
; Media control
!1::ActionPlayer("Pandora:{Space}|Grooveshark:{Space}|Earbits:{click 577,67}")
!2::ActionPlayer("Pandora:{Right}|Grooveshark:^{Right}|Earbits:{click 621,70}", "Media_Next")
!3::VA_SetMasterVolume(VA_GetMasterVolume()-5)
!4::VA_SetMasterVolume(VA_GetMasterVolume()+5)
; Specific task shortcuts
^!t::OpenTerminal()
^!+t::OpenTerminal(false)
!t::CreateTask() ;Alt+t will activate the shortcut
!f::ToggleWindowFullscreen()
!a::Run "C:\Program Files\Sublime Text 2\sublime_text.exe" "%HomePath%\Dropbox\Scripts\Autohotkey\ThalesMacros.ahk"
#LAlt::CommandWindow("Move", "LAlt")
#LCtrl::CommandWindow("Resize", "LCtrl")
; Shortcuts to folders
!q::Run %HomePath%\Dropbox\
!w::Run %HomePath%
!e::Run %HomePath%\Dropbox\ITA
; Control cursor
!/::SendInput {Esc}
!,::SendInput ^{PgUp}
!.::SendInput ^{PgDn}
!k::SendInput {Up}
!j::SendInput {Down}
!u::SendInput {PgUp}
!n::SendInput {PgDn}
!h::SendInput {Home}
!l::SendInput {End}
^+v::SendInput ^v{Enter}
; Functions
VarExist(_var=""){
local varAddr, nonExistentAddr
If (_var = "")
Return "NO"
varAddr := & %_var%
nonExistentAddr := & $I_Doubt?Such@Variable[Would]Exist#
If (varAddr = nonExistentAddr)
Return "NO"
Else If (%_var% = "")
Return "EMPTY"
Return "YES"
}
GetActiveWindowId(){
WinGet, active_id, ID, A
return active_id
}
Signal(x){
return x = 0 ? 0 : (x > 0 ? 1 : -1)
}
CreateTask(){
SetTitleMatchMode, 2 ;Title contains word
IfWinNotExist, Any.DO ;Verifies if Any.do window doesn't exists
{
Send #2 ;Inputs Windows + 2 to start Any.do shortcut (in the second position)
Sleep 3000 ;Waits 6 seconds for Any.do to load
IfWinNotExist Any.DO ;Certifies Any.do loaded properly
{
MsgBox, Any.DO Window doens't exist. Please verify script.
return
}
}
WinActivate, Any.DO ;Brings Any.do to the foreground
CoordMode, Mouse, Relative ;Setting relative coordinate mode for the mouse
WinGetPos, , , width ;Gets the width of the window
x1 := width - 12
x2 := width - 34
Click %x1%, 94, 10
Click %x2%, 113 ;Clicks the + button to input a new task
return
}
ActivatePlayerWindow(playerName, original_id, hiddenWindow = true){
if(hiddenWindow){
WinSet, AlwaysOnTop, on, ahk_id %original_id%
}
WinActivate, %playerName%
}
DeactivatePlayerWindow(original_id, hiddenWindow = true){
WinMinimize, A ; Minimizes the player, which is the currently active window
if(hiddenWindow){
WinActivate, ahk_id %original_id%
WinSet, AlwaysOnTop, off, ahk_id %original_id%
}
}
ItCanHideWindow(action){
IfInString, action, click
hiddenWindow := false
else
hiddenWindow := true
return hiddenWindow
}
SendCmd(cmd){
Send %cmd%
}
ExecuteActionKeepMousePosition(action){
CoordMode, Mouse, Screen
MouseGetPos, mouse_x0, mouse_y0
CoordMode, Mouse, Relative
Send %action%
CoordMode, Mouse, Screen
MouseGetPos, mouse_x1, mouse_y1
if(mouse_x1 != mouse_x0 || mouse_y1 != mouse_x0){
MouseMove, mouse_x0, mouse_y0
}
}
WebPlayerAction(playerName, action, argument = ""){
SetTitleMatchMode, 2 ;Title contains word
IfWinExist, %playerName%
{
original_id := GetActiveWindowId()
hiddenWindow := ItCanHideWindow(action)
ActivatePlayerWindow(playerName, original_id, hiddenWindow)
if(argument != ""){
%action%(`"%argument%`")
}
else{
ExecuteActionKeepMousePosition(action)
}
DeactivatePlayerWindow(original_id, hiddenWindow)
return true
}
Else
{
return false
}
}
ActionPlayer(command_pipeline, default_command = "Media_Play_Pause"){
StringSplit, command, command_pipeline, |
Loop, %command0%
{
StringSplit par, command%A_Index%, :
if(par0 == 3){
resp := WebPlayerAction(par1, par2, par3)
}
else if(par0 == 2){
resp := WebPlayerAction(par1, par2)
}
else{
MsgBox, Syntax error in ActionPlayer
}
if(resp){
return
}
}
Send {%default_command%}
}
ToggleWindowFullscreen(){
static
current := GetActiveWindowId()
WinGet, current_minmax, MinMax, ahk_id %current%
if(current_minmax = 0)
{
SetTitleMatchMode, 2
current := GetActiveWindowId()
WinGet Style, Style, ahk_id %current%
if(Style & 0xC40000) {
if(last_full <> current_id)
{
WinGet, last_style, Style, ahk_id %last_full%
if(last_style & 0xC40000 = 0)
{
WinSet, Style, +0xC40000, ahk_id %last_full%
WinMove,ahk_id %last_full%,,%X%,%Y%,%Width%,%Height%
}
}
WinGetPos, X, Y, Width, Height, A
WinSet, Style, -0xC40000, A
WinMove,A,,0,0,A_ScreenWidth,A_ScreenHeight
last_full = %current%
} else {
WinSet, Style, +0xC40000, A
WinMove,A,,%X%,%Y%,%Width%,%Height%
last_full =
}
}
return
}
IsExplorerActive(){
IfWinActive, ahk_class CabinetWClass
{
return true
}
else {
return false
}
}
ExtractPath(){
global HomePath
DetectHiddenText, Off
WinGetText, win_text, ahk_class CabinetWClass
path_found := RegExMatch(win_text, "(\w:(\\[^\\/:\*<>\|\r\n]+)+)" , path_a)
path := path_found ? path_a1 : HomePath
return path
}
OpenTerminal(shell = true){
global HomePath
ConEmuPath = C:\Program Files (x86)\ConEmu\ConEmu.exe
ShellTask = sh --login -i
CmdTask = cmd /k `"%HomePath%\Dropbox\Scripts\WinBashProfile\load.bat`"
Task := shell ? ShellTask : CmdTask
Path := isExplorerActive() ? ExtractPath() : HomePath
Command = %ConEmuPath% /Single /Dir `"%Path%`" /cmd %Task%
Run, %Command%
return
}
CommandWindow(command = "Move", key_hold = "LAlt", sleep_time = 20){
DELTA := 100
CoordMode, Mouse, Screen ; Switch to screen/absolute coordinates.
MouseGetPos, mouse_x0, mouse_y0
active_win_id := GetActiveWindowId()
WinGet, active_win_state, MinMax, ahk_id %active_win_id%
if active_win_state = 0 ; Only if the window isn't maximized
{
direction_x = 0
direction_y = 0
SystemCursor("Off")
WinGetPos, active_win_x0, active_win_y0,active_win_w0,active_win_h0, ahk_id %active_win_id%
MouseMove, active_win_x0 + active_win_w0/2, active_win_y0 + active_win_h0/2, 0
MouseGetPos, cur_mouse_x1, cur_mouse_y1
While true
{
SetMouseDelay, -1
SetWinDelay, -1 ; Makes the below move faster/smoother.
GetKeyState, key_hold_state, %key_hold%, P
if key_hold_state= U ; Button has been released, so drag is complete.
{
break
}
GetKeyState, esc_key_state, Escape, P
if esc_key_state = D ; Escape has been pressed, so drag is cancelled.
{
WinMove, ahk_id %active_win_id%,, %active_win_x0%, %active_win_y0%
break
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse, Screen
MouseGetPos, cur_mouse_x2, cur_mouse_y2
WinGetPos, cur_win_x, cur_win_y, cur_win_w, cur_win_h, ahk_id %active_win_id%
delta_x := cur_mouse_x2 - cur_mouse_x1
delta_y := cur_mouse_y2 - cur_mouse_y1
cur_win_center_x := cur_win_x + delta_x + cur_win_w/2
cur_win_center_y := cur_win_y + delta_y + cur_win_h/2
if(command = "Move")
{
new_x := cur_win_x + delta_x
new_y := cur_win_y + delta_y
new_w := cur_win_w
new_h := cur_win_h
}
else if(command = "Resize")
{
if(delta_x <> 0 && direction_x = 0)
{
direction_x := Signal(delta_x)
}
if(delta_y <> 0 && direction_y = 0)
{
direction_y := Signal(delta_y)
}
new_x := cur_win_x + (1 - direction_x) / 2 * delta_x
new_y := cur_win_y + (1 - direction_y) / 2 * delta_y
new_w := cur_win_w + direction_x * delta_x
new_h := cur_win_h + direction_y * delta_y
}
WinMove, ahk_id %active_win_id%,, new_x, new_y, new_w, new_h
cur_mouse_x1 := cur_mouse_x2 ; Update for the next timer-call to this subroutine.
cur_mouse_y1 := cur_mouse_y2
Sleep %sleep_time%
}
MouseMove, mouse_x0, mouse_y0
SystemCursor("On")
}
return
}
SystemCursor(OnOff=1){ ; INIT = "I","Init"; OFF = 0,"Off"; TOGGLE = -1,"T","Toggle"; ON = others
static AndMask, XorMask, $, h_cursor
,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 ; system cursors
, b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13 ; blank cursors
, h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13 ; handles of default cursors
if (OnOff = "Init" or OnOff = "I" or $ = "") ; init when requested or at first call
{
$ = h ; active default cursors
VarSetCapacity( h_cursor,4444, 1 )
VarSetCapacity( AndMask, 32*4, 0xFF )
VarSetCapacity( XorMask, 32*4, 0 )
system_cursors = 32512,32513,32514,32515,32516,32642,32643,32644,32645,32646,32648,32649,32650
StringSplit c, system_cursors, `,
Loop %c0%
{
h_cursor := DllCall( "LoadCursor", "uint",0, "uint",c%A_Index% )
h%A_Index% := DllCall( "CopyImage", "uint",h_cursor, "uint",2, "int",0, "int",0, "uint",0 )
b%A_Index% := DllCall("CreateCursor","uint",0, "int",0, "int",0
, "int",32, "int",32, "uint",&AndMask, "uint",&XorMask )
}
}
if (OnOff = 0 or OnOff = "Off" or $ = "h" and (OnOff < 0 or OnOff = "Toggle" or OnOff = "T"))
$ = b ; use blank cursors
else
$ = h ; use the saved cursors
Loop %c0%
{
h_cursor := DllCall( "CopyImage", "uint",%$%%A_Index%, "uint",2, "int",0, "int",0, "uint",0 )
DllCall( "SetSystemCursor", "uint",h_cursor, "uint",c%A_Index% )
}
}
UPDATEDSCRIPT:
FileGetAttrib,attribs,%A_ScriptFullPath%
IfInString,attribs,A
{
FileSetAttrib,-A,%A_ScriptFullPath%
Sleep,500
Reload
}
Return
EXITSCRIPT:
SystemCursor("On")
ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment