Run() and quote() functions

Post your working scripts, libraries and tools
User avatar
Lateralus138
Posts: 49
Joined: 30 Aug 2015, 20:52
Facebook: lateralus138
Google: +IanPride
GitHub: Lateralus138
Location: Decatur, IL.
Contact:

Run() and quote() functions

22 Aug 2018, 09:58

Just a couple of quick functions I thought some might find useful. I have libraries full of functions and classes I have written and will post soon, but for now here's a couple of small funcs that can make your scripts a little easier to work with.

Run() is a wrapper function for the built-in AHK 'Run' command and quote() compartmentalizes strings you want to display quoted.

Run(): (returns true on success or some errors with successful run and returns false on all else, Run() is global by default):

Code: Select all

Run(target,work_dir:="",opts:="",pidvar:="",admin:=0){
	Global
	Local tmpv:=pidvar
	If ! admin
		Try,Run,%target%,%work_dir%,%opts%,%tmpv%
	Else
		Try,Run *RunAs %target%,%work_dir%,%opts%,%tmpv%
	Catch ERR
		Return
	Return ErrorLevel?ErrorLevel:1
}
quote() (double or single quote strings in any var/expression):

Code: Select all

quote(string,mode:=0){ ; mode defaults to double quotes
	mode:=!mode?"""":"`'"
	Return mode string mode
}
Example script (part of a little project I'm doing, no where near finished):
Using Run() to do a hidden/silent for loop in Windows cmd and using quote() to section off the "tokens=*" and 'dir /b' commands:

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Wrapper for the Windows 'move' command line utility 									;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Init - Directives                                                                     ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1
#SingleInstance, Force
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Init - Vars                                                                           ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TOKENS:=quote("tokens=*")
dir:=quote("C:/Users/" A_Username "/Desktop/Desktop Files/")
dircom:=quote("dir /b " dir ,1)
runcom:="C:\Windows\System32\cmd.exe /k for /f " TOKENS " %f in (" dircom ") do (echo %f >> " A_WorkingDir "\this.log)"
Run(runcom,A_WorkingDir,"hide","pid",1)
WinWait, ahk_pid %pid%
Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Functions                                                                             ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Run(target,work_dir:="",opts:="",pidvar:="",admin:=0){
	Global
	Local tmpv:=pidvar
	If ! admin
		Try,Run,%target%,%work_dir%,%opts%,%tmpv%
	Else
		Try,Run *RunAs %target%,%work_dir%,%opts%,%tmpv%
	Catch ERR
		Return
	Return ErrorLevel?ErrorLevel:1
}
quote(string,mode:=0){ ; mode defaults to double quotes
	mode:=!mode?"""":"`'"
	Return mode string mode
}
To test this script put some random files and folders into a new folder called "Desktop Files" on your desktop and run this.

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

Return to “Scripts and Functions”

Who is online

Users browsing this forum: boiler, MTRNord, robodesign, Weshuggah, Y-knot and 58 guests