Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Fixes Sublime Text 2 open project odd behavior on Windows by making sure the selected project receives focus. Will also open files in a specific project window.
;
; Opens Sublime Text 2 and makes the wanted project have focus
; If additional files are specified, they'll be opened in the specified project
; Useful for scripting, global hotkeys, command launchers, etc
;
; Example:
; openSublimeProject.ahk c:\sites\blog\blog.sublime-project c:\sites\blog\.gitignore c:\sites\blog\Gemfile
;
; Problems this script fixes:
; 1. If sublime is already open with the requested project, it creates an empty window and makes that active
; 2. If two projects or more are open, the requested project window does not receive focus
; 3. The most recently active window is the one to open a file, which may not be the requested project
; 4. Sometimes the project window isn't made active, a PX_TIMER_CLASS hidden window actually gets focus
;
; created by Noah Coad in Aug 2012
;
; wlog("started " . A_Now)
; required at least one parameter
if (%0%==0)
{
MsgBox specify the Sublime project file to open on the command prompt
ExitApp
}
; make sure first parameter is a sublime project and get the project file name
FileName = %1%
; obtain full long path to file, converts if in 8.3 short format or if relative path is specified
Loop, %FileName%
FileName = %A_LoopFileLongPath%
; make sure this is a sublime project file
FoundPos := RegExMatch(FileName,"^(?:.+\\){0,1}(.*)\.sublime-project$",proj)
; let the user know if it isn't a sublime project
if (FoundPos==0)
{
MsgBox needs to be a .sublime-project file on command line
ExitApp
}
; pull the project name from the file name
; and set other variables used throughout the app
ProjectName = %proj1%
ProjReg = (\(?%ProjectName%\)? - Sublime Text 2)
; find install path to sublime
EnvGet, env_programs, ProgramFiles
EnvGet, env_programs86, ProgramFiles(x86)
EnvGet, env_programswow, ProgramW6432
EnvGet, env_tools, tools
SublimePath := LocateExists(env_programs "\Sublime Text 2\sublime_text.exe;" env_programs86 "\Sublime Text 2\sublime_text.exe;" env_programswow "\Sublime Text 2\sublime_text.exe;" env_programs "\Sublime Text 3\sublime_text.exe;" env_programs86 "\Sublime Text 3\sublime_text.exe;" env_programswow "\Sublime Text 3\sublime_text.exe;" env_tools "\apps\SublimeText\sublime_text.exe")
IfNotExist %SublimePath%
{
MsgBox could not locate sublime text
ExitApp
}
; use regex to find windows
SetTitleMatchMode RegEx
; if sublime isn't running w the project alread open
; run sublime and activate the window
; maybe the last project used is the one wanted
IfWinNotExist %ProjReg%
{
Run %SublimePath%
}
WinWait (Sublime Text 2)
; now that sublime is running, see if it's the desired project
; if not, run sublime w the project to load it, activate that sublime
IfWinNotExist %ProjReg%
{
; wlog("running sublime: " . ProjReg)
Run %SublimePath% --project "%FileName%"
WinWait %ProjReg%
}
WinActivate
; close empty instances of Sublime
SetTitleMatchMode 2
loop {
IfWinExist, untitled - Sublime Text 2 ahk_class PX_WINDOW_CLASS
WinClose
else
break
}
; open any aditional files within the desired project's window
params = %0%
filecount := params - 1
SetTitleMatchMode, RegEx
if (filecount > 0)
{
Loop %filecount%
{
index := A_Index + 1
GivenPath := %index%
Loop %GivenPath%, 1
LongPath = %A_LoopFileLongPath%
Run %SublimePath% "%GivenPath%
}
WinActivate %ProjReg%
}
; Debugging log
; FileAppend, time out`n, c:\temp\log.txt
; make sure the actual project window is active instead of the Sublime timer
DetectHiddenWindows On
loop {
SetTitleMatchMode 2
WinWaitActive ahk_class PX_TIMER_CLASS,,3
if ErrorLevel
break
else
{
SetTitleMatchMode RegEx
WinActivate %ProjReg%
}
}
exit
wlog(_what)
{
_file := temp "\openSublimeProject.log"
FileAppend,%_what% `n, %_file%
}
; checks a list of paths seperated by semicolon to see if one exists and returns it
LocateExists(_locations)
{
; try each item in the list
Loop, parse, _locations, `;
IfExist %A_LoopField%
return %A_LoopField%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment