So for any of you Python buffs out there, this little script can be a nice shortcut. For those of you who don't know, Python is an interpreted language like AHK, but sharing them on Windows can be a PITA. So they invented a program called "cx_freeze" that compiles scripts to an EXE. But it's a command-line app that can also be a PITA . I wanted a way to just click an icon, choose a file, and be done with it (kinda like ahk2exe ). Yes, sometimes having tons of options is a good thing (especially if you're building something commercial), but most of the time it really isn't necessary. So here goes nuttin':
Code: Select all
; Bring up a "file open" dialog, saving the path to the output variable "file"
FileSelectFile, file,, %A_ScriptDir%, Freeze Python Script, Python Scripts (*.py)
; If no file was selected, we're done
if (file == "")
ExitApp
; Replaces \ with \\, as the Windows command prompt requires that
file := StrReplace(file, "\", "\\", 0)
; Set up the directory where the compiled EXE should go
; I just wanted it to be a "build" folder in the same path as the Python script
parts := StrSplit(file, "\\")
path := ""
Loop % parts.MaxIndex()
{
if (A_Index == parts.MaxIndex())
path .= "build"
else
path .= parts[A_Index] . "\\"
}
; If an icon was placed in the same directory as the script, add it to the settings
; So first, we need the script's path (some more string-replacing stuff)
icon := ""
path2 := StrReplace(path, "\\", "\", 0)
path2 := StrReplace(path2, "\build", "", 0)
; Then if an icon is found, save its path
Loop, Files, %path2%/*.ico
{
icon := path2 . "\" . A_LoopFileName
icon := StrReplace(icon, "\", "\\", 0)
break
}
; Then run cxfreeze and we're good
if (icon != "")
RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%" --icon "%icon%", , Hide
else
RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%", , Hide
TrayTip, Freezer, Python script "frozen" successfully; Bring up a "file open" dialog, saving the path to the output variable "file"
FileSelectFile, file,, %A_ScriptDir%, Freeze Python Script, Python Scripts (*.py)
; If a file was selected, "freeze" it as an EXE
if (file == "")
ExitApp
; Replaces \ with \\, as the Windows command prompt requires that
file := StrReplace(file, "\", "\\", 0)
; Set up the directory where the compiled EXE should go
; I just wanted it to be a "build" folder in the same path as the Python script
parts := StrSplit(file, "\\")
path := ""
Loop % parts.MaxIndex()
{
if (A_Index == parts.MaxIndex())
path .= "build"
else
path .= parts[A_Index] . "\\"
}
; If an icon was placed in the same directory as the script, add it to the settings
; So first, we need the script's path (some more string-replacing stuff)
icon := ""
path2 := StrReplace(path, "\\", "\", 0)
path2 := StrReplace(path2, "\build", "", 0)
; Then if an icon is found, save its path
Loop, Files, %path2%/*.ico
{
icon := path2 . "\" . A_LoopFileName
icon := StrReplace(icon, "\", "\\", 0)
break
}
; Then run cxfreeze and we're good
if (icon != "")
RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%" --icon "%icon%", , Hide
else
RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%", , Hide
TrayTip, Freezer, Python script "frozen" successfully