Highlighted

How to I add a fail-safe to my renaming script if the user cancels at the text entry prompt?

Participant ,
Nov 13, 2020

Copy link to clipboard

Copied

I wrote a script that batch renames both the footage and the original file by adding a prefix, which the user is prompted for. The script then relinks the footage to the new file name. This works fine if it's used as aspected.

 

However, if the user cancels at the rename prompt, the items are renamed anyway but with a "null" prefix. How do I prevent this? I basically just want it to do nothing if they cancel.

 

var items = app.project.selection;

// Prompt for prefix
var prefix = prompt("Enter a prefix", "SH_20X_ST-XX-0XX_v0X");
         
// Rename Loop
for(var i = 0; i < items.length; i++) {
var myItem = items[i];
         
// Strip extension from old name
var oldName = myItem.name.substring(0, myItem.name.lastIndexOf("."));
         
var spacer = " - ";
var fileExt = ".swf";
         
var path = myItem.file.fsName;
         
// Strip filename from the path
var regEx = /[^\/]*$/;
var dirOnly = path.replace(regEx, '');
         
// Assemble new footage name
myItem.name = prefix+spacer+oldName+fileExt;
         
// Assemble new file name
var newName = myItem.name;
myItem.file.rename(newName);
         
// Replace missing footage
myItem.replace(File(dirOnly+newName));

 

Like this maybe:

var prefix = prompt("Enter a prefix", "SH_20X_ST-XX-0XX_v0X");
if (prefix){
	// do stuff
}

 

TOPICS
Scripting

Views

22

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to I add a fail-safe to my renaming script if the user cancels at the text entry prompt?

Participant ,
Nov 13, 2020

Copy link to clipboard

Copied

I wrote a script that batch renames both the footage and the original file by adding a prefix, which the user is prompted for. The script then relinks the footage to the new file name. This works fine if it's used as aspected.

 

However, if the user cancels at the rename prompt, the items are renamed anyway but with a "null" prefix. How do I prevent this? I basically just want it to do nothing if they cancel.

 

var items = app.project.selection;

// Prompt for prefix
var prefix = prompt("Enter a prefix", "SH_20X_ST-XX-0XX_v0X");
         
// Rename Loop
for(var i = 0; i < items.length; i++) {
var myItem = items[i];
         
// Strip extension from old name
var oldName = myItem.name.substring(0, myItem.name.lastIndexOf("."));
         
var spacer = " - ";
var fileExt = ".swf";
         
var path = myItem.file.fsName;
         
// Strip filename from the path
var regEx = /[^\/]*$/;
var dirOnly = path.replace(regEx, '');
         
// Assemble new footage name
myItem.name = prefix+spacer+oldName+fileExt;
         
// Assemble new file name
var newName = myItem.name;
myItem.file.rename(newName);
         
// Replace missing footage
myItem.replace(File(dirOnly+newName));

 

Like this maybe:

var prefix = prompt("Enter a prefix", "SH_20X_ST-XX-0XX_v0X");
if (prefix){
	// do stuff
}

 

TOPICS
Scripting

Views

23

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 13, 2020 0
Guide ,
Nov 13, 2020

Copy link to clipboard

Copied

Like this maybe:

var prefix = prompt("Enter a prefix", "SH_20X_ST-XX-0XX_v0X");
if (prefix){
	// do stuff
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 13, 2020 0
Participant ,
Nov 13, 2020

Copy link to clipboard

Copied

ah, so simple! Thanks Dan.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 13, 2020 0