Can You Dynamically Change the Name of an Object?

Actions you can use in your projects.

Can You Dynamically Change the Name of an Object?

Postby Clifton » Wed Oct 03, 2018 10:26 pm

Answer is YES!
However, please be aware that future references to that object should be made ONLY using PowerPac functions. Normal actions will probably not be able to find the object and they may appear orphaned by the actions system. PowerPac's functions can still find them using its internal object referencing engine.
    NOTE: It may be preferrable to use the PowerPac function userProperty() to create custom properties for objects. These properties can easily be set and checked and will not interfere with other actions that expressly manipulate the objects by name.
Also note that objects that are renamed, will not persist when navigating to another page in your book. You will have to rename them again with each visit to the page where they are used. This is because, the JavaScript that defines the objects is hardcoded with the original name.

Having said all of that, attached is an example of several objects being renamed and it shows that the PowerPac can still work with the renamed objects and style them or perform other tasks on those objects.

To build your own example, create a function in either XML or JavaScript format.
 
function tbfunction_renameTBO(curName, newName) {
if (newName.length > 25) return 'error'; //Arbitrate limit to new name length
newName = newName.replace(/ |\s/, ''); //Remove spaces & new lines
var obj = gTBo(curName, 'tbObj'); //Internal PowerPac function gets tbObj
if (obj) {
obj.name = newName;
} else return 'error'; //Cannot find object
return (obj.name == newName) ? 'success' : 'error';
};

/* USAGE:
var rtn = tbfunction_renameTBO(curName, newName);
@rtn = "error" or "success"
***/
Attachments
nameChanger.zip
ToolBook v10.5 file. Extract zip file and export and run in browser.
Ignore warnings about the TBK file having been created with a newer version of the PowerPac.
(32.95 KiB) Downloaded 1867 times
Clifton
Site Admin
 
Posts: 731
Joined: Tue Jan 14, 2014 1:04 am

Return to Action Editor Examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron