Sublime Forum

Open pdf for latex files

#1

I’m writting a plugin that open the corresponding pdf file when a latex source file is opened. The code

import sublime, sublime_plugin,os,subprocess

class openpdf(sublime_plugin.EventListener):
	def on_load(self, view):
		fs=view.file_name()
		fsp=fs:-4]+".pdf"

		if fs-4:]=='.tex' and os.path.isfile(fsp):
			subprocess.Popen("\"F:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe\" -reuse-instance "+fsp+" -forward-search "+fs+" 1")

does that with sublime text 2 but with sublime text 3, it only opens the pdf when sublime’s already running, i.e. when I open sublime and open a latex file there, and not when I open a latex file from, say, windows file explorer. How to fix that? Thanks!

0 Likes

#2

If Sublime isn’t active - you have no access to the integrated plugins. Therefore it is impossible to open a file in Windows explorer and to wait that the ST plugin works.
If you enter as open command for latex in the Windows Registry ST3, it should work.
Or have I misunderstand your question?

0 Likes

#3

[quote=“BugFix”]If Sublime isn’t active - you have no access to the integrated plugins. Therefore it is impossible to open a file in Windows explorer and to wait that the ST plugin works.
If you enter as open command for latex in the Windows Registry ST3, it should work.
Or have I misunderstand your question?[/quote]

Yes, I enter as open command for latex in the Windows Registry. This exact code works in ST2 but not ST3.

0 Likes

#4

OK.
What happens exactly?

  • Windows explorer context menu, select open
  • opens ST3 now?
  • if yes, is the file loaded in the view?

Try step by step to find out where it hangs.

Use a little debug in your plugin:

[code]class openpdf(sublime_plugin.EventListener):
def on_load(self, view):
fs=view.file_name()
fsp=fs:-4]+".pdf"
# debug line:
print(‘file loaded’)

  if fs-4:]=='.tex' and os.path.isfile(fsp):
     # debug line:
     print('try to run SumatraPDF')
     subprocess.Popen("\"F:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe\" -reuse-instance "+fsp+" -forward-search "+fs+" 1")[/code]

Don’t forget to open the console, to see the debug output.

0 Likes