Wednesday, August 27, 2008

AHK - Easy edit scripts

This is a simple script that allows you to edit .ahk & .au3 files simply by pressing ctrl+e while they are highlighted in an explorer window.

#NoTrayIcon
#IfWinActive ahk_class CabinetWClass
^e::
ControlGet, List, List, Selected, SysListView321
FName := RegExReplace(List,"U)^(.*)`t.*$","$1")
FPath := ShellFolder()
Ext := SubStr(FName, -3, 4)
If Ext in .ahk,.au3
{
Run, edit "%FPath%\%FName%"
}
Return

; Shell Folder function by Sean
; http://www.autohotkey.com/forum/topic20701.html
ShellFolder(hWnd=0)
{
If hWnd||(hWnd:=WinExist("ahk_class CabinetWClass"))||(hWnd:=WinExist("ahk_class ExploreWClass"))
{
COM_Init()
psh := COM_CreateObject("Shell.Application")
psw := COM_Invoke(psh, "Windows")
Loop, % COM_Invoke(psw, "Count")
If COM_Invoke(pwb:=COM_Invoke(psw, "Item", A_Index-1), "hWnd") <> hWnd
COM_Release(pwb)
Else Break
pfv := COM_Invoke(pwb, "Document")
sFolder := COM_Invoke(pfi:=COM_Invoke(psf:=COM_Invoke(pfv, "Folder"), "Self"), "Path"), COM_Release(psf), COM_Release(pfi), pfi:=0
COM_Release(psi)
COM_Release(pfv)
COM_Release(pwb)
COM_Release(psw)
COM_Release(psh)
COM_Term()
Return sFolder
}
}


* Note - This script requires the COM Standard Library (you can place it in your standard lib directory, or #include it).
* Note - The ShellFolder() function was written by Sean from the AutoHotkey forums.

Explorer Windows Manipulations, COM Standard Library

AHK - Make a script delete itself

With these few lines of code put in your OnExit label, your script will automatically delete itself when it exits. All it does is create a batch file and run it as soon as the script exits.
FileAppend, DEL "%A_ScriptFullPath%"`nDEL "%A_ScriptDir%\del.bat", %A_ScriptDir%\del.bat
Loop {
if (FileExist(A_ScriptDir "\del.bat"))
break
}
Run, del.bat,, Hide
Exitapp
Forum Post

Tuesday, August 26, 2008

AHK - GetFileLines()

This is a simple function that retrieves the total number of lines in a text file, without using any loops. I tested it on a file with about 1200 lines and it took less than 20ms.

GetFileLines(ByRef OutputVar,FileLocation) {
FileRead, File, % FileLocation
StringSplit, Lines, File, `n
OutputVar := Lines0
}
Example use
GetFileLines(TotalLines,"YourTextFile.txt")
MsgBox YourTextFile.txt has %TotalLines% lines!
Forum Post

Wednesday, August 13, 2008

AHK - Function Linker for forums

This is a script I just threw together that will replace names of functions and built-in variables with links to the online documentation. For example, when I type MsgBox it replaces it with [url=http://www.autohotkey.com/docs/commands/MsgBox.htm]MsgBox[/url].

Toggles suspend with the insert key, suspends when you type [code] and un-suspends when you type [/code].

*Note - I left out the special loops because the didn't have a standard page (yes, I'm lazy, might add them later). I also left out the "If var" commands because I didn't feel like including them
#Hotstring NoMouse
SendMode, Input

; ----------
; Toggle Keys
; ----------
~Insert::Suspend
:*b0?:[code]::
Suspend On
Return
:*b0?:[/code]::
Suspend Off
Return

; ----------
; Built in variables
; ----------
::A_AhkPath::
::A_AhkVersion::
::A_AppData::
::A_AppDataCommon::
::A_AutoTrim::
::A_BatchLines::
::A_CaretX::
::A_CaretY::
::A_ComputerName::
::A_ControlDelay::
::A_Cursor::
::A_DD::
::A_DDD::
::A_DDDD::
::A_DefaultMouseSpeed::
::A_Desktop::
::A_DesktopCommon::
::A_DetectHiddenText::
::A_DetectHiddenWindows::
::A_EndChar::
::A_EventInfo::
::A_ExitReason::
::A_FormatFloat::
::A_FormatInteger::
::A_Gui::
::A_GuiControl::
::A_GuiControlEvent::
::A_GuiEvent::
::A_GuiHeight::
::A_GuiWidth::
::A_GuiX::
::A_GuiY::
::A_Hour::
::A_IconFile::
::A_IconHidden::
::A_IconNumber::
::A_IconTip::
::A_Index::
::A_IPAddress::
::A_IsAdmin::
::A_IsCompiled::
::A_IsSuspended::
::A_KeyDelay::
::A_Language::
::A_LastError::
::A_LineNumber::
::A_LoopField::
::A_LoopFileAttrib::
::A_LoopFileDir::
::A_LoopFileExt::
::A_LoopFileLongPath::
::A_LoopFileName::
::A_LoopFileShortName::
::A_LoopFileShortPath::
::A_LoopFileSize::
::A_LoopFileSizeKB::
::A_LoopFileSizeMB::
::A_LoopFileTimeAccessed::
::A_LoopFileTimeCreated::
::A_LoopFileTimeModified::
::A_LoopReadLine::
::A_LoopRegKey::
::A_LoopRegName::
::A_LoopRegSubKey::
::A_LoopRegTimeModified::
::A_LoopRegType::
::A_MDay::
::A_Min::
::A_MM::
::A_MMM::
::A_MMMM::
::A_Mon::
::A_MouseDelay::
::A_MSec::
::A_MyDocuments::
::A_Now::
::A_NowUTC::
::A_NumBatchLines::
::A_OSType::
::A_OSVersion::
::A_PriorHotkey::
::A_ProgramFiles::
::A_Programs::
::A_ProgramsCommon::
::A_ScreenHeight::
::A_ScreenWidth::
::A_ScriptDir::
::A_ScriptFullPath::
::A_ScriptName::
::A_Sec::
::A_Space::
::A_StartMenu::
::A_StartMenuCommon::
::A_Startup::
::A_StartupCommon::
::A_StringCaseSense::
::A_Tab::
::A_Temp::
::A_ThisFunc::
::A_ThisHotkey::
::A_ThisLabel::
::A_ThisMenu::
::A_ThisMenuItem::
::A_ThisMenuItemPos::
::A_TickCount::
::A_TimeIdle::
::A_TimeIdlePhysical::
::A_TimeSincePriorHotkey::
::A_TimeSinceThisHotkey::
::A_TitleMatchMode::
::A_TitleMatchModeSpeed::
::A_UserName::
::A_WDay::
::A_WinDelay::
::A_WinDir::
::A_WorkingDir::
::A_YDay::
::A_Year::
::A_YWeek::
::A_YYYY::
Var := RegExReplace(A_ThisHotkey,"^::A_","")
Send, [url=http://www.autohotkey.com/docs/Variables.htm{#}%Var%]A_%Var%[/url]%A_EndChar%
Return


; ----------
; Built in functions
; ----------
::Abs()::
::ACos()::
::Asc()::
::ASin()::
::ATan()::
::Ceil()::
::Chr()::
::Cos()::
::DllCall()::
::Exp()::
::FileExist()::
::Floor()::
::GetKeyState()::
::InStr()::
::IsLabel()::
::Ln()::
::Log()::
::Mod()::
::NumGet()::
::NumPut()::
::OnMessage()::
::RegExMatch()::
::RegExReplace()::
::RegisterCallback()::
::Round()::
::Sin()::
::SubStr()::
::Sqrt()::
::StrLen()::
::Tan()::
::VarSetCapacity()::
::WinActive()::
::WinExist()::
Var := RegExReplace(A_ThisHotkey,"^::(.*)\(\)$","$1")
Send, [url=http://www.autohotkey.com/docs/Functions.htm{#}%Var%]%Var%()[/url]%A_EndChar%
Return


; ----------
; # Commands
; ----------
::#AllowSameLineComments::
::#ClipboardTimeout::
::#CommentFlag::
::#ErrorStdOut::
::#EscapeChar::
::#HotkeyInterval::
::#HotkeyModifierTimeout::
::#Hotstring::
::#IfWinActive::
::#IfWinExist::
::#IfWinNotActive::
::#IfWinNotExist::
::#Include::
::#IncludeAgain::
::#InstallKeybdHook::
::#InstallMouseHook::
::#KeyHistory::
::#LTrim::
::#MaxHotkeysPerInterval::
::#MaxMem::
::#MaxThreads::
::#MaxThreadsBuffer::
::#MaxThreadsPerHotkey::
::#NoEnv::
::#NoTrayIcon::
::#Persistent::
::#SingleInstance::
::#UseHook::
::#WinActivateForce::
Var := RegExReplace(A_ThisHotkey,"^::#","")
Send, [url=http://www.autohotkey.com/docs/commands/_%Var%.htm]#%Var%[/url]%A_EndChar%
Return


; ----------
; Commands
; ----------
::AutoTrim::
::BlockInput::
::Break::
::Click::
::ClipWait::
::Continue::
::Control::
::ControlClick::
::ControlFocus::
::ControlGet::
::ControlGetFocus::
::ControlGetPos::
::ControlGetText::
::ControlMove::
::ControlSend::
::ControlSendRaw::
::ControlSetText::
::CoordMode::
::Critical::
::DetectHiddenText::
::DetectHiddenWindows::
::Drive::
::DriveGet::
::DriveSpaceFree::
::Edit::
::Else::
::EnvAdd::
::EnvDiv::
::EnvGet::
::EnvMult::
::EnvSet::
::EnvSub::
::EnvUpdate::
::Exit::
::ExitApp::
::FileAppend::
::FileCopy::
::FileCopyDir::
::FileCreateDir::
::FileCreateShortcut::
::FileDelete::
::FileGetAttrib::
::FileGetShortcut::
::FileGetSize::
::FileGetTime::
::FileGetVersion::
::FileInstall::
::FileMove::
::FileMoveDir::
::FileRead::
::FileReadLine::
::FileRecycle::
::FileRecycleEmpty::
::FileRemoveDir::
::FileSelectFile::
::FileSelectFolder::
::FileSetAttrib::
::FileSetTime::
::FormatTime::
::GetKeyState::
::Gosub::
::Goto::
::GroupActivate::
::GroupAdd::
::GroupClose::
::GroupDeactivate::
::Gui::
::GuiControl::
::GuiControlGet::
::Hotkey::
::IfEqual::
::IfExist::
::IfGreater::
::IfGreaterOrEqual::
::IfInString::
::IfLess::
::IfLessOrEqual::
::IfMsgBox::
::IfNotEqual::
::IfNotExist::
::IfNotInString::
::IfWinActive::
::IfWinExist::
::IfWinNotActive::
::IfWinNotExist::
::ImageSearch::
::IniDelete::
::IniRead::
::IniWrite::
::Input::
::InputBox::
::KeyHistory::
::KeyWait::
::ListHotkeys::
::ListLines::
::ListVars::
::Loop::
::Menu::
::MouseClick::
::MouseClickDrag::
::MouseGetPos::
::MouseMove::
::MsgBox::
::OnExit::
::OutputDebug::
::Pause::
::PixelGetColor::
::PixelSearch::
::PostMessage::
::Process::
::Progress::
::Random::
::RegDelete::
::RegRead::
::RegWrite::
::Reload::
::Repeat::
::Return::
::Run::
::RunAs::
::RunWait::
::Send::
::SendEvent::
::SendInput::
::SendMessage::
::SendMode::
::SendPlay::
::SendRaw::
::SetBatchLines::
::SetCapslockState::
::SetControlDelay::
::SetDefaultMouseSpeed::
::SetEnv::
::SetFormat::
::SetKeyDelay::
::SetMouseDelay::
::SetNumlockState::
::SetScrollLockState::
::SetStoreCapslockMode::
::SetTimer::
::SetTitleMatchMode::
::SetWinDelay::
::SetWorkingDir::
::Shutdown::
::Sleep::
::Sort::
::SoundBeep::
::SoundGet::
::SoundGetWaveVolume::
::SoundPlay::
::SoundSet::
::SoundSetWaveVolume::
::SplashImage::
::SplashTextOff::
::SplashTextOn::
::SplitPath::
::StatusBarGetText::
::StatusBarWait::
::StringCaseSense::
::StringGetPos::
::StringLeft::
::StringLen::
::StringLower::
::StringMid::
::StringReplace::
::StringRight::
::StringSplit::
::StringTrimLeft::
::StringTrimRight::
::StringUpper::
::Suspend::
::SysGet::
::Thread::
::ToolTip::
::Transform::
::TrayTip::
::URLDownloadToFile::
::WinActivate::
::WinActivateBottom::
::WinClose::
::WinGet::
::WinGetActiveStats::
::WinGetActiveTitle::
::WinGetClass::
::WinGetPos::
::WinGetText::
::WinGetTitle::
::WinHide::
::WinKill::
::WinMaximize::
::WinMenuSelectItem::
::WinMinimize::
::WinMinimizeAll::
::WinMinimizeAllUndo::
::WinMove::
::WinRestore::
::WinSet::
::WinSetTitle::
::WinShow::
::WinWait::
::WinWaitActive::
::WinWaitClose::
::WinWaitNotActive::
Var := RegExReplace(A_ThisHotkey,"^::","")
Send, [url=http://www.autohotkey.com/docs/commands/%Var%.htm]%Var%[/url]%A_EndChar%
Return


*Note - See my post on the AHK Forums Here

Sunday, August 10, 2008

AHK - ASCII to HEX converter

This is a simple function that converts ASCII into HEX
AscToHex(str)
{
NStr =
Tmp := A_FormatInteger
SetFormat, Integer, H
Loop, Parse, str
NStr .= RegExReplace(asc(A_LoopField),"^0x")
SetFormat, Integer, %Tmp%
Return NStr
}
For example, "Hello World!" becomes
48656C6C6F20576F726C6421

Wednesday, August 6, 2008

AHK - SendDelay()

This function will have a script send "key" only once every "delay" milliseconds.
SendDelay(key,delay) {
Global
If (lastSent_%key% = "" || lastSent_%key% < A_TickCount - delay)
{
Send %key%
lastSent_%key% := A_TickCount
Return 1
}
Return 0
}
For example, the following script will send the "a" key once every 1.5 seconds.
Loop
SendDelay("a",1500)
For a better example, see the post AHK - World of Warcraft - Shaman ES/SS spam

AHK - MultiPress()

This is a function I wrote that allows you to press a hotkey multiple times and have different things happen for the number of presses.
MultiPress(Max=-1)
{
Presses := 1
KeyWait, %A_ThisHotkey%
Loop
{
KeyWait, %A_ThisHotkey%, D T0.25
If (ErrorLevel || (Presses >= Max && Max > 0))
Break
Presses++
KeyWait, %A_ThisHotkey%
}
Return Presses
}
An example of use for this function is
a::
P := MultiPress()
MsgBox Pressed %A_ThisHotkey% %P% times!
Return

AHK - World of Warcraft - Shaman ES/SS spam

This is a script that will make you spam Earth Shock/Stormstrike repeatedly when you press the F5 key, until the F5 key is pressed again. To use, set earth shock to hotkey 2 and stormstrike to hotkey 1.
GoGoShammySpam = 1
WinWaitActive, Untitled
Time := A_TickCount
Loop {
If (GoGoShammySpam) {
If (SendDelay("1",10000)) ; Stormstrike
sleep, 1500 ; GCD
If (SendDelay("2",6000)) ; Earth Shock
sleep, 1500 ; GCD
}
}

f5::GoGoShammySpam:=!GoGoShammySpam

SendDelay(key,delay) {
Global
If (lastSent_%key% = "" || lastSent_%key% < A_TickCount - delay)
{
Send %key%
lastSent_%key% := A_TickCount
Return 1
}
Return 0
}

*Note - The use of this script violates World of Warcraft's Terms of Service, and can get you banned. It is meant as an example. I do not recommend or advocate the use of this script on live servers. If you are stupid enough to use it, I take no responsibility for the consequences.

Tuesday, August 5, 2008

AU3 - Dynamic Variable Example

Here's an example of how to create a dynamic variable in AU3.
$Fu    = "World!"
$Bar = "Fu"
$Fubar = "Hello " & Execute("$"&$Bar)
In this example, $Fubar is set to "Hello World!"

Here's a function that will convert all variables and macros in a string into their stored values.
Func ParseMacros($Str)
Global
Local $Macros = StringRegExp($Str,"([\$\@]\w+)",3)
If Not @error Then
For $i=0 to UBound($Macros) - 1 step 1
$Str = StringRegExpReplace($Str,"\"&$Macros[$i],StringReplace(Execute($Macros[$i]),"\","\\"))
Next
EndIf
Return $Str
EndFunc
For example, using the variables from the first code, this will evaluate to "Hello World!"
ParseMacros("Hello $Fu")
I find this useful for scripts where you need to store dynamic strings in a text file, because you can just use something like ParseMacros(FileRead("File.txt")) and you get a nice dynamic string to play with.

AHK - Scan Code Finder

A short script that creates a listview that will record the scan codes of the keys you press. Credits to Krogdor for creating this method of finding the scan code of pressed keys.
#InstallKeybdhook
#NoTrayIcon

Gui, Add, ListView, r20 w150 h125 NoSort Readonly, SC
Gui, Show, Autosize
Loop 9
OnMessage( 255+A_Index, "ScanCode" ) ; 0x100 to 0x108
Return

ScanCode( wParam, lParam ) {
Static Prev := 0
If (Prev != SubStr((((lParam>>16) & 0xFF)+0xF000),-2)) {
LV_Insert(1,"Focus Select","SC" SubStr((((lParam>>16) & 0xFF)+0xF000),-2))
LV_Modify(2,"-Select")
}
Prev := SubStr((((lParam>>16) & 0xFF)+0xF000),-2)
}

GuiClose:
ExitApp

AHK - Notepad Indent

This is a simple key replacement I use for notepad that replaces the TAB character with 3 spaces. Just my personal preference for indenting nested ifs, loops, etc.
#NoTrayIcon
#IfWinActive ahk_class Notepad
:*:`t::{SPACE 3}

Sunday, August 3, 2008

AHK - A simple auto-clicker

Simply auto-clicks when the left mouse button is held down. Insert toggles the auto-clicker on and off.
Insert::
Hotkey, LButton, Toggle
Hotkey, LButton Up, Toggle
Return
LButton::
Loop
{
If (Stop)
Break
Send {lbutton}
}
Stop := 0
Return
LButton Up::Stop := 1