Back

If your email is not recognized and you believe it should be, please contact us.

Forum Replies Created

Viewing 15 posts - 226 through 240 (of 340 total)
  • Author
    Posts
  • in reply to: swap text with specific paragraph style #88230
    Peter Kahrel
    Participant

    David: you can swap the contents of two (consecutive) paragraphs, but not the paragraph objects. So you risk losing all formatting. And you need to reapply the paragraph styles.

    Kathy: You can use this script to swap Red and Blue. Works only to swap Red/Blue, Red/Blue, etc. To change Blue/Red, Blue/Red, exchange ‘Red’ and ‘Blue’ in the script:

    (function () {
      var i;
      var red;
      var next;
      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = 'Red';
      red = app.documents[0].findGrep();
      for (i = red.length-1; i >= 0; i--) {
        next = red[i].insertionPoints[-1].paragraphs[0];
        if (next.characters[-1].contents !== '\r') {
          next.insertionPoints[-1].contents = '\r';
        }
        if (next.appliedParagraphStyle.name == 'Blue') {
          red[i].move (LocationOptions.AFTER, next.insertionPoints[-1]);
        }
      }
    }());

    Peter

    in reply to: GREP wildcard question #88229
    Peter Kahrel
    Participant

    Yes, it does:

    Find what: (.)
    Change to: $1

    This would replace any character (except the paragraph return) with itself. The parentheses create a referent, which is referenced by $1. You can do 10 references:

    Find what: (.)(.)
    Change to: $1$2

    etc.

    in reply to: GREP wildcard question #88227
    Peter Kahrel
    Participant

    You may not have properly emptied the Find what and Change to fields.
    You don’t need Grep, you can do this in both the Text and the Grep panels:

    1. Make sure that the Find what and Change to fields are empty.
    2. In the Find format panel, enter -0.75 points
    3. In the Change format panel, enter 0 points. This will be displayed as ‘+not shifted’ in the panel.
    4. Do the replacement.

    Works for me.

    Peter

    in reply to: Search and Paste #88179
    Peter Kahrel
    Participant

    You can do that with Find/Change. In the Text tab,

    Find what: your search string
    Change to: ^c (formatted) or ^C (unformatted)

    (Use ~c or ~C in the Grep tab)

    Peter

    in reply to: Use GREP to Put In Footnote Reference #87782
    Peter Kahrel
    Participant

    Emiel,

    If your dummy footnotes are formatted [[Abc]] and a footnote should be created with content Abc to replace [[Abc]], then you’d need something like this script:

    (function () {
      var i;
      var placeholders;
      var fnote;
      app.findGrepPreferences = null;
      app.findGrepPreferences.findWhat = '[[(.+?)]]';
      placeholders = app.documents[0].findGrep();
      for (i = placeholders.length-1; i >= 0; i--) {
        fnote = placeholders[i].insertionPoints[0].footnotes.add();
        placeholders[i].move (LocationOptions.AFTER, fnote.insertionPoints[-1]);
      }
    }());

    You end up with [[ and ]] in your footnotes, which you can remove with some find/replace operation.

    Peter

    Peter Kahrel
    Participant

    Do a GREP find/change:

    Find what: \[([-\d,]+)\]
    Change to: $1
    Change format: select superscript

    Peter

    in reply to: Sort a Book? #85984
    Peter Kahrel
    Participant

    You need to replace ^lt; with the less-than symbol.

    P.

    in reply to: Sort a Book? #85230
    Peter Kahrel
    Participant

    Thanks for fixing the brace and parentheses, Ari. Cursed copy gremlins!
    As to the page numbering setting, that must be by section because otherwise Bryan’s workflow doesn’t work at all.

    P.

    in reply to: Sort a Book? #85224
    Peter Kahrel
    Participant

    Here’s a script that sorts book files by their (first) page number.

    (function () {
      var list = [];
      var docs = app.books[0].bookContents;
      for (var i = 0; i < docs.length; i++) {
        list.push ({name: docs[i].name, firstPage: Number(docs[i].documentPageRange.replace(/-\d+/,''))});
      }
    
      list.sort (function (a,b) {return a.firstPage - b.firstPage})
    
      for (i = list.length-1; i >= 0; i--) {
        docs.item(list[i].name).move (LocationOptions.AT_BEGINNING);
      }
    }

    Peter

    in reply to: Remove character at the end of a line of wrapped text #85221
    Peter Kahrel
    Participant

    > wouldn’t that script apply the ‘Hide’ character style to the last two characters

    No: lines[j].characters[-2] means ‘the one-before-last character in lines[j]’

    > in a line that ends with a discretionary line break; even if the line ended with a discretionary line break following a letter?

    That’s true. But in Mike’s example that wasn’t the case.

    P.

    in reply to: Remove character at the end of a line of wrapped text #85210
    Peter Kahrel
    Participant

    > can discretionary line breaks be imported in XML?

    Probably. Their hex code is 200B, you can use that. Not sure how, though.

    > you are hiding every comma, not just the comma at the end of a link where a wrap occurred. Am I correct?

    Not really. The script first unhides all commas, then hides the commas at the end of each line. That’s really only needed if you change the width of a column table, because the styles aren’t updated automatically when you resize a column. If you never resize, then lines 5 and 6 can be deleted (the two lines starting with cells[i].texts[0]).

    P.

    in reply to: Remove character at the end of a line of wrapped text #85189
    Peter Kahrel
    Participant

    Question #1: Add a discretionary line break after every comma. That’ll keep the digits together.

    Question #2: Not automatically. A Grep style won’t work because you can’t find the end of turn lines (except when you add forced line breaks, which you probably don’t want). But a script can do that.

    Create a character style Hide (set just the colour to Paper), then click somewhere in the table and run the script.

    (function () {
      var i, lines, cells;
      cells = app.selection[0].parent.parent.cells.everyItem().getElements();
      for (i = 0; i < cells.length; i++) {
        cells[i].texts[0].appliedCharacterStyle = app.documents[0].characterStyles[0];
        cells[i].texts[0].clearOverrides (OverrideType.CHARACTER_ONLY);
        lines = cells[i].lines;
        for (j = 0; j < lines.length; j++) {
          if (lines[j].characters[-1].contents == SpecialCharacters.DISCRETIONARY_LINE_BREAK) {
            lines[j].characters[-2].appliedCharacterStyle = 'Hide';
          }
        }
      }
    }());

    Peter

    in reply to: Script to update a page number and file name. #85083
    Peter Kahrel
    Participant

    You say you’re new to scripting, so before you get into file renaming you should read chapter 3 in the JavaScript Tools guide, see Help > JavaScript Tools Guide CC (or CS6, etc.) in the ESTK.

    You can’t rename an open document, but what you can is save it under a different name and delete the original. Here’s some code to get you started (including a simple dialog to get a page number):

    (function () {
      // Get a page number from the user
      var page = prompt ('Page number: ', '0');
      // If the user pressed Escape or just closed the window, 'page' is null
      if (page !== null) {
        // Store the document's name
        var docPath = app.documents[0].fullName;
        // Create the new name by replacing the initial number with the user input
        var outfile = File (app.documents[0].filePath+'/'+app.documents[0].name.replace (/^\d+/, page));
        // Save the document using the new name
        app.documents[0].save (outfile);
        app.documents[0].close();
        // Delete the original document
        docPath.remove();
      }
    }

    Peter

    in reply to: GREP to change character styles in a paragraph #84268
    Peter Kahrel
    Participant

    No, with GREP styles you can look for text only, not any formatting.
    By the way, do you really need that character style? Many modern fonts have old-style figures built in, which you can set in your paragraph style at ‘Default figure style’ (sorry, had to ask).

    Peter

    in reply to: GREP help – find and replace ……………….. #84225
    Peter Kahrel
    Participant

    Find what: \.\.+(?=\d) // i.e. find two or more consecutive dots followed by a digit
    Change to:

    Peter

Viewing 15 posts - 226 through 240 (of 340 total)