Bridge: Text Preview palette script

  • 1
  • Idea
  • Updated 2 months ago
Bridge doesn't support previewing text files but it has the built-in ability to read them. So, I wrote a script to create a TabbedPalette that previews a selected text file.

The biggest problem with it is that Bridge is buggy when using a Tabbed Palette. Resizing the containing column is ugly. Even Adobe's sample Tabbed Palette code redraws wrong, so its a Bridge bug.

If you need to resize the column or browser window, click off and back on the TextPreview tab so it redraws.

Once you run the script, this palette will be in the Window menu if you close it.

To use this script, copy the text below and paste into a text editor. Save with a .jsx file extension. Select Edit->Preferences->Startup Scripts and click the Reveal button. Drag the saved .jsx file into that folder and relaunch Bridge. You'll have a new menu command Tools->Text Preview

---------------------------------------------------------------------------------------------

#target bridge

if(BridgeTalk.appName == 'bridge'){
    var newCommand = new MenuElement('command', 'Text Preview', '');
    }

newCommand.onSelect = function (){
    txtPreview();
    }

function txtPreview(){
    this.paletteRefs = new Array();
    var wrapper = this;
    function addtxtPalette(){
        scriptPalette = new TabbedPalette(app.document, "TextPreview", "textPre", "script");
        scriptPalette.setLocation('right');
        wrapper.paletteRefs.push(scriptPalette);   
        tbPanel = scriptPalette.content.add('panel', [10,10,350,450], '');
        tbPanel.txtField = tbPanel.add('edittext', [10,10,335,400], '', {multiline:true, readonly:true});
        }
    for(var i = 0;i < app.documents.length;i++){
        addtxtPalette(app.documents[i]);
    }
    app.eventHandlers.push({handler: openDocument});
}

 function openDocument(event){
    if(event.object instanceof Document && event.type == 'selectionsChanged'){
        try{
            if(app.document.selectionLength == 1){
                    thumbs = app.document.getSelection('txt, jsx, xml, xmp, html');
                    }
            else{
                tbPanel.txtField.text = 'Please select one text file.';
                thumbs = null;
                    }
            if(thumbs.length == 1){
                textFile = new File(thumbs[0].spec);
                textFile.open('r');
                var content = textFile.read();
                textFile.close();
                tbPanel.txtField.text = content;
                }
            else{
                tbPanel.txtField.text = 'Please select one text file.';
                }
            }
        catch(e){
            }
        }
    }

Photo of David Converse

David Converse

  • 361 Posts
  • 80 Reply Likes

Posted 2 months ago

  • 1
Photo of Kukurykus

Kukurykus

  • 317 Posts
  • 46 Reply Likes
It reads also .html, .jsx and .xml, but doesn't .log what you can fix. Window is too small, I mean there is small up / down scrollable txt window in palette that is much smaller than palette that is contined in. Can't be it fixed too? And I agree it's bad we have to switch palette labels to redraw resized TextPreview palette content.

I don't know how it could be used for your script additionally but maybe you can do something alike there already is for .pdf file. When you select some you can browse next pages in 'Preview' Tab.

If you would export .txt file to graphics file or directly .pdf, that could be resizable without distortion.
(Edited)
Photo of David Converse

David Converse

  • 361 Posts
  • 80 Reply Likes
I got it to work with auto layout. Much improved.
Photo of David Converse

David Converse

  • 361 Posts
  • 80 Reply Likes
New version using auto-layout. Much nicer, resizes with the panel. No ugly redraw.

-------------------------------------------------------------------------------------

#target bridge

if(BridgeTalk.appName == 'bridge'){
    var newCommand = new MenuElement('command', 'Text Preview', '');
    }

newCommand.onSelect = function (){
    txtPreview();
    }

function txtPreview(){
    try{
    this.paletteRefs = new Array();
    var wrapper = this;
    function addtxtPalette(){
        scriptPalette = new TabbedPalette(app.document, "Text Preview", "textPre", "script");
        scriptPalette.setLocation('right');
        scriptPalette.content.onResize = function(){
            var b = this.bounds;
            tbPanel.bounds = b;
            tbPanel.txtField.bounds = [b[0] -10, b[1] - 10, b[2] - 25, b[3] - 25];
            this.layout.resize(true);
            scriptPalette.content.layout.layout(true);
            }
        wrapper.paletteRefs.push(scriptPalette);
        tbPanel = scriptPalette.content.add('panel', undefined, '');
        tbPanel.alignment = ['fill', 'fill'];
        tbPanel.alignChildren = ['fill', 'fill'];
        tbPanel.txtField = tbPanel.add('edittext', undefined, 'Please select one text file.', {multiline:true, readonly:true});
        scriptPalette.content.layout.layout(true);
        }
    for(var i = 0;i < app.documents.length;i++){
        addtxtPalette(app.documents[i]);
    }
    app.eventHandlers.push({handler: openDocument});
    }
catch(e){
    }
}

 function openDocument(event){
    if(event.object instanceof Document && event.type == 'selectionsChanged'){
        try{
            if(app.document.selectionLength == 1){
                    thumbs = app.document.getSelection('txt, jsx, xml, xmp, html');
                    }
            else{
                tbPanel.txtField.text = 'Please select one text file.';
                thumbs = null;
                    }
            if(thumbs.length == 1){
                textFile = new File(thumbs[0].spec);
                textFile.open('r');
                var content = textFile.read();
                textFile.close();
                tbPanel.txtField.text = content;
                }
            else{
                tbPanel.txtField.text = 'Please select one text file.';
                }
            }
        catch(e){
            }
        }
    }

Photo of Nantucketbob

Nantucketbob

  • 9 Posts
  • 1 Reply Like
Very cool.  Interesting.  What language is this code?  May I ask why you want to read text files?  It never occurred to me to do that.
Photo of David Converse

David Converse

  • 346 Posts
  • 78 Reply Likes
This is Extendscript which is an offshoot of Javascript but specific to Adobe. This is one way of adding features to Photoshop, Bridge, InDesign, etc. Lightroom uses scripts written in Lua, a different language.

Reading and writing files is helpful if you want to save and use preferences. I have another script for exporting JPEG files (to replace a feature in previous versions of Bridge) and save settings this way.

I mainly wondered why image files are previewed but text files are not, given that Bridge has the capability already. This is a proof-of-concept plus its useful to me, making Bridge a bit more versatile file browser.
Photo of Nantucketbob

Nantucketbob

  • 9 Posts
  • 1 Reply Like
Thanks.  Appreciated.  Very interesting ideas.
Photo of David Converse

David Converse

  • 361 Posts
  • 80 Reply Likes
And its broken on the Mac- scrolling doesn't work. Bug filed. :(