Skip to main content
Participant
September 20, 2024
Question

Is there a script to run a bulk import of variables from a text file?

  • September 20, 2024
  • 2 replies
  • 249 views

Hello FrameMaker experts!

I'm trying to consolidate multiple documents into FrameMaker for translation purposes. The document has several text strings that would be easier to maintain/validate as variables. I was wondering if anyone found or created a script they'd be willing to share. I started this project in InDesign and have a script for that program, but it doesn't work in FrameMaker. I am no coding expert, but I didn't expect it to work.

Someone else posted about this a few years ago: https://community.adobe.com/t5/framemaker-discussions/script-for-importing-variables/m-p/13121274 with a link to a Swedish company. I will try translating that as a last resort.

Here is the link for the script that works in InDesign: https://gist.github.com/circleb/e87421b163479aa38d495163ba6ad76d and the code is as follows:

 

Any help would be appreciated!!

Thanks!

Wendy

/* ***************************************************************** */
/* Most of the code came from the InDesign script FindChangeByList.jsx
/* and the function setCustomTextVariable() by Jon S. Winters.
/* 
/* DESCRIPTION
/* This script allows you to import a simple text file containing a
/* text variable list and registers them as "Custom Text Variables,"
/* this script doesn't work for any other kind of InDesign variable.
/* It will update any variables that already exist.
/*
/* INSTRUCTIONS
/* Make a text file with a new line for each variable you like to add.
/* Each line chould start with var, have a tab, then the variable name,
/* another tab and the variable value. Install the script in the proper
/* location, then run it from the Scripts panel. You'll be presented
/* a dialog box to choose your text file, choose the file and the
/* script will do the rest.
/*
/* ***************************************************************** */

var doc = app.activeDocument;
var myVars = doc.textVariables;
var variableNames = myVars.everyItem().name;

var myFindChangeFile;
var myFindChangeArray, myFindPreferences, myChangePreferences;
myFindChangeFile = myFindFile("./Test.ini")
if(myFindChangeFile != null){
	myFindChangeFile = File(myFindChangeFile);
	var myResult = myFindChangeFile.open("r", undefined, undefined);
	if(myResult == true){
		//Loop through the find/change operations.
		do{
			myLine = myFindChangeFile.readln();
			//Ignore comment lines and blank lines.
			if((myLine.substring(0,3)=="var")){
				myFindChangeArray = myLine.split("\t");
				setCustomTextVariable(doc, myFindChangeArray[1], myFindChangeArray[2])
			}
		} while(myFindChangeFile.eof == false);
		myFindChangeFile.close();
	}
}
function myFindFile(myFilePath){
	var myScriptFile = myGetScriptPath();
	var myScriptFile = File(myScriptFile);
	var myScriptFolder = myScriptFile.path;
	myFilePath = myScriptFolder + myFilePath;
	if(File(myFilePath).exists == false){
		//Display a dialog.
		myFilePath = File.openDialog("Choose the file containing your find/change list");
	}
	return myFilePath;
}
function myGetScriptPath(){
	try{
		myFile = app.activeScript;
	}
	catch(myError){
		myFile = myError.fileName;
	}
	return myFile;
}

function setCustomTextVariable(docRef, name, value) {
	//-- Written: 2010.04.19 by Jon S. Winters of electronic publishing support
	//-- eps@electronicpublishingsupport.com
 	try {
		var atv = docRef.textVariables.everyItem();

		if (atv.hasOwnProperty(name)) {
			atv[name].variableOptions.contents = value;
		}
		else {
			var newTV = docRef.textVariables.add({ name: name, variableType: VariableTypes.CUSTOM_TEXT_TYPE });
			newTV.variableOptions.contents = value;
		}
		return true;
	}
	catch (failSilently) {
		var localError = failSilently;
		return false;
	}
}

 

    This topic has been closed for replies.

    2 replies

    Inspiring
    December 18, 2024

    Hi Wendy,

     

    I was the one who posted about the Variable script from Mallverkstan. I can really recommend it. I actually use it right now, in project that involves six manuals across three languages. I control about 50 variables in each file from an external Excel sheet that I save as tsv and then update all variables in FM with the script.

     

    Mats

    frameexpert
    Community Expert
    Community Expert
    September 20, 2024

    Hi Wendy, I thought for sure I had written a script like this a long time ago, but I haven't been able to locate it. It may have been back in my FrameScript days.

     

    I do have a script that does Finds/Changes in batches and it could be modified to replace the Find text with a variable. But I probably can't do it for free. Please let me know if you have budget for this.

     

    There is a way to do this with stock FrameMaker (albeit one at a time). You first copy one of the variables to the clipboard. Then you choose Edit > Find/Change and enter your Find string. In the Change dropdown, choose By Pasting.

    Community Expert
    September 20, 2024

    Hi,

    4XScripts (from Klaus Göbel who is also active in this forum) has a script VariableWizzard which can manage variables and import defintions from a table, when the language changed.

    https://4xscripts.com/en/scripts

    I do not know, if this is something which would help in your case.

    Best regards, Winfried