forked from loo2k/Sublime-Text-Portable-Tool
/
Sublime-Text-Portable-Tool.bat
177 lines (151 loc) · 5.36 KB
/
Sublime-Text-Portable-Tool.bat
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
@ECHO OFF
TITLE Sublime Text Portable Tool
SET PATH=%b2eincfilepath%;%PATH%
SET VERSION=v1.4.1
SET FILE_ICON_MENU=icon_menu_st.ico
SET FILE_ICON_ASSOCIATED=icon_associated_st.ico
SET FILE_ICON_EXECUTABLE=icon_executable_st.ico
SET FILE_SUBLIME_LAUNCHER=SublimeLauncher.exe
:menu
ECHO Sublime Text Portable Tool %VERSION% by Jack Cherng ^<jfcherng@gmail.com^>
ECHO ------------------------------------------------------------------------------
ECHO.
ECHO Operations:
ECHO 1: Add "Open with Sublime Text" to context menu (%FILE_ICON_MENU%)
ECHO 2: Remove "Open with Sublime Text" from context menu
ECHO 3: Add file associations (ext_st.txt, %FILE_ICON_ASSOCIATED%)
ECHO 4: Remove file associations
ECHO 5: Set Sublime Text as the default text editor (hijack notepad.exe)
ECHO 6: Restore notepad.exe as the default text editor
ECHO 7: Change the icon of sublime_text.exe (%FILE_ICON_EXECUTABLE%)
ECHO 8: Exit
ECHO.
ECHO Some notes:
ECHO 1. Put this .exe file with sublime_text.exe.
ECHO 2. Write file exetensions in ext_st.txt line by line
ECHO.
ECHO ------------------------------------------------------------------------------
ECHO.
:check_sublime_text_exist
IF EXIST "sublime_text.exe" (
GOTO prepareFiles
) ELSE (
ECHO Cannot find "sublime_text.exe"...
PAUSE >NUL
EXIT
)
:prepareFiles
FOR %%f IN (
"%FILE_ICON_ASSOCIATED%"
"%FILE_ICON_EXECUTABLE%"
"%FILE_ICON_MENU%"
"%FILE_SUBLIME_LAUNCHER%"
) DO (
IF NOT EXIST "%%f" copy "%b2eincfilepath%\%%f" . >NUL
)
GOTO begin
:begin
SET /p u="What are you going to do? "
IF "%u%" == "1" GOTO regMenu
IF "%u%" == "2" GOTO unregMenu
IF "%u%" == "3" GOTO sublime_text_file
IF "%u%" == "4" GOTO un_sublime_text_file
IF "%u%" == "5" GOTO set_sublime_default_editor
IF "%u%" == "6" GOTO unset_sublime_default_editor
IF "%u%" == "7" GOTO change_program_icon
IF "%u%" == "8" EXIT
GOTO begin
:regMenu
:: for files
reg add "HKCR\*\shell\Sublime Text" /ve /d "Open with Sublime Text" /f
reg add "HKCR\*\shell\Sublime Text" /v "Icon" /d "%CD%\%FILE_ICON_MENU%" /f
reg add "HKCR\*\shell\Sublime Text\command" /ve /d "%CD%\sublime_text.exe ""%%1""" /f
:: for directories
reg add "HKCR\Directory\shell\Sublime Text" /ve /d "Open with Sublime Text" /f
reg add "HKCR\Directory\shell\Sublime Text" /v "Icon" /d "%CD%\%FILE_ICON_MENU%" /f
reg add "HKCR\Directory\shell\Sublime Text\command" /ve /d "%CD%\subl.exe ""%%1""" /f
:: for directories background
reg add "HKCR\Directory\Background\shell\Sublime Text" /ve /d "Open with Sublime Text" /f
reg add "HKCR\Directory\Background\shell\Sublime Text" /v "Icon" /d "%CD%\%FILE_ICON_MENU%" /f
reg add "HKCR\Directory\Background\shell\Sublime Text\command" /ve /d "%CD%\subl.exe ""%%V""" /f
ECHO.
ECHO Done: add "Open with Sublime Text" to context menu
ECHO.
GOTO begin
:unregMenu
:: for files
reg delete "HKCR\*\shell\Sublime Text" /f
:: for directories
reg delete "HKCR\Directory\shell\Sublime Text" /f
:: for directories background
reg delete "HKCR\Directory\Background\shell\Sublime Text" /f
ECHO.
ECHO Done: remove "Open with Sublime Text" from context menu
ECHO.
GOTO begin
:sublime_text_file
IF NOT EXIST "ext_st.txt" (
ECHO.
ECHO Cannot find "ext_st.txt"...
ECHO.
GOTO begin
)
reg add "HKCR\sublime_text_file" /ve /d "Sublime Text file" /f
reg add "HKCR\sublime_text_file\DefaultIcon" /ve /d "%CD%\%FILE_ICON_ASSOCIATED%" /f
reg add "HKCR\sublime_text_file\shell\open\command" /ve /d "%CD%\sublime_text.exe ""%%1""" /f
FOR /F "eol=;" %%e IN (ext_st.txt) DO (
:: ECHO %%e
reg query "HKCR\.%%e" > NUL || reg add "HKCR\.%%e" /f
FOR /f "skip=2 tokens=1,2,* delims= " %%a IN ('reg query "HKCR\.%%e" /ve') DO (
IF NOT "%%c" == "sublime_text_file" (
reg add "HKCR\.%%e" /v "sublime_text_backup" /d "%%c" /f
)
)
assoc .%%e=sublime_text_file
)
ECHO.
ECHO Done: add file associations
ECHO.
GOTO begin
:un_sublime_text_file
IF NOT EXIST "ext_st.txt" (
ECHO.
ECHO Cannot find "ext_st.txt"...
ECHO.
GOTO begin
)
reg delete "HKCR\sublime_text_file" /f
FOR /F "eol=;" %%e IN (ext_st.txt) DO (
:: ECHO %%e
reg query "HKCR\.%%e" /v "sublime_text_backup" > NUL || reg add "HKCR\.%%e" /ve /f
FOR /f "skip=2 tokens=1,2,* delims= " %%a IN ('reg query "HKCR\.%%e" /v "sublime_text_backup"') DO (
reg add "HKCR\.%%e" /ve /d "%%c" /f
reg delete "HKCR\.%%e" /V "sublime_text_backup" /f
)
)
ECHO.
ECHO Done: remove file associations
ECHO.
GOTO begin
:set_sublime_default_editor
:: set self-executable to be the proxy (Debugger)
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /t REG_SZ /d "\"%CD%\SublimeLauncher.exe\" -z" /f
ECHO.
ECHO Done: set Sublime Text as the default text editor
ECHO.
GOTO begin
:unset_sublime_default_editor
reg delete "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /f
ECHO.
ECHO Done: use notepad.exe as the default text editor
ECHO.
GOTO begin
:change_program_icon
rcedit.exe "sublime_text.exe" --set-icon "%FILE_ICON_EXECUTABLE%"
:: try to clean icon cache
ie4uinit.exe -ClearIconCache 2>NUL
DEL /F /A %USERPROFILE%\AppData\Local\IconCache.db 2>NUL
ECHO.
ECHO Done: change the icon of sublime_text.exe
ECHO.
GOTO begin