Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
user:geek:tricks [2022-03-20 19:05] – [DllCall] Add GetClipboardOwner geekuser: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, Times) StrRepeat(String, Times)
 { {
  return StrReplace(Format("{:0" Times "}", 0), "0", String)  return StrReplace(Format("{:0" Times "}", 0), "0", String)
 } }
-</code>+ 
 +MsgBox, % StrRepeat("na ", 13) "batman!" 
 +</runner>
  
 Pad a string to a given length. Pad a string to a given length.
  
-<code autohotkey>+<runner ahk1>
 Var := "abc123" Var := "abc123"
  
Line 25: Line 27:
 MsgBox, % SubStr("-=-=-=-=-=" Var, 1-10) MsgBox, % SubStr("-=-=-=-=-=" Var, 1-10)
 MsgBox, % SubStr(Var "=-=-=-=-=-", 1, 10) MsgBox, % SubStr(Var "=-=-=-=-=-", 1, 10)
-</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://www.json.org/example.html ; http://www.json.org/example.html
 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 autohotkey> 
-Loop+Loop 10
  MsgBox, % Toggle := !Toggle  MsgBox, % Toggle := !Toggle
-</code>+</runner>
  
 =====  DllCall ===== =====  DllCall =====
Line 162: Line 166:
 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's users to put your code into their auto-execute section. 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's users to put your code into their auto-execute section.
  
-<code autohotkey>+<runner ahk1> 
 +global sampleVariable 
 Initialize() Initialize()
 { {
- Static Dummy := Initialize() + static _ := Initialize() 
- MsgBox, This init function has been called automatically+ sampleVariable := "Automatically Initialized"
 } }
-</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 False''. 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 False''.
  
-<code autohotkey>+<runner ahk1>
 if False if False
 { {
Line 180: Line 188:
  
 MsgBox, Auto-execution not interrupted MsgBox, Auto-execution not interrupted
-</code>+ 
 +ExitApp 
 +</runner>
  
 ===== Windows ===== ===== Windows =====
Line 304: 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 353: Line 363:
  }  }
 } }
-</code>+</runner>
  
 ===== Networking and Web ===== ===== Networking and Web =====
Line 394: Line 404:
 Overwrite a file in one step using ''FileOpen''. Overwrite a file in one step using ''FileOpen''.
  
-<code autohotkey>+<runner ahk1>
 /* Old method: /* Old method:
  FileDelete, FileName.txt  FileDelete, FileName.txt
Line 400: Line 410:
 */ */
  
-FileOpen("FileName.txt", "w").Write("New contents") +FileOpen("FileName.txt", "w").Write("First overwrite") 
-</code>+FileOpen("FileName.txt", "w").Write("Second overwrite"
 + 
 +FileRead, contents, FileName.txt 
 +MsgBox, Contains only: %contents% 
 +</runner>
  
 ===== Notes ===== ===== Notes =====