Bridge: document.refresh not working in script

  • 1
  • Problem
  • Updated 2 months ago
I have a Bridge script to automate renaming files. I photograph products tethered with four variants, all using the same base filename with a variant letter. Filenames are always all caps (company naming style.)

Example: ABC111-A, ABC111-B, ABC111-PA

This meant typing in the same base name four times with the variant letter (I always name the first file when initially captured.) So I wrote a script to speed this up and it works great. Except. I might have stumbled across a display bug.

I'm trying to avoid switching back and forth with caps lock (leave it off) so I can also answer emails. Originally I took the input, converted to uppercase, and changed the filename. But that left the first file lowercase unless I remembered to switch.

So, fine, I'll just convert renamed files to uppercase. But I can't get the Bridge browser window to refresh. Synchronous mode doesn't help. If I manually refresh with F5, the extra files go away. I've tried moving the document.refresh statement inside the Try block, with and without synchronous mode, document.refresh before exiting synchronous mode, etc. No luck.

See screen captures. I have not tested this on the Mac yet.







Script follows:

#target bridge
if(BridgeTalk.appName == 'bridge'){
    FT = MenuElement.create('command', 'Rename', 'at the end of Tools');
    FC = MenuElement.create('command', 'Rename', 'after Thumbnail/Open', this.menuID);
    }

FT.onSelect = function(){
    Renamer();
    }

FC.onSelect = function(){
    Renamer();
    }

function Renamer(){
    var thumbs = app.document.selections;
    var newName = '';
    var Title = [];
    var counter = 0;
    var variant = ['-PA', '-PB'];
    if(thumbs.length != 4) return;
    app.synchronousMode = true;
    try{
        newName = prompt('Enter part number');
        for (var a in thumbs){
            var selectedFile = thumbs[a].spec;
            Title[a] = selectedFile.name;
            Title[a] = (Title[a].split( '.' ))[0];
            if(Title[a].substr(0, 4) == 'IMG5'){
                thumbs[a].name = newName + '-B.CR2';
                }
            if(Title[a].substr(0,4) == 'IMG_'){
                thumbs[a].name = newName + variant[counter] + '.CR2';
                counter = counter + 1;
                }
            thumbs[a].name = thumbs[a].name.toUpperCase();
            }
        }
    catch(e){
        }
    app.synchronousMode = false;
    app.document.refresh();
    }
Photo of David Converse

David Converse

  • 303 Posts
  • 67 Reply Likes
  • like it should work.

Posted 2 months ago

  • 1
Photo of David Converse

David Converse

  • 303 Posts
  • 67 Reply Likes
Forgot to mention, the file count is wrong too. So its not just a display issue. Bridge thinks the lowercase and uppercase are separate files. (Look at the file count in the lower left corner. It should be four, not seven. Refreshing with F5 changes the count to four.)

Photo of David Converse

David Converse

  • 303 Posts
  • 67 Reply Likes
And a bit easier script to test with. Select an arbitrary number of files and type a test name into the dialog.

#target bridge
if(BridgeTalk.appName == 'bridge'){
    FT2 = MenuElement.create('command', 'Rename2', 'at the end of Tools');
    FC2 = MenuElement.create('command', 'Rename2', 'after Thumbnail/Open', this.menuID);
    }

FT2.onSelect = function(){
    Renamer2();
    }

FC2.onSelect = function(){
    Renamer2();
    }

function Renamer2(){
    app.synchronousMode = true;
    var thumbs = app.document.selections;
    var newName = '';
    var counter = 0;
    try{
        newName = prompt('Enter part number');
        for (var a in thumbs){
            thumbs[a].name = newName + counter + '.CR2';
            counter = counter + 1;
            thumbs[a].name = thumbs[a].name.toUpperCase();
            }
        }
    catch(e){
        }
    app.document.refresh();
    app.synchronousMode = false;
    }
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 142 Reply Likes
Have you tried something like this...

function Renamer2(){
    app.synchronousMode = true;
    var thumbs = app.document.selections;
    var newName = '';
    var counter = 0;
    try{
        newName = prompt('Enter part number');
        for (var a in thumbs){
            newFileName =  newName + counter + '.CR2';
            counter++;
            thumbs[a].spec.rename(newFileName.toUpperCase());
            newFileName="";
            }
        }
    catch(e){
        }
    app.document.refresh();
    app.synchronousMode = false;
    }
Photo of David Converse

David Converse

  • 301 Posts
  • 67 Reply Likes
I had some problems in the original with splitting the filename which is why I used .spec in one my identification logic. I hadn't tested with writing to it.

Still seems like an odd bug.
Photo of David Converse

David Converse

  • 301 Posts
  • 67 Reply Likes
The original logic was:

function Renamer2(){
    app.synchronousMode = true;
    var thumbs = app.document.selections;
    var newName = '';
    var newNameU = '';
    var counter = 0;
    try{
        newName = prompt('Enter part number');
        if(newName != null){
            newNameU = newName.toUpperCase();
            for (var a in thumbs){
                thumbs[a].name = newNameU + counter + '.CR2';
                counter = counter + 1;
                }
            }
        }
    catch(e){
        }
    app.document.refresh();
    app.synchronousMode = false;
    }
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 142 Reply Likes
I think the problem is related to changing the thumbs name?
Docs say:-
name String  The label displayed for the thumbnail. Read/write.
Default is the  path value.
Nothing about file renaming, that's why I suggested using rename David.
Photo of David Converse

David Converse

  • 303 Posts
  • 67 Reply Likes
Yeah that could be it, that its the wrong value to change. You'd think they wouldn't make it work though? The operating system sees it as a valid filename change.
Photo of Avinash Kumar Singh

Avinash Kumar Singh, Employee

  • 26 Posts
  • 7 Reply Likes
Hi ,

Instead  of app.document.refresh(); try 
app.document.chooseMenuItem("Refresh");

Here is the final script and it worked for me on Win 10 also the file count is getting correct 

Thanks
Avinash 


Code :

#target bridge-8
if(BridgeTalk.appName == 'bridge'){
    FT = MenuElement.create('command', 'Rename', 'at the end of Tools');
    FC = MenuElement.create('command', 'Rename', 'after Thumbnail/Open', this.menuID);
    }

FT.onSelect = function(){
    Renamer();
    }

FC.onSelect = function(){
    Renamer();
    }

function Renamer(){
    var thumbs = app.document.selections;
    var newName = '';
    var Title = [];
    var counter = 0;
    var variant = ['-PA', '-PB'];
    if(thumbs.length != 4) {
    return;
    }
    app.synchronousMode = true;
    try{
        newName = prompt('Enter part number');
        for (var a in thumbs){
            var selectedFile = thumbs[a].spec;
            Title[a] = selectedFile.name;
            Title[a] = (Title[a].split( '.' ))[0];
            if(Title[a].substr(0, 4) == 'IMG5'){
                thumbs[a].name = newName + '-B.CR2';
                }
            if(Title[a].substr(0,4) == 'IMG_'){
                thumbs[a].name = newName + variant[counter] + '.CR2';
                counter = counter + 1;
                }
            thumbs[a].name = thumbs[a].name.toUpperCase();
            }
        }
    catch(e){
        }
    app.synchronousMode = false;
    app.document.chooseMenuItem("Refresh");



    }
Photo of David Converse

David Converse

  • 303 Posts
  • 67 Reply Likes
Any idea why the browser does this? And I guess this can be added to the list of ExtendScript bugs, if a documented call doesn't work.

Luckily this wasn't a showstopper as it was easy to work around, I just wanted the devs to know about it.
Photo of Avinash Kumar Singh

Avinash Kumar Singh, Employee

  • 26 Posts
  • 7 Reply Likes
Thanks For you feedback . We will check this with Dev team.