Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
user:geek:tricks [2022-03-20 18:24] – created geek | user:geek:tricks [2023-07-15 13:02] (current) – [Writing Libraries] Adjust for better sandbox behavior geek | ||
---|---|---|---|
Line 5: | Line 5: | ||
Repeat a String. | Repeat a String. | ||
- | <code autohotkey> | + | <runner ahk1> |
StrRepeat(String, | StrRepeat(String, | ||
{ | { | ||
return StrReplace(Format(" | return StrReplace(Format(" | ||
} | } | ||
- | </code> | + | |
+ | MsgBox, % StrRepeat(" | ||
+ | </runner> | ||
Pad a string to a given length. | Pad a string to a given length. | ||
- | <code autohotkey> | + | <runner ahk1> |
Var := " | Var := " | ||
Line 25: | Line 27: | ||
MsgBox, % SubStr(" | MsgBox, % SubStr(" | ||
MsgBox, % SubStr(Var " | MsgBox, % SubStr(Var " | ||
- | </code> | + | </runner> |
Remove extraneous delimiters when compiling a list in a loop WITHOUT using an if statement. | Remove extraneous delimiters when compiling a list in a loop WITHOUT using an if statement. | ||
- | <code autohotkey> | + | <runner ahk1> |
Loop, 9 | Loop, 9 | ||
List .= ", " A_Index | List .= ", " A_Index | ||
Line 38: | Line 40: | ||
; LTrim can be used to remove many delimiters | ; LTrim can be used to remove many delimiters | ||
MsgBox, % LTrim(List, ", ") | MsgBox, % LTrim(List, ", ") | ||
- | </code> | + | </runner> |
Check if a string starts with another string. | Check if a string starts with another string. | ||
Line 57: | Line 59: | ||
Use standard JSON format when defining your objects by placing the definition into a continuation section. | Use standard JSON format when defining your objects by placing the definition into a continuation section. | ||
- | <code autohotkey> | + | <runner ahk1> |
; http:// | ; http:// | ||
MyObject := | MyObject := | ||
Line 84: | Line 86: | ||
} | } | ||
) | ) | ||
- | </code> | + | |
+ | Print(MyObject) | ||
+ | </runner> | ||
===== Toggles ===== | ===== Toggles ===== | ||
Line 90: | Line 94: | ||
Do a toggle with only one line. | Do a toggle with only one line. | ||
- | <code autohotkey> | + | <runner |
- | Loop | + | Loop 10 |
MsgBox, % Toggle := !Toggle | MsgBox, % Toggle := !Toggle | ||
- | </code> | + | </runner> |
===== DllCall ===== | ===== DllCall ===== | ||
Line 107: | Line 111: | ||
* This doesn' | * This doesn' | ||
* This is not the same as using the standard output, so output done in this way will not appear if you run your script from the command prompt. | * This is not the same as using the standard output, so output done in this way will not appear if you run your script from the command prompt. | ||
+ | |||
+ | See also [[libraries: | ||
<code autohotkey> | <code autohotkey> | ||
Line 147: | Line 153: | ||
</ | </ | ||
+ | Get the name of the process that last touched the clipboard. | ||
+ | |||
+ | <code autohotkey> | ||
+ | OnClipboardChange: | ||
+ | DetectHiddenWindows, | ||
+ | WinGet, name, ProcessName, | ||
+ | ToolTip, %name% | ||
+ | return | ||
+ | </ | ||
===== Writing Libraries ===== | ===== Writing Libraries ===== | ||
When writing a library that has code which must be run before any of your functions can be used, you can create an initialization function that runs itself automatically when the script starts. This eliminates any need for the library' | When writing a library that has code which must be run before any of your functions can be used, you can create an initialization function that runs itself automatically when the script starts. This eliminates any need for the library' | ||
- | <code autohotkey> | + | <runner ahk1> |
+ | global sampleVariable | ||
Initialize() | Initialize() | ||
{ | { | ||
- | Static Dummy := Initialize() | + | static _ := Initialize() |
- | MsgBox, This init function has been called automatically | + | sampleVariable := " |
} | } | ||
- | </code> | + | |
+ | MsgBox, % sampleVariable | ||
+ | </runner> | ||
If your library defines hotkeys, it can break other scripts that include it in their auto-execute section. You can avoid this by wrapping your hotkey definitions in '' | If your library defines hotkeys, it can break other scripts that include it in their auto-execute section. You can avoid this by wrapping your hotkey definitions in '' | ||
- | <code autohotkey> | + | <runner ahk1> |
if False | if False | ||
{ | { | ||
Line 169: | Line 188: | ||
MsgBox, Auto-execution not interrupted | MsgBox, Auto-execution not interrupted | ||
- | </code> | + | |
+ | ExitApp | ||
+ | </runner> | ||
===== Windows ===== | ===== Windows ===== | ||
Line 293: | Line 314: | ||
Find, and optionally replace, all matches of regular expression efficiently using a custom enumerator. | Find, and optionally replace, all matches of regular expression efficiently using a custom enumerator. | ||
- | <code autohotkey> | + | <runner ahk1> |
Haystack = | Haystack = | ||
( | ( | ||
Line 342: | Line 363: | ||
} | } | ||
} | } | ||
- | </code> | + | </runner> |
===== Networking and Web ===== | ===== Networking and Web ===== | ||
Line 366: | Line 387: | ||
; Get the data pointer and size. The pointer will be valid | ; Get the data pointer and size. The pointer will be valid | ||
; only as long as a reference to Request.responseBody is kept. | ; only as long as a reference to Request.responseBody is kept. | ||
- | pData := NumGet(ComObjValue(Request.responseBody)+8+A_PtrSize, | + | pData := NumGet(ComObjValue(Request.responseBody)+8+A_PtrSize, |
Size := Request.responseBody.MaxIndex()+1 | Size := Request.responseBody.MaxIndex()+1 | ||
Line 383: | Line 404: | ||
Overwrite a file in one step using '' | Overwrite a file in one step using '' | ||
- | <code autohotkey> | + | <runner ahk1> |
/* Old method: | /* Old method: | ||
FileDelete, | FileDelete, | ||
Line 389: | Line 410: | ||
*/ | */ | ||
- | FileOpen(" | + | FileOpen(" |
- | </code> | + | FileOpen(" |
+ | |||
+ | FileRead, contents, FileName.txt | ||
+ | MsgBox, Contains only: %contents% | ||
+ | </runner> | ||
===== Notes ===== | ===== Notes ===== |