[texworks] SCRIPTING: Get content of specified directory
Paul A Norman
paul.a.norman at gmail.com
Tue May 17 02:12:25 CEST 2011
Looking really awesome Henrik!
Haven't tried all scenarios yet, but really like the precision of
operation, and the clarity of coding - made it really easy for me to
try and get it running under Windows for your new feature - input of
filenames.
Often people have asked here whether there could be an automatic
completion of environments
\begin{something} sa a s a sad sad fd f
<contorl-key M>
\begin{ofsomething} sa a s a sad sad fd f
\end{ofsomething}
Is that something that you would see possibly logically fitting in
with your code at all?
With your new code, under Windows - I've tried these alterations, I
don't think they'd break the Linux version (?)
Quotes for possible spaces in file path (platform tested), and I've
found that for *some* Dos commands and utilities we need to absolutely
reverse the path delimiter to a backslash - fickle problem, and safer
to actually sample the output to determine whether \n or \r\n is doing
the line breaks and not assume it on platform.
Regards,
Paul
line 304(?)
function getListOfFilesInDir(directory)
{
if (TW.platform() == 'Windows')
{
var retVal = TW.system("cmd /c dir /b \"" +
directory.replace(/\//g,"\\") +"\"", true);
}
else
{
var retVal = TW.system("ls " + directory, true);
}
return retVal.output;
}
function typeOfLineBreak(sample) // Win OS with utilities called can
return either
{
if (sample.indexOf("\r\n") > -1)
{
return "\r\n";
}
return "\n";
}
function getMatchingFilenames(filenamesInDirectory, localPath, inputWord, words)
{
while(filenamesInDirectory.indexOf(typeOfLineBreak(filenamesInDirectory)) > -1)
{
...
On 17 May 2011 03:53, Henrik Skov Midtiby <henrikmidtiby at gmail.com> wrote:
> Hi Paul
>
> Thanks for your input.
>
> I now have autocompletion working for the macros \includegraphics,
> \input and \include based on the files present in the directory of the
> file open for editing.
> This part works on systems where the "ls" command is available and
> where the file path does not contain any spaces. The code have only
> been tested on linux at the moment.
>
> The result is uploaded at
> http://code.google.com/p/texworks/issues/detail?id=261#c60
> With the description
> ====================
> Context based autocompleter for completing words used in the open
> document, arguments to label and ref and as a new feature filename
> autocompletion for include, input and includegraphics commands.
>
> Guide to use.
> 1. start typing the long word that should be autocompleted
> 2. press "<crtl> + m" and the autocompleter will complete the word or
> at least make a suggestion for a completion.
> 3. if not satisfied with the suggestion press "<crtl> + m" until the
> correct word is found.
> ====================
>
> Best regards,
> Henrik Skov Midtiby
>
>
> On 15 May 2011 12:22, Paul A Norman <paul.a.norman at gmail.com> wrote:
>> Dear Henrik,
>>
>> Not so far as far as I know.
>>
>> As you have, I've been using system calls to things like "cmd /c
>> mkdir \""+ documentPath +"images\""
>> Can switch case on
>> http://twscript.paulanorman.com/docs/html/files/platform.html and
>> prepare differnt statements for the TW.system call.
>> http://twscript.paulanorman.com/docs/html/files/systemQString.html
>>
>> (see subheadngs in and under main headings "User 'Library' Modules and
>> helper objects" at
>> http://twscript.paulanorman.com/docs/html/TexWorksQtScripting.htm for
>> some explanation on twPan - recomend downloading from
>> http://twscript.paulanorman.com/docs/index.html and the .chm there.)
>>
>> and have been using things like ...
>>
>> function chooseFromDirectoryListing(Description, pathDir, fileExtension)
>> {
>> // pathDir (no trailing slash), fileExtension with . E.g. .txt
>>
>> // TW.app.clipboard = "cmd /c dir /b \""
>> + pathDir + "\\*" + fileExtension + "\"";
>>
>> var fileList = twPan.osCmd("cmd /c dir /b \""
>> + pathDir + "\\*" + fileExtension + "\""
>> , true)
>>
>> removeExtnsn = new RegExp(fileExtension,"ig");
>>
>> fileList = fileList.replace(removeExtnsn,"").split(lineBreak);
>>
>> // var chosenAnswer =
>> // twPan.select(Description, fileList);
>>
>> var chosenAnswer = TW.getItem(null, Description, Description, fileList);
>>
>> if (twPan.emptyString(chosenAnswer) == false)
>> {
>> chosenAnswer = chosenAnswer + fileExtension;
>> }
>>
>> return chosenAnswer;
>> }
>>
>> You can use
>>
>> http://twscript.paulanorman.com/docs/html/files/getOpenFileNames.html
>>
>> See also notes in
>> http://twscript.paulanorman.com/docs/html/files/getOpenFileNameQString.html
>>
>> Can get one or multi-select more file names though returns an array in
>> that case, requires User interaction though.
>>
>> Paul
>>
>>
>>
>> On 15 May 2011 21:26, Henrik Skov Midtiby <henrikmidtiby at gmail.com> wrote:
>>> Hi
>>>
>>> >From at script would I like to get access to the contents of the
>>> current directory.
>>> This information should be used for autocompleting filenames for
>>> includegraphics and input macros in latex.
>>>
>>> I know it is possible to access the commandline and issue an 'ls -all'
>>> or something similar, but is this information also available directly
>>> in QtScript?
>>>
>>> Best regards,
>>> Henrik Skov Midtiby
>>>
>>
>
>
More information about the texworks
mailing list