[texworks] Scripting: Open new file with a specified name

Paul A Norman paul.a.norman at gmail.com
Sat Nov 5 00:05:38 CET 2011

Hi Henrik,

Ok there is a more cumbersome 'standard' type approach below,

But if you want complete control form script (and permissions are set)...

User double clicks \input{filethatdoesnotexist.tex} any where {between} or
you get it with TW.target.balanceDelimiters(); then TW,target.selection and
remove the braces in script.

Use TW.target.fileName and extract your file path.

Build your full newFileName.

Send any contents straight to disk then open the file in an editor
window. Contents afaik can be an empty string.

Note warnings on html linked page below.   ;)  s.l.

var resp = TW.writeFile(QString filename, QString content);


var opendFileInfo = TW.app.openFileFromScript( dah dah)


Note that opendFileInfo is an object with a member result which points to a
target object for the newly opened file which you can use as if it were
TW.target  (see link above for more  details)

Hope this helps,

Paul -

 - a standard ( - low permissions settings) type approach below..

Standard type of route below will probably not be much use as you do not
have easy full control of the proposed file name in Script, you could build
it from the selection property, and the filepath of the current document,
and put it on the clipboard using the clipboard property, and then manually
paste it into the SaveAs dialogue box when it appears

 TW.app.newFile() will give you a new "target" window.document type object

 var xx = TW.app.newFile();



TW.information(null,"Test", xx.text);

//Then call


Or as it is a first save, just

xx.save() would works as saveAs()

As far as I know so far, you can not pass anything useful by way of a file
name to .saveAs()  -- .saveAs("myFile")   - string is ignored.

On 4 November 2011 23:38, Henrik Skov Midtiby <henrikmidtiby at gmail.com>wrote:

> Hi
> I would like to make a script that opens a new file and save it with a
> specified name.
> The following approach does not work
> TW.app.newFile(newfilename);
> The use case I wan't to handle is the following
> A new line with the following content have just been entered
> \input{filethatdoesnotexist.tex}
> By placing the cursor inside the curly brackets and invoking the
> script, texworks should create a new file with the name
> "filethatdoesnotexist.tex" in the same directory as the current file.
> Any hints?
> Best regards,
> Henrik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/texworks/attachments/20111105/95fc7adc/attachment.html>

More information about the texworks mailing list