Skip to content

Commit

Permalink
Allow plists passed as script args to open on script start
Browse files Browse the repository at this point in the history
  • Loading branch information
corpnewt committed May 6, 2019
1 parent e0db5a8 commit ec13f3c
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 17 deletions.
162 changes: 149 additions & 13 deletions ProperTree.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ setlocal enableDelayedExpansion
REM Setup initial vars
set "script_name=%~n0.command"
set "thisDir=%~dp0"
set /a tried=0
set "toask=yes"

goto checkscript

:checkscript
REM Check for our script first
if not exist "!thisDir!\!script_name!" (
echo Could not find !script_name!.
Expand All @@ -15,31 +20,162 @@ if not exist "!thisDir!\!script_name!" (
pause > nul
exit /b
)
goto checkpy

:checkpy
REM Get python location
FOR /F "tokens=* USEBACKQ" %%F IN (`where python 2^> nul`) DO (
SET "python=%%F"
)

REM Check for py and give helpful hints!
if /i "!python!"=="" (
echo Python is not installed or not found in your PATH var.
echo Please install it from https://www.python.org/downloads/windows/
echo.
echo Make sure you check the box labeled:
echo.
echo "Add Python X.X to PATH"
echo.
echo Where X.X is the py version you're installing.
echo.
echo Press [enter] to quit.
pause > nul
exit /b
if %tried% lss 1 (
if /i "!toask!"=="yes" (
REM Better ask permission first
goto askinstall
) else (
goto installpy
)
) else (
cls
echo ### ###
echo # Warning #
echo ### ###
REM Couldn't install for whatever reason - give the error message
echo Python is not installed or not found in your PATH var.
echo Please install it from https://www.python.org/downloads/windows/
echo.
echo Make sure you check the box labeled:
echo.
echo "Add Python X.X to PATH"
echo.
echo Where X.X is the py version you're installing.
echo.
echo Press [enter] to quit.
pause > nul
exit /b
)
)
goto runscript

:askinstall
cls
echo ### ###
echo # Python Not Found #
echo ### ###
echo.
echo Python was not found on the system or in the PATH var.
echo.
set /p "menu=Would you like to install it now? [y/n]: "
if /i "!menu!"=="y" (
REM We got the OK - install it
goto installpy
) else if "!menu!"=="n" (
REM No OK here...
set /a tried=%tried%+1
goto checkpy
)
REM Incorrect answer - go back
goto askinstall

:installpy
REM This will attempt to download and install python
REM First we get the html for the python downloads page for Windows
set /a tried=%tried%+1
cls
echo ### ###
echo # Installing Python #
echo ### ###
echo.
echo Gathering info from https://www.python.org/downloads/windows/...
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')"
if not exist "%TEMP%\pyurl.txt" (
goto checkpy
)

echo Parsing for latest...
REM Got the file, let's parse it
pushd "%TEMP%"
set "release="
for /F "tokens=*" %%x in (pyurl.txt) do (
set "t=%%x"
if /i not "%%x" == "" (
if /i not "!t:Latest Python 3=!"=="!t!" (
REM echo !t!
set "release=!t!"
)
)
)
popd

REM Let's replace the " with ' and split the string by spaces
REM to get the actual version number
set "release=!release:"='!"
for /F "tokens=8* delims= " %%x in ("!release!") do (
set "release=%%x"
)
REM Once more - split at the < and get the first
for /F "tokens=1* delims=<" %%x in ("!release!") do (
set "release=%%x"
)

echo Found Python !release! - Downloading...
REM Let's delete our txt file now - we no longer need it
del "%TEMP%\pyurl.txt"

REM At this point - we should have the version number.
REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe"
set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe"
REM Now we download it with our slick powershell command
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.exe')"
REM If it doesn't exist - we bail
if not exist "%TEMP%\pyinstall.exe" (
goto checkpy
)
REM It should exist at this point - let's run it to install silently
echo Installing...
echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
pushd "%TEMP%"
pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
popd
echo Installer finsihed with %ERRORLEVEL% status.
REM Now we should be able to delete the installer and check for py again
del "%TEMP%\pyinstall.exe"
REM If it worked, then we should have python in our PATH
REM this does not get updated right away though - let's try
REM manually updating the local PATH var
set "spath="
set "upath="
for /f "tokens=2* delims= " %%i in ('reg.exe query "HKCU\Environment" /v "Path" 2^> nul') do (
if NOT "%%j"=="" (
set "upath=%%j"
)
)
for /f "tokens=2* delims= " %%i in ('reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul') do (
if NOT "%%j"=="" (
set "spath=%%j"
)
)
if not "!spath!" == "" (
REM We got something in the system path
set "PATH=!spath!"
if not "!upath!" == "" (
REM We also have something in the user path
set "PATH=!PATH!;!upath!"
)
) else if not "!upath!" == "" (
set "PATH=!upath!"
)
goto checkpy
exit /b

:runscript
REM Python found
cls
if "%*"=="" (
"!python!" "!thisDir!!script_name!"
) else (
"!python!" "!thisDir!!script_name!" %*
)
)
goto :EOF
23 changes: 19 additions & 4 deletions ProperTree.command
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ except:
from Scripts import *

class ProperTree:
def __init__(self):
def __init__(self, plists = []):
# Create the new tk object
self.tk = tk.Tk()
self.tk.title("Convert Values")
Expand Down Expand Up @@ -122,8 +122,14 @@ class ProperTree:
# Rewrite the default Command-Q command
self.tk.bind_all("<{}-q>".format(key), self.quit)

# create a fresh plist to start
self.start_window = self.new_plist()
if isinstance(plists, list) and len(plists):
self.start_window = None
# Iterate the passed plists and open them
for p in plists:
self.open_plist_with_path(None,p,None)
else:
# create a fresh plist to start
self.start_window = self.new_plist()

# Start our run loop
tk.mainloop()
Expand Down Expand Up @@ -335,6 +341,12 @@ class ProperTree:
window.bell()
mb.showerror("File Already Open", "{} is already open here.".format(path), parent=window)
return
self.open_plist_with_path(event,path,current_window)

def open_plist_with_path(self, event = None, path = None, current_window = None):
if path == None:
# Uh... wut?
return
# Let's try to load the plist
try:
with open(path,"rb") as f:
Expand Down Expand Up @@ -374,4 +386,7 @@ class ProperTree:
self.tk.destroy()

if __name__ == '__main__':
p = ProperTree()
plists = []
if len(sys.argv) > 1:
plists = sys.argv[1:]
p = ProperTree(plists)

0 comments on commit ec13f3c

Please sign in to comment.