Copy link to clipboard
Copied
Hi All,
I've got an extendscript app that has worked fine for years in FM 2017, but it's stopped working.
It gets as far as cutting the table>
SelectElement(doc, para);
var textRange = new TextRange;
textRange.beg.obj = textRange.end.obj = para;
textRange.beg.offset = 0;
textRange.end.offset = Constants.FV_OBJ_END_OFFSET;
doc.TextSelection = textRange;
textRange = doc.TextSelection;
var pgf = doc.TextSelection.beg.obj;
var start = pgf.Start;
SelectElement(doc, table);
doc.Cut(0);
This works fine. But then this next bit is no longer working.
var importParams = GetImportDefaultParams();
i=GetPropIndex(importParams,Constants.FS_ImportAsType);
importParams[i].propVal.ival=Constants.FV_TYPE_XML;
i=GetPropIndex(importParams,Constants.FS_FileIsXmlDoc);
importParams[i].propVal.ival=Constants.FV_DoOK;
i=GetPropIndex(importParams,Constants.FS_AlertUserAboutFailure);
importParams[i].propVal.ival=0;
i=GetPropIndex(importParams,Constants.FS_DontNotifyAPIClients);
importParams[i].propVal.ival=1;
i=GetPropIndex(importParams,Constants.FS_HowToImport);
importParams[i].propVal.ival=Constants.FV_DoByCopy;
var returnParams = new PropVals() ;
//add notification for user response to trigger set_shading
Notification(Constants.FA_Note_BackToUser, true);
doc.Import (doc.TextSelection.end , filename, importParams, returnParams);
// reapply para page next properties
pgf.Start = start;
The filename variable exists and is a valid xml file for import.
I get no failure alert, just nothing happens, no file imported.
I'm a bit rusty so would appreciate any pointers on potential issues and best way forward on trouble-shooting?
Thank you!
Copy link to clipboard
Copied
tracey: I've got an extendscript app that has worked fine for years in FM 2017…
Stopped working in FM2017, or in a later FM?
And if so, which major & point release, as I seem to recall reports of script problems in the latest.
Copy link to clipboard
Copied
Let’s go through it line by line. I have added comments to your script (// ...) to make it easier to understand. I have also added some console messages to help with debugging and changed some smaller things, e.g., in this line:
// Your code, note the space before the comma in "doc.TextSelection.end ,":
doc.Import (doc.TextSelection.end , filename, importParams, returnParams);
// Changed code:
doc.Import (doc.TextSelection.end, filename, importParams, returnParams);
Commented and updated code:
// Ensure SelectElement is defined
// Function to select an element (Assuming this is defined elsewhere in your script)
function SelectElement(doc, element) {
// Implementation of your SelectElement function
}
// Select the paragraph element
SelectElement(doc, para);
// Create and set the text range
var textRange = new TextRange;
textRange.beg.obj = textRange.end.obj = para;
textRange.beg.offset = 0;
textRange.end.offset = Constants.FV_OBJ_END_OFFSET;
// Apply text selection
doc.TextSelection = textRange;
// Get the text selection range
textRange = doc.TextSelection;
var pgf = doc.TextSelection.beg.obj;
var start = pgf.Start;
// Select the table element
SelectElement(doc, table);
// Cut the selected table
doc.Cut(0);
// Debugging: Check if Cut operation was successful
$.writeln("Table cut operation completed");
// Import parameters setup
var importParams = GetImportDefaultParams();
// Set the import type to XML
var i = GetPropIndex(importParams, Constants.FS_ImportAsType);
importParams[i].propVal.ival = Constants.FV_TYPE_XML;
// Set the file as an XML document and import anyway
i = GetPropIndex(importParams, Constants.FS_FileIsXmlDoc);
importParams[i].propVal.ival = Constants.FV_DoOK;
// Do not alert the user about failures
i = GetPropIndex(importParams, Constants.FS_AlertUserAboutFailure);
importParams[i].propVal.ival = 0;
// Do not notify API clients
i = GetPropIndex(importParams, Constants.FS_DontNotifyAPIClients);
importParams[i].propVal.ival = 1;
// Set how to import to "copy"
i = GetPropIndex(importParams, Constants.FS_HowToImport);
importParams[i].propVal.ival = Constants.FV_DoByCopy;
// Allocate return parameters
var returnParams = new PropVals() ;
// Add notification for user response to trigger set_shading
Notification(Constants.FA_Note_BackToUser, true);
// Debugging: Check if importParams are set correctly
$.writeln("Import parameters set: " + JSON.stringify(importParams));
// Perform the import
doc.Import(doc.TextSelection.end, filename, importParams, returnParams);
// Debugging: Check if import operation was successful
$.writeln("Import operation completed");
// Reapply paragraph start property
pgf.Start = start;
// Debugging: Final confirmation
$.writeln("Script execution completed.");
That given, I would suggest to break the code into functions, separate concerns, and add error handling so that the user also gets information what went wrong and why. Here is a complete rewrite of your code (note, that your "SelectElement" function needs to be present!):
// Function to select an element (Assuming this is defined elsewhere in your script)
function SelectElement(doc, element) {
// Implementation of your SelectElement function
}
// Function to get import parameters with desired settings
function getCustomImportParams() {
var importParams = GetImportDefaultParams();
// Helper function to set property value safely
function setPropVal(params, propConst, value) {
var index = GetPropIndex(params, propConst);
if (index !== Constants.FE_BadPropNum) {
params[index].propVal.ival = value;
} else {
$.writeln("Property not found: " + propConst);
}
}
// Set import parameters
setPropVal(importParams, Constants.FS_ImportAsType, Constants.FV_TYPE_XML);
setPropVal(importParams, Constants.FS_FileIsXmlDoc, Constants.FV_DoOK);
setPropVal(importParams, Constants.FS_AlertUserAboutFailure, 0);
setPropVal(importParams, Constants.FS_DontNotifyAPIClients, 1);
setPropVal(importParams, Constants.FS_HowToImport, Constants.FV_DoByCopy);
return importParams;
}
// Main function to execute the script logic
function main() {
// Ensure SelectElement is defined and usable
if (typeof SelectElement !== "function") {
throw new Error("SelectElement function is not defined");
}
// Select the paragraph element
SelectElement(doc, para);
// Create and set the text range
var textRange = new TextRange();
textRange.beg.obj = textRange.end.obj = para;
textRange.beg.offset = 0;
textRange.end.offset = Constants.FV_OBJ_END_OFFSET;
// Apply text selection
doc.TextSelection = textRange;
// Get the text selection range
textRange = doc.TextSelection;
var pgf = doc.TextSelection.beg.obj;
var start = pgf.Start;
// Select the table element
SelectElement(doc, table);
// Cut the selected table
doc.Cut(0);
// Debugging: Check if Cut operation was successful
$.writeln("Table cut operation completed");
// Get custom import parameters
var importParams = getCustomImportParams();
// Allocate return parameters
var returnParams = new PropVals();
// Add notification for user response to trigger set_shading
Notification(Constants.FA_Note_BackToUser, true);
// Debugging: Check if importParams are set correctly
$.writeln("Import parameters set: " + JSON.stringify(importParams));
// Perform the import
doc.Import(doc.TextSelection.end, filename, importParams, returnParams);
// Debugging: Check if import operation was successful
$.writeln("Import operation completed");
// Reapply paragraph start property
pgf.Start = start;
// Debugging: Final confirmation
$.writeln("Script execution completed");
}
// Execute the main function
try {
main();
} catch (e) {
$.writeln("Error: " + e.message);
}
Encapsulation and Modularization:
getCustomImportParams
function encapsulates the logic for setting up import parameters, making the code more modular.setPropVal
helper function sets property values safely and logs a message if the property is not found.
Error Handling:
SelectElement
function is defined. It is currently not part of this script and is missing in your code example.main
function in a try-catch
block to handle any runtime errors gracefully.
Code Clarity and Maintainability:
setPropVal
function abstracts the repetitive task of setting property values, reducing code duplication.
Debugging and Logging:
$.writeln
to trace the execution flow and capture the state of important variables.
This version implements some best practices for coding in ExtendScript. It should make it more robust, maintainable, and easier to understand. Let us know if this helps.