1+ #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
2+ ; #Warn ; Enable warnings to assist with detecting common errors.
3+ SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
4+ SetWorkingDir %A_ScriptDir % ; Ensures a consistent starting directory.
5+
6+
7+
8+
9+ ; Function to open file picker dialog
10+ FilePicker (VarName, Options := "" ) {
11+ ; Store the current value
12+ PrevValue := %VarName%
13+
14+ ; Open the file picker dialog
15+ FileSelectFile , %VarName%, %Options%
16+
17+ ; Check if the user canceled the dialog and restore the previous value
18+ if (VarName = 0 )
19+ %VarName% := PrevValue
20+ }
21+
22+ ; Function to open folder picker dialog
23+ FolderPicker (VarName, Options := "" ) {
24+ ; Store the current value
25+ PrevValue := %VarName%
26+
27+ ; Open the folder picker dialog
28+ FileSelectFolder , %VarName%, %Options%
29+
30+ ; Check if the user canceled the dialog and restore the previous value
31+ if (VarName = 0 )
32+ %VarName% := PrevValue
33+ }
34+
35+
36+
37+ ; Define the INI file path
38+ IniFilePath := A_ScriptDir " \config.ini"
39+
40+ Gui , Add , Text , cBlue, Note: All assets must be in a folder which is in the same directory as your Python file.
41+
42+ ; Add this line after the last Gui, Add, Button line
43+ Gui , Add , Button , h25 gOpenExample, !!! When importing files do this !!!
44+ Gui , Add , Text ,,
45+ Gui , Add , Text ,,
46+
47+ Gui , Add , Text ,, Python Install Directory:
48+ Gui , Add , Edit , vPythonDir w480
49+ Gui , Add , Button , w80 h25 gBrowseDir, Browse
50+
51+ Gui , Add , Text ,, Python File:
52+ Gui , Add , Edit , vPythonFile w480
53+ Gui , Add , Button , w80 h25 gBrowseFile, Browse
54+
55+ Gui , Add , Text ,, Icon File (Optional):
56+ Gui , Add , Edit , vIconFile w480
57+ Gui , Add , Button , w80 h25 gBrowseIcon, Browse
58+
59+ Gui , Add , Text ,, Assets Directory (Optional):
60+ Gui , Add , Edit , vAssetsDir w480
61+ Gui , Add , Button , w80 h25 gBrowseAssets, Browse
62+
63+ Gui , Add , Text ,, Executable Name:
64+ Gui , Add , Edit , vExecName w480
65+
66+ Gui , Add , Button , w100 h30 gCompileButton, Compile
67+
68+ ; Load values from the INI file
69+ IniRead , PythonDir, %IniFilePath%, Settings, PythonDir
70+ IniRead , PythonFile, %IniFilePath%, Settings, PythonFile
71+ IniRead , IconFile, %IniFilePath%, Settings, IconFile
72+ IniRead , ExecName, %IniFilePath%, Settings, ExecName
73+ IniRead , AssetsDir, %IniFilePath%, Settings, AssetsDir
74+
75+ ; Check if any user inputted variable is "ERROR" and set it to ""
76+ if (PythonDir = " ERROR" )
77+ PythonDir := ""
78+ if (PythonFile = " ERROR" )
79+ PythonFile := ""
80+ if (IconFile = " ERROR" )
81+ IconFile := ""
82+ if (AssetsDir = " ERROR" )
83+ AssetsDir := ""
84+ if (ExecName = " ERROR" )
85+ ExecName := ""
86+
87+ GuiControl , , PythonDir, %PythonDir%
88+ GuiControl , , PythonFile, %PythonFile%
89+ GuiControl , , IconFile, %IconFile%
90+ GuiControl , , ExecName, %ExecName%
91+ GuiControl , , AssetsDir, %AssetsDir%
92+
93+ Gui , Show , w500 h500, Python Compiler
94+
95+ return
96+
97+ BrowseDir :
98+ Gui , Submit , NoHide
99+ FileSelectFolder , PythonDir, 4 , %PythonDir%, Choose Directory
100+ GuiControl , , PythonDir, %PythonDir%
101+ return
102+
103+ BrowseFile :
104+ Gui , Submit , NoHide
105+ FileSelectFile , PythonFile, 3 , %PythonFile%, Choose File
106+ GuiControl , , PythonFile, %PythonFile%
107+ return
108+
109+ BrowseIcon :
110+ Gui , Submit , NoHide
111+ FileSelectFile , IconFile, 3 , %IconFile%, Choose File
112+ GuiControl , , IconFile, %IconFile%
113+ return
114+
115+ BrowseAssets :
116+ Gui , Submit , NoHide
117+ FileSelectFolder , AssetsDir, 4 , %AssetsDir%, Choose Directory
118+ GuiControl , , AssetsDir, %AssetsDir%
119+ return
120+
121+ CompileButton :
122+
123+ Gui , Submit , NoHide
124+
125+ ; Save values to the INI file
126+ IniWrite , %PythonDir%, %IniFilePath%, Settings, PythonDir
127+ IniWrite , %PythonFile%, %IniFilePath%, Settings, PythonFile
128+ IniWrite , %IconFile%, %IniFilePath%, Settings, IconFile
129+ IniWrite , %ExecName%, %IniFilePath%, Settings, ExecName
130+ IniWrite , %AssetsDir%, %IniFilePath%, Settings, AssetsDir
131+
132+ ; Check if PythonDir is not blank and contains python.exe
133+ if (PythonDir = "" or ! FileExist (PythonDir " \Python.exe" ))
134+ {
135+ MsgBox , 16 , Error , Invalid Python Directory or Python.exe not found.
136+ return
137+ }
138+
139+ ; Check if PythonFile is not blank and has a .py extension
140+ if (PythonFile = "" or ! RegExMatch (PythonFile, " i)\.py$" ))
141+ {
142+ MsgBox , 16 , Error , Invalid Python File or not a .py file.
143+ return
144+ }
145+
146+ ; Check if IconFile is not blank and has a .ico extension
147+ if (IconFile ! = "" and ! RegExMatch (IconFile, " i)\.ico$" ))
148+ {
149+ MsgBox , 16 , Error , Invalid Icon File or not a .ico file.
150+ return
151+ }
152+
153+ ; Install or upgrade pyinstaller
154+ RunWait , %PythonDir%\Python.exe - m pip install -- trusted- host pypi.org -- trusted- host pypi.python.org -- trusted- host files.pythonhosted.org -- upgrade pyinstaller
155+
156+ ; Compile the Python script
157+ CompileCmd := " "" " . PythonDir . " \Scripts\pyinstaller.exe"" --distpath="" ."" --onefile --name="" " . ExecName . " "" "
158+ if (IconFile ! = "" )
159+ CompileCmd := CompileCmd . " --icon="" " . IconFile . " "" "
160+ if (AssetsDir ! = "" )
161+ SplitPath , AssetsDir, DirName
162+ CompileCmd := CompileCmd . " --add-data="" " . AssetsDir . " ;" . DirName . " "" "
163+ CompileCmd := CompileCmd . " "" " . PythonFile . " "" "
164+
165+ ; Set the working directory to the directory of the Python file
166+ FileGetAttrib , FileAttributes, %PythonFile%
167+ ; If PythonFile is a file, set the working directory to the directory containing the file
168+ SplitPath , PythonFile, , , Dir
169+
170+ RunWait , %CompileCmd%, %Dir%, , CompileExitCode
171+
172+ ; Display an info box indicating the completion of the process with an indeterminate success message
173+ MsgBox , 64 , Process Completed, The compilation process has finished. Please check the results.
174+
175+
176+ return
177+
178+
179+ GuiClose :
180+ ExitApp
181+ return
182+
183+
184+ OpenExample :
185+ ; Create the example window
186+ Gui , Example: New
187+ Gui , Example: Add , Edit , w600 h400 vExampleText ReadOnly r20
188+ Gui , Example: Show , w640 h480, Example way to use this tool
189+
190+ ; Set the Python syntax-highlighted code in the example window
191+ ExampleCode := " import sys`n" . " import os`n" . " def resource_path(relative_path):`n" . " # Get absolute path to resource, works for dev and for PyInstaller`n" . " base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))`n" . " return os.path.join(base_path, relative_path)`n`n" . " oldPath = 'assetsFileName/assetFile.txt'`n" . " newPath = resource_path(oldPath)`n"
192+ GuiControl , Example:, ExampleText, %ExampleCode%
193+ return
194+
195+ ; Add this line after the GuiClose: label
196+ OpenExampleGuiClose :
197+ Gui , Example: Destroy
198+ return
199+
200+ ; Add these lines at the end of the CompileButton label, after the MsgBox line
201+ ; Open the Example window when the user clicks "This Way" button
202+ OpenExampleButton :
203+ Gui , Example: Show
204+ return
0 commit comments