Skip to main content
Participating Frequently
May 17, 2022
Answered

Problem renaming a file with a © symbol

  • May 17, 2022
  • 2 replies
  • 794 views

The version of Bridge that I'm using has a bug that reverses the order of the images when I import them.  I'm trying to write a extendscript to reverse the order of the images by renaming them.  For example if I import five images, image one needs to be renamed image five and image two needs to be renamed image four etc.  The problen seems to be my standard filenaming format:  ©_Stan_Tess_20220516_0005.ARW.  If I remove the © symbol I can rename the file but not with the symbol.  If I use Batch Rename... within Bridge I can add the © symbol but I would like to do it in the script.  Thanks!

 

#target bridge
if(BridgeTalk.appName == 'bridge'){
try{


//create menu

var cpPath = MenuElement.create('command', 'Image Order Fix', 'at the end of Tools');

var ftcpPath = MenuElement.create('command', 'Image Order Fix', 'after Thumbnail/Open', this.menuID); //add to Contextual menu


cpPath.onSelect = function(){
ImageOrderFix();
}

ftcpPath.onSelect = function(){
ImageOrderFix();
}

function ImageOrderFix(){

var thumbs = app.document.selections;
var selectedFile = new Thumbnail(thumbs[0]);

MyFolder = decodeURI(selectedFile.spec.parent.toString());

var folder = Folder(MyFolder);


var files = folder.getFiles();
var file, extension, baseName, rename;

for(var i = 1; i < files.length; i++){
file= files[i];
extension = files[i].name.slice(files[i].name.length-4, files[i].length);
baseName =files[i].name.slice(6, files[i].name.length-8);
rename= ((files.length) -i ).toString();

switch(rename.length.toString() ) {
case "1":
rename= "000" + rename
break;
case "2":
rename= "00" + rename
break;
case "3":
rename= "0" + rename
break;
case "4":
//~ rename = rename
break;

}

rename = baseName +rename +extension

//~ file.rename(rename);
file.rename( "©" + rename);
//~ file.rename( String.fromCharCode(169) +rename);
}


}
}
catch(e){
alert(e + ' ' + e.line);
}
}

This topic has been closed for replies.
Correct answer gregreser

That is bizarre.  I just tried removing "_Stan_" from baseName and then using file.rename( "©_Stan_"+rename), but that doesn't work either. I also tried adding  a second _ to file.rename( "©__Stan_"+rename) and that works but it's not in my naming convention.  Then I tried file.rename( "b©_Stan_" + rename) and that works as well and so does file.rename( "©Stan_" + rename).   I then added "Stan_" back to baseName and then used file.rename( "©\_" + rename) Which works but screwed up my i incrementor.  I'll see if I can figure that out in the morning.  It seems the problem is the "©_"  combination and a way to escape it.


There's definitely something weird with "©_" on Windows.


This is a workaround so I'm not confident recommending it, but I was able to rename using "©X_"  and then rename again removing the "X". 

Also, I had to change your loop to begin at 0 to get all the files to rename:

for(var i = 0; i < files.length; i++){ 

 

#target bridge
if(BridgeTalk.appName == 'bridge'){
try{


//create menu

var cpPath = MenuElement.create('command', 'Image Order Fix', 'at the end of Tools');

var ftcpPath = MenuElement.create('command', 'Image Order Fix', 'after Thumbnail/Open', this.menuID); //add to Contextual menu


cpPath.onSelect = function(){
ImageOrderFix();
}

ftcpPath.onSelect = function(){
ImageOrderFix();
}

function ImageOrderFix(){

var thumbs = app.document.selections;
var selectedFile = new Thumbnail(thumbs[0]);

MyFolder = decodeURI(selectedFile.spec.parent.toString());

var folder = Folder(MyFolder);

var files = folder.getFiles();
var file, extension, baseName, rename;

for(var i = 0; i < files.length; i++){
file= files[i];
// ©_Stan_Tess_20220516_0005
// %C2%A9_Stan_Tess_20220516_0005.jpg

extension = files[i].name.slice(files[i].name.length-4, files[i].length);
baseName =files[i].name.slice(6, files[i].name.length-8);
rename= ((files.length) -i ).toString();

switch(rename.length.toString() ) {
case "1":
rename= "000" + rename
break;
case "2":
rename= "00" + rename
break;
case "3":
rename= "0" + rename
break;
case "4":
//~ rename = rename
break;
}

rename = baseName +rename +extension
//~ file.rename(rename);
file.rename( "©X"+rename);
//~ file.rename( String.fromCharCode(169) +rename);
}

for(var ii = 0; ii < files.length; ii++){
    file= files[ii];
    rename2 = files[ii].name.split("X_").join("_")
    file.rename(rename2);
    }

}
}
catch(e){
alert(e + ' ' + e.line);
}
}

 

2 replies

stantessAuthor
Participating Frequently
May 18, 2022

Hi, Thanks, I missed that one in my search but unfortunately "\xa9" doesn't work either.  I had previously tried String.fromCharCode(169)  and  "\©" before but they also didn't work.

Stephen Marsh
Braniac
May 18, 2022

Are you using Windows?

 

I'm on a Mac and the © symbol worked for me in Bridge 2022.

gregreser
gregreserCorrect answer
Braniac
May 18, 2022

That is bizarre.  I just tried removing "_Stan_" from baseName and then using file.rename( "©_Stan_"+rename), but that doesn't work either. I also tried adding  a second _ to file.rename( "©__Stan_"+rename) and that works but it's not in my naming convention.  Then I tried file.rename( "b©_Stan_" + rename) and that works as well and so does file.rename( "©Stan_" + rename).   I then added "Stan_" back to baseName and then used file.rename( "©\_" + rename) Which works but screwed up my i incrementor.  I'll see if I can figure that out in the morning.  It seems the problem is the "©_"  combination and a way to escape it.


There's definitely something weird with "©_" on Windows.


This is a workaround so I'm not confident recommending it, but I was able to rename using "©X_"  and then rename again removing the "X". 

Also, I had to change your loop to begin at 0 to get all the files to rename:

for(var i = 0; i < files.length; i++){ 

 

#target bridge
if(BridgeTalk.appName == 'bridge'){
try{


//create menu

var cpPath = MenuElement.create('command', 'Image Order Fix', 'at the end of Tools');

var ftcpPath = MenuElement.create('command', 'Image Order Fix', 'after Thumbnail/Open', this.menuID); //add to Contextual menu


cpPath.onSelect = function(){
ImageOrderFix();
}

ftcpPath.onSelect = function(){
ImageOrderFix();
}

function ImageOrderFix(){

var thumbs = app.document.selections;
var selectedFile = new Thumbnail(thumbs[0]);

MyFolder = decodeURI(selectedFile.spec.parent.toString());

var folder = Folder(MyFolder);

var files = folder.getFiles();
var file, extension, baseName, rename;

for(var i = 0; i < files.length; i++){
file= files[i];
// ©_Stan_Tess_20220516_0005
// %C2%A9_Stan_Tess_20220516_0005.jpg

extension = files[i].name.slice(files[i].name.length-4, files[i].length);
baseName =files[i].name.slice(6, files[i].name.length-8);
rename= ((files.length) -i ).toString();

switch(rename.length.toString() ) {
case "1":
rename= "000" + rename
break;
case "2":
rename= "00" + rename
break;
case "3":
rename= "0" + rename
break;
case "4":
//~ rename = rename
break;
}

rename = baseName +rename +extension
//~ file.rename(rename);
file.rename( "©X"+rename);
//~ file.rename( String.fromCharCode(169) +rename);
}

for(var ii = 0; ii < files.length; ii++){
    file= files[ii];
    rename2 = files[ii].name.split("X_").join("_")
    file.rename(rename2);
    }

}
}
catch(e){
alert(e + ' ' + e.line);
}
}

 

Stephen Marsh
Braniac
May 17, 2022

I haven't had time to test, however, I'd suggest that you try to use the code instead:

 

\xA9

 

https://charbase.com/00a9-unicode-copyright-sign