Skip to content

Commit ec13f3c

Browse files
committed
Allow plists passed as script args to open on script start
1 parent e0db5a8 commit ec13f3c

File tree

2 files changed

+168
-17
lines changed

2 files changed

+168
-17
lines changed

ProperTree.bat

Lines changed: 149 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ setlocal enableDelayedExpansion
44
REM Setup initial vars
55
set "script_name=%~n0.command"
66
set "thisDir=%~dp0"
7+
set /a tried=0
8+
set "toask=yes"
79

10+
goto checkscript
11+
12+
:checkscript
813
REM Check for our script first
914
if not exist "!thisDir!\!script_name!" (
1015
echo Could not find !script_name!.
@@ -15,31 +20,162 @@ if not exist "!thisDir!\!script_name!" (
1520
pause > nul
1621
exit /b
1722
)
23+
goto checkpy
1824

25+
:checkpy
1926
REM Get python location
2027
FOR /F "tokens=* USEBACKQ" %%F IN (`where python 2^> nul`) DO (
2128
SET "python=%%F"
2229
)
2330

2431
REM Check for py and give helpful hints!
2532
if /i "!python!"=="" (
26-
echo Python is not installed or not found in your PATH var.
27-
echo Please install it from https://www.python.org/downloads/windows/
28-
echo.
29-
echo Make sure you check the box labeled:
30-
echo.
31-
echo "Add Python X.X to PATH"
32-
echo.
33-
echo Where X.X is the py version you're installing.
34-
echo.
35-
echo Press [enter] to quit.
36-
pause > nul
37-
exit /b
33+
if %tried% lss 1 (
34+
if /i "!toask!"=="yes" (
35+
REM Better ask permission first
36+
goto askinstall
37+
) else (
38+
goto installpy
39+
)
40+
) else (
41+
cls
42+
echo ### ###
43+
echo # Warning #
44+
echo ### ###
45+
REM Couldn't install for whatever reason - give the error message
46+
echo Python is not installed or not found in your PATH var.
47+
echo Please install it from https://www.python.org/downloads/windows/
48+
echo.
49+
echo Make sure you check the box labeled:
50+
echo.
51+
echo "Add Python X.X to PATH"
52+
echo.
53+
echo Where X.X is the py version you're installing.
54+
echo.
55+
echo Press [enter] to quit.
56+
pause > nul
57+
exit /b
58+
)
59+
)
60+
goto runscript
61+
62+
:askinstall
63+
cls
64+
echo ### ###
65+
echo # Python Not Found #
66+
echo ### ###
67+
echo.
68+
echo Python was not found on the system or in the PATH var.
69+
echo.
70+
set /p "menu=Would you like to install it now? [y/n]: "
71+
if /i "!menu!"=="y" (
72+
REM We got the OK - install it
73+
goto installpy
74+
) else if "!menu!"=="n" (
75+
REM No OK here...
76+
set /a tried=%tried%+1
77+
goto checkpy
78+
)
79+
REM Incorrect answer - go back
80+
goto askinstall
81+
82+
:installpy
83+
REM This will attempt to download and install python
84+
REM First we get the html for the python downloads page for Windows
85+
set /a tried=%tried%+1
86+
cls
87+
echo ### ###
88+
echo # Installing Python #
89+
echo ### ###
90+
echo.
91+
echo Gathering info from https://www.python.org/downloads/windows/...
92+
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')"
93+
if not exist "%TEMP%\pyurl.txt" (
94+
goto checkpy
3895
)
3996

97+
echo Parsing for latest...
98+
REM Got the file, let's parse it
99+
pushd "%TEMP%"
100+
set "release="
101+
for /F "tokens=*" %%x in (pyurl.txt) do (
102+
set "t=%%x"
103+
if /i not "%%x" == "" (
104+
if /i not "!t:Latest Python 3=!"=="!t!" (
105+
REM echo !t!
106+
set "release=!t!"
107+
)
108+
)
109+
)
110+
popd
111+
112+
REM Let's replace the " with ' and split the string by spaces
113+
REM to get the actual version number
114+
set "release=!release:"='!"
115+
for /F "tokens=8* delims= " %%x in ("!release!") do (
116+
set "release=%%x"
117+
)
118+
REM Once more - split at the < and get the first
119+
for /F "tokens=1* delims=<" %%x in ("!release!") do (
120+
set "release=%%x"
121+
)
122+
123+
echo Found Python !release! - Downloading...
124+
REM Let's delete our txt file now - we no longer need it
125+
del "%TEMP%\pyurl.txt"
126+
127+
REM At this point - we should have the version number.
128+
REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe"
129+
set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe"
130+
REM Now we download it with our slick powershell command
131+
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.exe')"
132+
REM If it doesn't exist - we bail
133+
if not exist "%TEMP%\pyinstall.exe" (
134+
goto checkpy
135+
)
136+
REM It should exist at this point - let's run it to install silently
137+
echo Installing...
138+
echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
139+
pushd "%TEMP%"
140+
pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
141+
popd
142+
echo Installer finsihed with %ERRORLEVEL% status.
143+
REM Now we should be able to delete the installer and check for py again
144+
del "%TEMP%\pyinstall.exe"
145+
REM If it worked, then we should have python in our PATH
146+
REM this does not get updated right away though - let's try
147+
REM manually updating the local PATH var
148+
set "spath="
149+
set "upath="
150+
for /f "tokens=2* delims= " %%i in ('reg.exe query "HKCU\Environment" /v "Path" 2^> nul') do (
151+
if NOT "%%j"=="" (
152+
set "upath=%%j"
153+
)
154+
)
155+
for /f "tokens=2* delims= " %%i in ('reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul') do (
156+
if NOT "%%j"=="" (
157+
set "spath=%%j"
158+
)
159+
)
160+
if not "!spath!" == "" (
161+
REM We got something in the system path
162+
set "PATH=!spath!"
163+
if not "!upath!" == "" (
164+
REM We also have something in the user path
165+
set "PATH=!PATH!;!upath!"
166+
)
167+
) else if not "!upath!" == "" (
168+
set "PATH=!upath!"
169+
)
170+
goto checkpy
171+
exit /b
172+
173+
:runscript
40174
REM Python found
175+
cls
41176
if "%*"=="" (
42177
"!python!" "!thisDir!!script_name!"
43178
) else (
44179
"!python!" "!thisDir!!script_name!" %*
45-
)
180+
)
181+
goto :EOF

ProperTree.command

100644100755
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ except:
1313
from Scripts import *
1414

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

125-
# create a fresh plist to start
126-
self.start_window = self.new_plist()
125+
if isinstance(plists, list) and len(plists):
126+
self.start_window = None
127+
# Iterate the passed plists and open them
128+
for p in plists:
129+
self.open_plist_with_path(None,p,None)
130+
else:
131+
# create a fresh plist to start
132+
self.start_window = self.new_plist()
127133

128134
# Start our run loop
129135
tk.mainloop()
@@ -335,6 +341,12 @@ class ProperTree:
335341
window.bell()
336342
mb.showerror("File Already Open", "{} is already open here.".format(path), parent=window)
337343
return
344+
self.open_plist_with_path(event,path,current_window)
345+
346+
def open_plist_with_path(self, event = None, path = None, current_window = None):
347+
if path == None:
348+
# Uh... wut?
349+
return
338350
# Let's try to load the plist
339351
try:
340352
with open(path,"rb") as f:
@@ -374,4 +386,7 @@ class ProperTree:
374386
self.tk.destroy()
375387

376388
if __name__ == '__main__':
377-
p = ProperTree()
389+
plists = []
390+
if len(sys.argv) > 1:
391+
plists = sys.argv[1:]
392+
p = ProperTree(plists)

0 commit comments

Comments
 (0)