-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython and Wait.ahk
More file actions
94 lines (78 loc) · 2.17 KB
/
Copy pathPython and Wait.ahk
File metadata and controls
94 lines (78 loc) · 2.17 KB
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
pythonExe := ""
; Main script execution
args := A_Args
scriptPath := ""
scriptArgs := []
if (args.Length < 1) {
; Show file selection dialog if no argument is provided
fileDialog := FileSelect("1", A_WorkingDir, "Please select a Python script to run.", "Python Script (*.py)|*.py")
if (fileDialog = "") {
ExitApp
}
scriptPath := fileDialog
} else {
scriptPath := args[1]
if (args.Length > 1) {
Loop args.Length - 1 {
scriptArgs.Push(args[A_Index + 1])
}
}
}
; Verify the file exists
if !FileExist(scriptPath) {
MsgBox("The specified Python script was not found:`n" . scriptPath)
ExitApp
}
pythonExe := ResolvePythonCommand()
if (pythonExe = "") {
MsgBox("No Python interpreter was found. Configure run-with-python.ini or add Python to PATH.")
ExitApp
}
pythonCommand := QuoteArg(pythonExe) . " " . QuoteArg(scriptPath)
for arg in scriptArgs {
pythonCommand .= " " . QuoteArg(arg)
}
SplitPath(scriptPath, , &scriptDir)
if (scriptDir = "") {
scriptDir := A_WorkingDir
}
; Run the Python script and keep the terminal open
RunWait(A_ComSpec . ' /c "' . pythonCommand . ' & echo. & pause"', scriptDir)
ExitApp
QuoteArg(value) {
escaped := StrReplace(value, '"', '""')
return '"' . escaped . '"'
}
ResolvePythonCommand() {
configuredPython := ReadConfiguredPython()
if (configuredPython != "") {
if !FileExist(configuredPython) {
MsgBox("Configured Python path in run-with-python.ini does not exist:`n" . configuredPython)
ExitApp
}
return configuredPython
}
if (GetCommandPath("py.exe") != "") {
return "py"
}
if (GetCommandPath("python.exe") != "") {
return "python"
}
return ""
}
ReadConfiguredPython() {
iniPath := A_ScriptDir . "\\run-with-python.ini"
if !FileExist(iniPath) {
return ""
}
configuredPython := IniRead(iniPath, "run-with-python", "python_path", "")
return Trim(configuredPython)
}
GetCommandPath(commandName) {
shell := ComObject("WScript.Shell")
try {
return shell.Exec("where " . commandName).StdOut.ReadAll()
} catch {
return ""
}
}