-
Notifications
You must be signed in to change notification settings - Fork 1
/
ahkExplorerPassPath.ahk
134 lines (98 loc) · 5.61 KB
/
ahkExplorerPassPath.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, 2
OnExit, OnExit
TaskName := RunAsTask() ; This function will never return on the first run
FileRead, CmdLine, %A_Temp%\%A_ScriptName%.tmp
If ( CmdLine = "" ) ; Script/shortcut was launched with another admin app
CmdLine := DllCall( "GetCommandLine","Str" ) ; or with context menu item "Run as Administrator" !!!
Loop % ( A_IsCompiled ? 1 : 2 ) ; Excludes 'AutoHotkey exe' and 'Script path' from the command line
CmdLine := DllCall( "shlwapi\PathGetArgs", "Str",CmdLine, "Str" )
if (CmdLine) {
if (SubStr(CmdLine, 1 , 1)!="""")
p("WTH IS GOING ON")
pos:=InStr(CmdLine, """" ,, 2)
unQuoted:=SubStr(CmdLine, 2 , pos-2)
if WinExist("ahk_explorer ahk_class AutoHotkeyGUI") and unQuoted
{
send_string(unQuoted)
}
else
{
lol="%A_AhkPath%" "%A_ScriptFullPath%\..\ahk_explorer.ahk" "%unQuoted%"
run, %lol%
}
} else {
run, "%A_AhkPath%" ""%A_ScriptFullPath%\..\ahk_explorer.ahk""
}
ExitApp
OnExit:
IfNotEqual, A_ExitReason, Reload, FileDelete, %A_Temp%\%A_ScriptName%.tmp
IfEqual, TaskName,, FileAppend, % DllCall( "GetCommandLine","Str" ), %A_Temp%\%A_ScriptName%.tmp, UTF-8
ExitApp
send_string(stringToSend)
{
stringToSend .= "|1"
VarSetCapacity(message, size := StrPut(stringToSend, "UTF-16")*2, 0)
StrPut(stringToSend, &message, "UTF-16")
VarSetCapacity(COPYDATASTRUCT, A_PtrSize*3)
NumPut(size, COPYDATASTRUCT, A_PtrSize, "UInt")
NumPut(&message, COPYDATASTRUCT, A_PtrSize*2)
DetectHiddenWindows, On
SetTitleMatchMode, 2
SendMessage, WM_COPYDATA := 0x4A,, ©DATASTRUCT,, ahk_explorer.ahk ahk_class AutoHotkey
}
RunAsTask() { ; By SKAN, http://goo.gl/yG6A1F, CD:19/Aug/2014 | MD:24/Apr/2020
Local CmdLine, TaskName, TaskExists, XML, TaskSchd, TaskRoot, RunAsTask
Local TASK_CREATE := 0x2, TASK_LOGON_INTERACTIVE_TOKEN := 3
Try TaskSchd := ComObjCreate( "Schedule.Service" ), TaskSchd.Connect()
, TaskRoot := TaskSchd.GetFolder( "\" )
Catch
Return "", ErrorLevel := 1
CmdLine := ( A_IsCompiled ? "" : """" A_AhkPath """" ) A_Space ( """" A_ScriptFullpath """" )
TaskName := "[RunAsTask] " A_ScriptName " @" SubStr( "000000000" DllCall( "NTDLL\RtlComputeCrc32"
, "Int",0, "WStr",CmdLine, "UInt",StrLen( CmdLine ) * 2, "UInt" ), -9 )
Try RunAsTask := TaskRoot.GetTask( TaskName )
TaskExists := ! A_LastError
If ( not A_IsAdmin and TaskExists ) {
RunAsTask.Run( "" )
ExitApp
}
If ( not A_IsAdmin and not TaskExists ) {
Run *RunAs %CmdLine%, %A_ScriptDir%, UseErrorLevel
ExitApp
}
If ( A_IsAdmin and not TaskExists ) {
XML := "
( LTrim Join
<?xml version=""1.0"" ?><Task xmlns=""http://schemas.microsoft.com/windows/2004/02/mit/task""><Regi
strationInfo /><Triggers /><Principals><Principal id=""Author""><LogonType>InteractiveToken</LogonT
ype><RunLevel>HighestAvailable</RunLevel></Principal></Principals><Settings><MultipleInstancesPolic
y>Parallel</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><
StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAva
ilable><IdleSettings><StopOnIdleEnd>true</StopOnIdleEnd><RestartOnIdle>false</RestartOnIdle></IdleS
ettings><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled><Hidden>false</Hidden><
RunOnlyIfIdle>false</RunOnlyIfIdle><DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteApp
Session><UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine><WakeToRun>false</WakeToRun><
ExecutionTimeLimit>PT0S</ExecutionTimeLimit></Settings><Actions Context=""Author""><Exec>
<Command>""" ( A_IsCompiled ? A_ScriptFullpath : A_AhkPath ) """</Command>
<Arguments>" ( !A_IsCompiled ? """" A_ScriptFullpath """" : "" ) "</Arguments>
<WorkingDirectory>" A_ScriptDir "</WorkingDirectory></Exec></Actions></Task>
)"
TaskRoot.RegisterTask( TaskName, XML, TASK_CREATE, "", "", TASK_LOGON_INTERACTIVE_TOKEN )
}
Return TaskName, ErrorLevel := 0
} ; _____________________________________________________________________________________________________
Args( CmdLine := "", Skip := 0 )
{ ; By SKAN, http://goo.gl/JfMNpN, CD:23/Aug/2014 | MD:24/Aug/2014
Local pArgs := 0, nArgs := 0, A := []
pArgs := DllCall( "Shell32\CommandLineToArgvW", "WStr",CmdLine, "PtrP",nArgs, "Ptr" )
Loop % ( nArgs )
If ( A_Index > Skip )
A[ A_Index - Skip ] := StrGet( NumGet( ( A_Index - 1 ) * A_PtrSize + pArgs ), "UTF-16" )
Return A, A[0] := nArgs - Skip, DllCall( "LocalFree", "Ptr",pArgs )
}
f3::Exitapp