Macro for Exporting or Save as Plain Text
-
- Posts: 28
- Joined: 2014-07-18 18:32:13
Macro for Exporting or Save as Plain Text
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
And what would it look like?
Thanks
David
Re: Macro for Exporting or Save as Plain Text
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:
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.
Code: Select all
$doc = Document.active
$fp = $doc.filePath.filePathByChangingExtension('txt')
File.writeDataToPath $doc.text, $fp
Also, you might want to check for overwriting, etc.
philip
Re: Macro for Exporting or Save as Plain Text
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.
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.
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
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.
-
- Posts: 28
- Joined: 2014-07-18 18:32:13
Re: Macro for Exporting or Save as Plain Text
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
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
Re: Macro for Exporting or Save as Plain Text
"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.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.
philip
Re: Macro for Exporting or Save as Plain Text
You can set plain text encoding in NWP Preferences under Saving.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.
[...]
-
- Posts: 28
- Joined: 2014-07-18 18:32:13
Re: Macro for Exporting or Save as Plain Text
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
Thanks,
David
Re: Macro for Exporting or Save as Plain Text
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: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
- Save
- Save As [path]
- Save To [path]
- Save As PDF
philip
-
- Posts: 28
- Joined: 2014-07-18 18:32:13
Re: Macro for Exporting or Save as Plain Text
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
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
Re: Macro for Exporting or Save as Plain Text
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.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?
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