Skip to main content
Known Participant
July 8, 2009
Answered

Set all text in brackets as Notes?

  • July 8, 2009
  • 1 reply
  • 4254 views

I created the following search string to find the shortest match of text in brackets in my document.

\[.+?\]

What I'd like to do is set these all as notes so the info is still there, but not in the body text of the document. I don't see a way to do it in the Change Format options...is there a way to do it in JavaScript? I have an existing Find/Change script I could add the command to if someone can help me out.

I appreciate any help!

Matthew

This topic has been closed for replies.
Correct answer interesting_Flower157F

Sorry, Thomas. Just to catch you up...the script you posted works great. It finds all text in brackets and convert them to notes when they are preceeded by a space (because there is a space in the search string "  \\[.+?\\]"). What I'd like to do is append some additional lines of code to the script you posted that also finds all bracketed text that is not preceeded by a space (because it starts the paragraph) and comprises the entire paragraph. I know that the GREP I need to use is "^\\[.+?\\]$", but I don't know enough Javascript to code it properly.

You can not convert text to notes using Find/Change settings, which is why I would like it to be a script. It doesn't matter if the brackets stay in the notes are not, as long as the information that's inside the brackets gets converted.


This will run two searches, is this what you need?

var myDocument = app.activeDocument;
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
//Regular expression for finding an email address.
app.findGrepPreferences.findWhat = " \\[.+?\\]";
var myFoundItems = myDocument.findGrep();

for(i = myFoundItems.length-1; i >= 0; i--) {
    try {
        var myNote = myFoundItems.convertToNote();
        // Delete the [ ]
        myNote.characters.firstItem().remove();
        myNote.characters.lastItem().remove();
    } catch (e) {
        alert("The string: " + myFoundItems.contents + " could not be converted to note");
    }
}

app.findGrepPreferences.findWhat = "^\\[.+?\\]$";
var myFoundItemsTwo = myDocument.findGrep();

//Clear the find/change preferences after the search.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

for(i = myFoundItemsTwo.length-1; i >= 0; i--) {
    try {
        var myNote = myFoundItemsTwo.convertToNote();
        // Delete the [ ]
        myNote.characters.firstItem().remove();
        myNote.characters.lastItem().remove();
    } catch (e) {
        alert("The string: " + myFoundItemsTwo.contents + " could not be converted to note");
    }
}

1 reply

interesting_Flower157F
Inspiring
July 8, 2009

This will convert all text in brackets (your GREP) to notes, and remove the brackets from the text in the note.

var myDocument = app.activeDocument;
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
//Regular expression for finding an email address.
app.findGrepPreferences.findWhat = "\\[.+?\\]"; // every backslash should be doubled (special char in javascript).

var myFoundItems = myDocument.findGrep();

//Clear the find/change preferences after the search.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
for(i = myFoundItems.length-1; i >= 0; i--) {
    var myNote = myFoundItems.convertToNote();
    // Delete the [ ]
    myNote.characters.firstItem().remove();
    myNote.characters.lastItem().remove();
}

--

Thomas B. Nielsen

http://www.nobrainer.dk

mattacaAuthor
Known Participant
July 8, 2009

Thank you for the help. The script you provided set many of the brackets to notes, but half-way through I got the following error:

JavaScript Error!

Error Number: 41990

Error String: Can't create object at this location

File: /Users..../Brackets to notes.jsx

Line: 25

Source: var myNote = myFoundItems

.convertToNote();

It looks like it happened when it reached the end of the document. Does the cursor need to be at the beginning of the document for the script to work correctly?

I should have also mentioned I don't want set as notes brackets that begin a paragraph. For that reason I had put a single space at the beginning of my search string, though you probably could tell that from my initial post. Would it be very hard to change the code to fix those two issues?

I'm sorry I know almost nothing about Javascript. I guess this is a good impetus to learn.

Matthew

mattacaAuthor
Known Participant
July 10, 2009

Can anyone with the know-how help on this? I would really appreciate it.