[texworks] Scripting root file behaviour

Thomas Fehige thomas_listen at fehige.de
Tue May 15 12:32:11 CEST 2012


Hello again,

here's my script to typeset a root document no matter which file I'm 
working on at the time. It is almost finished. The only thing I'd rather 
get rid of is the message window at the end of the script telling me 
that the result of the script is true. The problem is where I want to 
get the focus back to the window I've called the script from.

In the beginning I save that window with:

	var WorkTG = TW.target;

then do all the typesetting etc. and in the end if all is well I go:

	WorkTG.focus = true;

which does indeed shift the focus back but also causes the annoying (and 
illogical) message.

What's going on?

Thanks and best regards -- Thomas

-- 
Here's the whole script:
-----------------------------------
// TeXworksScript
// Title: Typeset Main File
// Description: Typeset the presently set main file
// Author: Thomas Fehige
// Version: 0.1
// Date: 2012-05-15
// Script-Type: standalone
// Context: TeXDocument
// Shortcut: Alt+M, Alt+M

if (TW.app.hasGlobal("main_file") == false){
	TW.app.setGlobal("main_file", "");
}

var WorkFN = TW.target.fileName;
var WorkTG = TW.target;

var MainFN = TW.app.getGlobal("main_file");

if (MainFN != ""){
// See if file is already open:
	var found = false;
	var windows = TW.app.getOpenWindows();
	for (var win in windows){
		if (windows[win].fileName == MainFN){
			found = true;
			windows[win].typeset()
			break;
		}
	}
// If it's not, try to open it:
	if (found == false){
		file_open_return = TW.app.openFileFromScript(MainFN, TW, -1, true);
		switch(file_open_return.status){
			case 0:
				found = true;
				file_open_return.result.typeset();
				break;
			case 2:
				var Dummy = TW.warning(null, "Access denied", "File Access denied");
				break;
			default:
				var Dummy = TW.warning(null, "Problem opening main file", "Main file 
could not be opened.");
				break;
		}
	}
// Give the focus back to the document we've been working on:
	if (found){
		WorkTG.focus = true;
	}
} else { // if the main file variable is empty, typeset the active document
	TW.target.typeset()
}


More information about the texworks mailing list