Guest User

Untitled

a guest
Apr 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. ;
  2. ; Opens Sublime Text 2 and makes the wanted project have focus
  3. ; If additional files are specified, they'll be opened in the specified project
  4. ; Useful for scripting, global hotkeys, command launchers, etc
  5. ;
  6. ; Example:
  7. ; openSublimeProject.ahk c:\sites\blog\blog.sublime-project c:\sites\blog\.gitignore c:\sites\blog\Gemfile
  8. ;
  9. ; Problems this script fixes:
  10. ; 1. If sublime is already open with the requested project, it creates an empty window and makes that active
  11. ; 2. If two projects or more are open, the requested project window does not receive focus
  12. ; 3. The most recently active window is the one to open a file, which may not be the requested project
  13. ; 4. Sometimes the project window isn't made active, a PX_TIMER_CLASS hidden window actually gets focus
  14. ;
  15. ; created by Noah Coad in Aug 2012
  16. ;
  17.  
  18. ; wlog("started " . A_Now)
  19.  
  20. ; required at least one parameter
  21. if (%0%==0)
  22. {
  23. MsgBox specify the Sublime project file to open on the command prompt
  24. ExitApp
  25. }
  26.  
  27. ; make sure first parameter is a sublime project and get the project file name
  28. FileName = %1%
  29.  
  30. ; obtain full long path to file, converts if in 8.3 short format or if relative path is specified
  31. Loop, %FileName%
  32. FileName = %A_LoopFileLongPath%
  33.  
  34. ; make sure this is a sublime project file
  35. FoundPos := RegExMatch(FileName,"^(?:.+\\){0,1}(.*)\.sublime-project$",proj)
  36.  
  37. ; let the user know if it isn't a sublime project
  38. if (FoundPos==0)
  39. {
  40. MsgBox needs to be a .sublime-project file on command line
  41. ExitApp
  42. }
  43.  
  44. ; pull the project name from the file name
  45. ; and set other variables used throughout the app
  46. ProjectName = %proj1%
  47. ProjReg = (\(?%ProjectName%\)? - Sublime Text 2)
  48.  
  49. ; find install path to sublime
  50. EnvGet, env_programs, ProgramFiles
  51. EnvGet, env_programs86, ProgramFiles(x86)
  52. EnvGet, env_programswow, ProgramW6432
  53. EnvGet, env_tools, tools
  54. 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")
  55. IfNotExist %SublimePath%
  56. {
  57. MsgBox could not locate sublime text
  58. ExitApp
  59. }
  60.  
  61. ; use regex to find windows
  62. SetTitleMatchMode RegEx
  63.  
  64. ; if sublime isn't running w the project alread open
  65. ; run sublime and activate the window
  66. ; maybe the last project used is the one wanted
  67. IfWinNotExist %ProjReg%
  68. {
  69. Run %SublimePath%
  70. }
  71. WinWait (Sublime Text 2)
  72.  
  73. ; now that sublime is running, see if it's the desired project
  74. ; if not, run sublime w the project to load it, activate that sublime
  75. IfWinNotExist %ProjReg%
  76. {
  77. ; wlog("running sublime: " . ProjReg)
  78. Run %SublimePath% --project "%FileName%"
  79. WinWait %ProjReg%
  80. }
  81. WinActivate
  82.  
  83. ; close empty instances of Sublime
  84. SetTitleMatchMode 2
  85.  
  86. loop {
  87. IfWinExist, untitled - Sublime Text 2 ahk_class PX_WINDOW_CLASS
  88. WinClose
  89. else
  90. break
  91. }
  92.  
  93. ; open any aditional files within the desired project's window
  94. params = %0%
  95. filecount := params - 1
  96. SetTitleMatchMode, RegEx
  97. if (filecount > 0)
  98. {
  99. Loop %filecount%
  100. {
  101. index := A_Index + 1
  102. GivenPath := %index%
  103. Loop %GivenPath%, 1
  104. LongPath = %A_LoopFileLongPath%
  105. Run %SublimePath% "%GivenPath%
  106. }
  107. WinActivate %ProjReg%
  108. }
  109.  
  110. ; Debugging log
  111. ; FileAppend, time out`n, c:\temp\log.txt
  112.  
  113. ; make sure the actual project window is active instead of the Sublime timer
  114. DetectHiddenWindows On
  115. loop {
  116. SetTitleMatchMode 2
  117. WinWaitActive ahk_class PX_TIMER_CLASS,,3
  118. if ErrorLevel
  119. break
  120. else
  121. {
  122. SetTitleMatchMode RegEx
  123. WinActivate %ProjReg%
  124. }
  125. }
  126.  
  127. exit
  128.  
  129. wlog(_what)
  130. {
  131. _file := temp "\openSublimeProject.log"
  132. FileAppend,%_what% `n, %_file%
  133. }
  134.  
  135. ; checks a list of paths seperated by semicolon to see if one exists and returns it
  136. LocateExists(_locations)
  137. {
  138. ; try each item in the list
  139. Loop, parse, _locations, `;
  140. IfExist %A_LoopField%
  141. return %A_LoopField%
  142. }
Add Comment
Please, Sign In to add comment