Reply to topic  [ 3 posts ] 
Returns at the end of the file 
Author Message

Joined: 2008-04-18 09:17:38
Posts: 3
Most documents I work with end up in InDesign. By default, most documents I get have a return as the last character of the file. Is there a way to delete this?

Don't say the backspace key... I mean like in a macro...

Find: \n <---- followed by an end of file character
Replace: end of file character.

Thanks. That last return often makes a text block problematic in InDesign. I'd like to include deleting it in a macro.


2008-04-18 09:21:39
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
The special PowerFind Pro anchor for "end of document" is "\Z", but that will actually match the end of other text areas as well, eg: table cells. I'd use this macro myself:
Code:
Require Application Version "3.1"

$doc = Document.active
$text = $doc.text
If $text.length > 0
    # check for newline character at end of main body
    $lastIndex = $text.length - 1
    $lastChar = $text.characterAtIndex($lastIndex)
    If $lastChar == 10
        # delete just the final character
        $lastCharRange = Range.new($lastIndex, 1)
        $text.deleteInRange($lastCharRange)
    End
End

I almost forgot to say, this macro requires version 1.1, which is now in public beta.


2008-04-18 13:33:39
Profile WWW

Joined: 2008-05-17 04:02:32
Posts: 400
Since NWP 1.3, a single command can do the job.
Code:
Find and Replace '\n+\z', '', 'E', '-am'
which replaces the return(s) at the document end (\n+\z) with nothing in the main body (-am).

What is inconvenient with it is that the caret will move to the document end when Find and Replace has worked successfully. The following macro get the caret back to the original position by executing Previous Selection when and only when Find and Replace has actually removed the return(s).
Code:
Require Pro Version 1.3
if Find and Replace '\n+\z', '', 'E', '-am'
   Menu ':Edit:Select:Previous Selection'
end

Edit: Modified the code so that multiple returns will be removed.

Edit: It seems that Previous Selection does not always work as expected. Perhaps I misunderstand how it works, though.

Edit: Then, the following is better.
Code:
Require Pro Version 1.3
$doc = Document.active
$doc.text.findAndReplace '\n+\z', '', 'E', '-am'

Edit: Alternatively, you can use something like this (assuming you are not using setMark: usually).
Code:
Require Pro Version 1.3
Send Selector 'setMark:'
if Find and Replace '\n+\z', '', 'E', '-am'
   Send Selector 'selectToMark:'
   Select Start
end

There’s more than a way to do it . . .


2010-09-20 06:26:56
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software