textStyleRange.convertToNote() becomes very confused.
I'll attach a self-contained script which first creates in order to demonstrate problems with textStyleRange.convertToNote(). The script creates a small document containing "b"s. These are marked with a smaller pointSize so that they form individual textStyleRanges. The script then converts these textStyleRanges into notes. Unfortunately, the script terminates with a message: "Object is invalid; tsr.convertToNote()". This message happens because the scripting engine has become confused and is trying to convert a "b" within a note into another note. This shouldn't happen and is the first problem.
Open the story window. The beginning of the story has a clear pattern of notes between sentences. This should continue to the end of the document, but looking further down, two more problems can be seen:
(1) Overflow.jpg shows that the text at the frame's overflow boundary has become confused. It should say "The quick green fox ...", but instead it says something like "The The The quick The ...".
(2) Nearer to the end of the story, Confusion.jpg shows that larger phrases are converted into notes even though only "b" should be converted.
I can't attach a script, so here it is:
// Run this script in InDesign CC 2019, Roman Region.
//THIS IS THE PROBLEM PART THE REST IS JUST TO SET IT UP.
// Convert all "b" characters into notes
function hideNotes(story){
// Find all the "b"s
app.findTextPreferences = null;
var findPrefs = app.findTextPreferences;
findPrefs.findWhat = "b";
var matchingItems = story.findText()
// It shouldn't be necessary to copy the findText() results, but just in case ...
var copy = []
for (var i = 0; i < matchingItems.length; i++) {
copy[i] = (matchingItems[i])
}
// Loop through the "b"s and convert them to notes
for (var i = 0; i < copy.length; i++) {
var tsr = copy[i].textStyleRanges.firstItem();
tsr.convertToNote();
}
}
// This is just setting up a document with reasonable defaults
function setup(){
// Set up application
var millimeterMeasurementUnits = 2053991795; // This means millimeters
app.viewPreferences.verticalMeasurementUnits = millimeterMeasurementUnits ;
app.viewPreferences.horizontalMeasurementUnits = millimeterMeasurementUnits ;
// Set up new document
var doc = app.documents.add();
doc.zeroPoint = [0, 0];
doc.viewPreferences.rulerOrigin = 1380143215 // This means PageOrigin;
doc.documentPreferences.pageWidth = 210
doc.documentPreferences.pageHeight= 297
doc.marginPreferences.left = 20
doc.marginPreferences.right = 20
doc.marginPreferences.top = 20
doc.marginPreferences.bottom = 20
// Set up new page
var page = doc.pages.firstItem();
page.marginPreferences.left = 20
page.marginPreferences.right = 20
page.marginPreferences.top = 20
page.marginPreferences.bottom = 20
// Set up new frame
var textFrame = doc.textFrames.add();
textFrame.geometricBounds = [20, 20, 277, 190];
// Add frame text
for(var i = 0; i < 250; i++){
var insertionPoint = textFrame.insertionPoints.lastItem()
insertionPoint.pointSize = 12
insertionPoint.contents = "The quick green fox jumped over the lazy dog. "
insertionPoint = textFrame.insertionPoints.lastItem()
// This point size will set the "b" into its own textStyleRange
insertionPoint.pointSize = 7
insertionPoint.contents = "b"
}
}
//Entry Point
setup();
var stories = app.activeDocument.stories;
for (var i = 0; i < stories.length; i++) {
hideNotes(stories[i]);
}
-
Bev commented
Since notes scripting is broken we've taken another approach for our project.