Macro for Exporting or Save as Plain Text

Get help using and writing Nisus Writer Pro macros.
Post Reply
davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Macro for Exporting or Save as Plain Text

Post by davidrcalvert » 2014-08-20 23:30:53

Can a macro be created that will save a Nisus doc as plain text or export to plain text with Unicode 8 text encoding.
And what would it look like?

Thanks
David

User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro for Exporting or Save as Plain Text

Post by phspaelti » 2014-08-21 06:53:33

This is not really the type of operation that Nisus Macro language handles (or is meant to handle). You can save as text with code like the following:

Code: Select all

$doc = Document.active
$fp = $doc.filePath.filePathByChangingExtension('txt')
File.writeDataToPath $doc.text, $fp
The resulting file is UTF-8 for me, but I don't know if that is automatic. I don't think there is any way to control the encoding.
Also, you might want to check for overwriting, etc.
philip

capvideo
Posts: 20
Joined: 2008-03-16 16:41:16
Contact:

Re: Macro for Exporting or Save as Plain Text

Post by capvideo » 2014-08-21 12:20:56

Depends on what it is you want. If you want the text to be readable as a text document, you'll probably need to go through and modify things as you save it. I have a macro I use to export short stories/novels to text format. It loops through each paragraph and looks for styles that need to be textified.

Code: Select all

#Save FlameWar as text, suitable for e-mailing agents

$thisDocument = Document.active
$pagePath = $thisDocument.filePath
$textPath = $pagePath.filePathByChangingExtension('.txt')

#special styles
$upperStyles = Array.new('Heading 1', 'Title', 'Subheading')
$indentedStyles = Array.new('Quote', 'Block Text', 'Player Description', 'Block Quote')
$singleSpaceStyles = Array.new('URL')

#now do the  conversion
Select Paragraph 1
Select Start

If File.existsAtPath $textPath
	File.deletePath $textPath
End

While Select Next Paragraph
	$currentSelection = $thisDocument.textSelection
	$currentParagraph = $currentSelection.subtext
	If $currentParagraph != "\n"
		$style = $currentSelection.typingAttributes
		$styleName = $style.paragraphStyleName
	
	
		If $styleName == "heading 2"
			$paragraphText = "\n\n\n---------------------------------------------------\n"
		Elsif $styleName == "heading 1"
			$paragraphText = "\n"
		Elsif $styleName == "Source"
			Begin Perl
				chomp($currentParagraph);
			End
			$paragraphText = "($currentParagraph)\n"
		Elsif $indentedStyles.indexOfValue($styleName) != -1
			$paragraphText = $currentParagraph
			Begin Perl
				use Text::Wrap;
				chomp($paragraphText);
				@lines = split("
", $paragraphText);
				@newLines = ();
				foreach $line (@lines) {
					if (length($line) > 56) {
						$line = wrap("\t", "\t", ($line));
					} else {
						$line = "\t$line";
					}
					$newLines[$#newLines+1] = $line;
				}
				$paragraphText = "\n" . join("\n", @newLines) . "\n";
			End
		Elsif $singleSpaceStyles.indexOfValue($styleName) != -1
			$paragraphText = $currentParagraph
		Else
			#are there any character styles?
			$currentIndex = 0
			$paragraphText = ""
			While $currentIndex < $currentParagraph.length
				$currentStyleRange = $currentParagraph.rangeOfAttributesAtIndex $currentIndex
				$subText = $currentParagraph.subtextInRange $currentStyleRange
				$subStyle = $subText.attributesAtIndex 0
				$characterStyle = $subStyle.characterStyleName
				If $characterStyle == 'Loudspeaker'
					$subText = $subText.textByUppercasing
				End
				
				#this works because style-caused italics don't show here
				If $subStyle.italic
					$subText = "*$subText" & "*"
				End

				$paragraphText = $paragraphText & $subText
				$currentIndex = $currentIndex + $currentStyleRange.length
			End
			
			Begin Perl
				$paragraphText =~ s/\*\. \*/. /g;
			End
			$paragraphText = "\n$paragraphText"
		End

		#make these upper case
		If $upperStyles.indexOfValue($styleName) != -1
			$paragraphText = $paragraphText.textByUppercasing
			$paragraphText = "\n$paragraphText"
		End

		File.appendDataToPath $paragraphText, $textPath
	End
End
As you can see, this is fairly specialized for my own documents. It capitalizes Heading 1, Title, and Subheading, indents some styles, and so on.

I don't use tables in such documents, so the macro doesn't handle tables. No reason you shouldn't be able to if you need to, though.

davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Re: Macro for Exporting or Save as Plain Text

Post by davidrcalvert » 2014-08-21 12:40:30

CapVideo and Phillip

Thank You. I will have to study up a bit more on the macro syntax.

My whole intention or goal is to use Nisus using
basic styles eg bold, italics, header 1,2,3 lists and tables then convert to markdown and then save as plain text.

I was able to use some of the macros to chage the styles to have the markdown syntax however the macros only find the styles and insert the markdown syntax but still keep the bold as bold with the syntax tags so to speak.

Thee next step requires a conversion to plaintext to strip out rtf formatting. I found a batch rtf to txt macro in the Nisus respository that seemed to work.

It would be really nice if Nisus had the capability to export in multimarkdown since it is used quite a bit for writers.
Do the Nisus developers really listen to feature requests? Maybe the next rev for Yosemite?
What do you think?

Thanks again,
David

User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro for Exporting or Save as Plain Text

Post by phspaelti » 2014-08-21 18:38:22

davidrcalvert wrote: Thee next step requires a conversion to plaintext to strip out rtf formatting. I found a batch rtf to txt macro in the Nisus respository that seemed to work.
"Stripping out" the formatting really doesn't require anything special. You can simply take the text and do "Cast as String". Even simpler the commands ".writeDataToPath" and ".appendDataToPath" remove the formatting. So if your macro adds the formatting tags, you can just write out the result, and the rtf will be removed.
philip

User avatar
Hamid
Posts: 734
Joined: 2007-01-17 03:25:42

Re: Macro for Exporting or Save as Plain Text

Post by Hamid » 2014-08-22 09:30:16

phspaelti wrote:[...]
The resulting file is UTF-8 for me, but I don't know if that is automatic. I don't think there is any way to control the encoding.
[...]
You can set plain text encoding in NWP Preferences under Saving.

davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Re: Macro for Exporting or Save as Plain Text

Post by davidrcalvert » 2014-09-02 17:31:46

I was thinking that if all menu commands can be made into macros then it would be possible to save as plaintext in the for of a macro or does menu command only apply to a defined set of that can be used as macros

Thanks,
David

User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro for Exporting or Save as Plain Text

Post by phspaelti » 2014-09-02 20:07:22

davidrcalvert wrote:I was thinking that if all menu commands can be made into macros then it would be possible to save as plaintext in the for of a macro or does menu command only apply to a defined set of that can be used as macros
The problem is with menu commands that bring up a dialog. In the case of the Save dialog, the choice of saving as plain text is done through a pop-up menu for which there is no macro access. The Nisus Macro language often provides no-dialog versions of such menu commands, but in the case of Save it provides only the following versions:
  • Save
  • Save As [path]
  • Save To [path]
  • Save As PDF
Ideally Nisus could add a second argument to the "Save As" and "Save To" commands, allowing the user to specify the file format.
philip

davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Re: Macro for Exporting or Save as Plain Text

Post by davidrcalvert » 2014-09-05 15:39:49

Phillip,

Then I take it that the Nisus Macro menu cannot produce the second argument to do a save as plaintext.

Is there a work around just using the Nisus Macro Language?

Could you use AppleScript to do a save as plaintext in Nisus?

Or could you use an app like keyboard maestro to achieve this with Nisus?

Your comments appreciated

David

User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro for Exporting or Save as Plain Text

Post by phspaelti » 2014-09-06 07:00:16

davidrcalvert wrote:Then I take it that the Nisus Macro menu cannot produce the second argument to do a save as plaintext.

Is there a work around just using the Nisus Macro Language?
Well, as mentioned above by both me and capvideo you can use the "File.writeDataToPath" method (or File.appendDataToPath" if you want to add to an existing file). This would really be the method I recommend.

It may be possible to use some interface manipulating app (like keyboard maestro) and work via the user interface, but I don't see any benefit.
philip

Post Reply