Skip to main content
Participating Frequently
July 13, 2023
Question

Swap single line of text across 70 projects

  • July 13, 2023
  • 3 replies
  • 559 views

I'm working on a large set of display ads in various sizes. Say I wanted to swap the text "6 months" to say "3 months" across all projects, is there a way to do that dynamically, rather than having to open up and manually revise, publish, zip every single project? Perhaps a variable data field that I can access and replace in visual studio code?

 

Thanks

    This topic has been closed for replies.

    3 replies

    Legend
    July 14, 2023

    Anyone who's reusing the same text across that many different FLAs should really, really be loading it from a common external file.

    Participating Frequently
    July 17, 2023

    I'm listening! Have any links to information explaining this process? I'm creating HTML5 ads, I suppose the loading method you mention works with this?

    JoãoCésar17023019
    Community Expert
    Community Expert
    July 13, 2023

    Hi.

     

    Here is a script that is supposed to open multiple FLAs and replace whatever text occurrencies it finds in each document.

     

    Some notes:

    - Please test it on some dummy files first;

    - This script may work better if you run it from within a FLA file and not when viewing or editing the JSFL file.

     

    Please feel free to improve or fix it in any way needed.

     

    Replace Text in Multiple FLAs.JSFL script:

     

    var dom, dialog;
    
    function main()
    {	
    	var ui =
    	[
    		"<dialog title='Replace Text in Multiple FLAs' buttons='accept, cancel'>",
    			"<hbox>",
    				"<label value='Find:'/>",
    				"<textbox id='find' value='' width='200'/>",
    			"</hbox>",
    			"<hbox>",
    				"<label value='Replace:'/>",
    				"<textbox id='replace' value='' width='200'/>",
    			"</hbox>",
    			"<checkbox id='matchCase' label='Match case' align='left' checked='false'/>",
    			"<checkbox id='wholeWord' label='Whole word' align='left' checked='false'/>",
    			"<checkbox id='close' label='Close FLAs' align='left' checked='true'/>",
    			"<checkbox id='promptToClose' label='Prompt to close if needed' align='left' checked='false'/>",
    			"<checkbox id='logResults' label='Log results' align='left' checked='true'/>",
    			"<checkbox id='save' label='Save' align='left' checked='true'/>",
    			"<spacer/>",
    		"</dialog>"
    	];
    
    	dialog = fl.xmlPanelFromString(ui.join(""));
    		
    	if (dialog.dismiss === "accept" && dialog.find !== "")
    		openFiles();
    }
    
    function openFiles()
    {
    	var uri = fl.browseForFolderURL("select", "Select the folder containing the FLAs.");		
    	var files = FLfile.listFolder(uri + "/*.fla", "Files.");
    	
    	if (files.length === 0)
    		return;
    	
    	try
    	{
    		var i;
    		
    		for (i = 0, total = files.length; i < total; i++)
    		{
    			fl.openDocument(uri + "/" + files[i]);
    			dom = fl.getDocumentDOM();
    			findAndReplace();
    			
    			if (dialog.save === "true")
    				dom.save();
    						
    			if (dialog.close === "true")
    				dom.close(dialog.promptToClose === "true");
    		}
    	}
    	catch(error)
    	{
    		if (dialog.logResults === "true")
    		{
    			fl.trace(files[i]);
    			fl.trace(error);
    			fl.trace("___________");
    		}
    	}
    }
    
    function findAndReplace()
    {
    	var results = fl.findObjectInDocByType("text", dom);
    	var total = results.length;
    
    	if (total > 0)
    	{
    		var pattern = dialog.wholeWord === "true" ? "\\b" + dialog.find + "\\b" : dialog.find;
    		var modifiers = dialog.matchCase === "true" ? "g" : "ig";
    		var regEx = new RegExp(pattern, modifiers);
    		var i, obj;
    		
    		for (i = 0; i < total; i++)
    		{
    			obj = results[i].obj;
    			obj.setTextString(obj.getTextString().replace(regEx, dialog.replace));
    		}
    
    		if (dialog.logResults === "true")
    		{
    			fl.trace(dom.name + ": Found " + total + " text field(s).");
    			fl.trace("");
    		}
    	}
    	else
    	{
    		if (dialog.logResults === "true")
    			fl.trace("Failed to find text field(s) in " + dom.name + ".");
    	}
    }
    
    main();

     

     

    Source / script / download:

    https://bit.ly/3pKlPJX

     

    I hope it helps.

     

    Regards,

    JC

    Participating Frequently
    July 14, 2023

    Will this script be any different the opening all of the project files and using the native Find and Replace tool inside of Animate? 

    JoãoCésar17023019
    Community Expert
    Community Expert
    July 14, 2023

    The difference is that the script will open all files, replace all the occurrences, save, and close.

    kglad
    Community Expert
    Community Expert
    July 13, 2023

    with jsfl and it's probably possible, but unless there are scores of files it's probably not worth it.

    Participating Frequently
    July 13, 2023

    Currently 70 projects to revise. Plus, these display ads are an ongoing thing at my work. We will launch a campaign that uses the same visual theme across all the ads but include a different price offer for areas with different pricing. A solution to this would save a ton of time. I'll look into jsfl, thanks.

    kglad
    Community Expert
    Community Expert
    July 13, 2023

    you're welcome.