[texworks] Scripting Api TW.app.openFileFromScript()

Paul A Norman paul.a.norman at gmail.com
Sun Feb 13 04:10:14 CET 2011


Hi,

Just finally getting my head around TW.app.openFileFromScript()

Looking to write it up like this ... (below)

So far I can not discern fileStartPosition in (2) below doing anything
different to (1) - is it supposed to set where the position where the
file is read from? So far I always seem to end up with all of the file
even when I set say 200 for fileStartPosition.

Which is probably good as saving the document would overwrite the full
contents on disk.

If fileStartPosition were meant to read in only part of the file would
it be a good idea to actually read the contents into an unnamed or
copy-of named file instead?

Are there advantages in setting what I have as null to anything else -
is that needed for script directory files to be read without
permissions being set?

Paul
Proposed Write up:
--------------------------------------------------

1. TW.app.openFileFromScript(fullFileName : String , null)
2. TW.app.openFileFromScript(fullFileName : String,  null,
fileStartPosition : integer )
3. TW.app.openFileFromScript(fullFileName : String,  null,
fileStartPosition : integer, ask user - if file access permission is
not set: bool)

Opens a File in Tw for editing.

Will read inside script or document directory, other wise needs File
access permissions set, but can ask user on occasion if told to
(True/False) in the third version of calling the function

Required - fullFileName a fully/qualified/path to the file contents wanted.

The position held by null here is actually a scriptApiObj which may
have further relevance in Tw scripting later on.

Start position (-1 for all), in the second version of calling the
function, allows starting position of file that will be read in to be
set.

Returns a response object with members:

  "status" as for other file open functions  Return Values for Status

  "result"  is the opened Document's scripting object with all the
properties and functions of TW.target

Example:

   retVal = TW.openFileFromScript("c:/somewhere/Mydocument.txt", null,
-1, true);

 if  (retVal.status == 0) // SystemAccess_OK
      {
        var openedDocumentObject = retVal["result"] ;

       TW.information(null,"Tw Message",openedDocument.fileName); //
shows the opened document's base filename
       TW.information(null,"Tw Message",openedDocument.text); // shows
the read in contents of the "opened" document. Might not be full
contents of disk file,  if fileStartPosition is used.

     }


More information about the texworks mailing list