Just put this script to your Standard Library and put this function at the top of the script, as simple as that.
RunAsAdmin()
Download : RunAsAdmin.ahk
History:
To check if the user has Administrator rights and elevate it if needed by the script, i stumbled upon the help doc, which states that
on page http://www.autohotke...les.htm#BuiltIn....this would need to be enhanced to work for compiled scripts and/or to pass parameters..
The code given is..
if not A_IsAdmin { DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_AhkPath , str, """" . A_ScriptFullPath . """", str, A_WorkingDir, int, 1) ExitApp }
To run for any scripts, compiled or not, with params use this code instead..
Loop, %0% ; For each parameter: { param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index. params .= A_Space . param } ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA" if not A_IsAdmin { If A_IsCompiled DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1) Else DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1) ExitApp }
Edit1: Now it should work with both Basic AHK, Ansi and Unicode Autohotkey_L
Edit2: Shorter code, thanks Lexikos.
Edit3: Lib compliant version.
Also, why not use this code in the help file and docs also, so that any noob like me can benefit?[/url]