Find text, copy and create new file

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
Matze
Posts: 170
Joined: 2004-12-14 07:54:49
Location: Düsseldorf Germany
Contact:

Find text, copy and create new file

Post by Matze »

Hi there,

I've got a document with thousands of textblocks as follows:

([[:digit:]]{1,5}\n[[:digit:]]\n)(.+\n)(.+\n)?(\n?)(A\:.+\n)\n

I'd like NW to find every textblock, copy it, open a new file, paste the copied text into that new file, name the new file (filename is first line of text), save it and close it.
So that in the end I have got thousands of documents.

Can someone please help me with this?

Thanks and best regards, Matze
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Find text, copy and create new file

Post by phspaelti »

Hello Matze,

here is a macro that will do this kind of thing:

Code: Select all

# Useful file saving info
$encoding = 'UTF-8'
$ext = 'txt'

# Locate a Folder to save the bits
$savePath = '~/Desktop'
$savePath = Choose Folder $savePath, 'Make Save Location'
prompt 'The files will be saved here', $savePath

# Find all bits to save. Quit if nothing found
$sourceDoc = Document.active
$bits = $sourceDoc.text.findAll '([[:digit:]]{1,5}\n[[:digit:]]\n)(.+\n)(.+\n)?(\n?)(A\:.+\n)\n', 'Ea'
if ! $bits.count
Exit 'No appropriate text found in this file'
end

# Sandbox access. Sufficient? Necessary?
File.requireAccessAtPath $savePath

foreach $bit in $bits
# Create a file name for saving
$bit.substring.find '([[:digit:]]{1,5}\n[[:digit:]]\n)(?<name>.+)\n(.+\n)?(\n?)(A\:.+\n)\n', 'E$'
$fileName = $savePath.filePathByAppendingComponent $name
$fileName = $fileName.filePathByAppendingExtension $ext
# Save the data to the file
File.writeStringToPath $bit.substring, $fileName, $encoding
end
There are a few things I am unsure of.
  • Note how I am taking the file name from your find expression. Is that the correct bit that you want? If those bits are not unique, this macro will overwrite earlier files with the same name.
  • I added a line for the Sandbox access. I hope this works, and that it only asks for access at most once. You may want to test on a small sample first.
  • At the beginning I added a place where you can choose the extension for the saved files, and for the encoding
  • This is only saving the content. The formatting will be dumped. If you want to save the formatting, this would have to be changed slightly. I picked this method, since I think it will be fastest. Let me know if it is not what you want.
Attachments
Matze_Save_Bits.nwm
(4.46 KiB) Downloaded 720 times
philip
User avatar
Matze
Posts: 170
Joined: 2004-12-14 07:54:49
Location: Düsseldorf Germany
Contact:

Re: Find text, copy and create new file

Post by Matze »

Hi Philip,

thanks a lot for your quick help.
The Macro finds the first bit and creates one file with this bit in it.
But it doesn't do so with all the following bits.
User avatar
Matze
Posts: 170
Joined: 2004-12-14 07:54:49
Location: Düsseldorf Germany
Contact:

Re: Find text, copy and create new file

Post by Matze »

Sorry, it has been my fault: searchpattern only matches one of six test-bits.

So IT WORKS, GREAT! Thank you!

PS When I want to name the bits after the first number I just have to replace the name-part, right?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Find text, copy and create new file

Post by phspaelti »

Matze wrote:When I want to name the bits after the first number I just have to replace the name-part, right?
To get bits out of a find expression into a variable, you can use the '$' option. So you should see that I added a '$' to the options on the second find expression. Note that this only works for "Find" not for "Find All".

So then to grab a bit and put it into a variable you use a parenthesis like this:

Code: Select all

… (?<variable_name> … ) …
The variable name can be any single word you want. The variable will have the same name with a '$' at the front.

Then you make sure that only the part you want in the variable is inside the parentheses. By the way your original find expression was fine, but it has a lot of parentheses, and most of those are not needed in the macro. So here is the expression with all the unnecessary parentheses removed:

Code: Select all

[[:digit:]]{1,5}\n[[:digit:]]\n.+\n(.+\n)?\n?A\:.+\n\n
To get the first 1–5 digits into a variable with name '$name', you add parentheses like this:

Code: Select all

(?<name>[[:digit:]]{1,5})\n[[:digit:]]\n.+\n(.+\n)?\n?A\:.+\n\n
You can use multiple parentheses like this at the same time, with different names, and put the different bits into different variables.

Code: Select all

(?<first_num>[[:digit:]]{1,5})\n(?<second_num>[[:digit:]])\n(?<first_line>.+)\n(.+\n)?\n?(?<A_code>A\:.+)\n\n
Which will end up putting the four different bits into the variables '$first_num', '$second_num', '$first_line', and '$A_code'.

Hope that is clear
philip
User avatar
Matze
Posts: 170
Joined: 2004-12-14 07:54:49
Location: Düsseldorf Germany
Contact:

Re: Find text, copy and create new file

Post by Matze »

Dear Philip,

sorry for the late reply and thanks again for your help and your explanations.
I have saved them in my database for further use.
What would I be without your generous macros over all these years?
If you ever need some help in german (translations, lecture or alike), please let me know. It will be a pleasure for me to pay back that way.

Best, Matze
Post Reply