Reply to topic  [ 10 posts ] 
Macro for Exporting or Save as Plain Text 
Author Message

Joined: 2014-07-18 18:32:13
Posts: 28
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


2014-08-20 23:30:53
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
$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


2014-08-21 06:53:33
Profile

Joined: 2008-03-16 16:41:16
Posts: 20
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:
#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.

_________________
Mimsy Were the Borogoves


2014-08-21 12:20:56
Profile WWW

Joined: 2014-07-18 18:32:13
Posts: 28
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


2014-08-21 12:40:30
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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


2014-08-21 18:38:22
Profile
User avatar

Joined: 2007-01-17 03:25:42
Posts: 729
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.


2014-08-22 09:30:16
Profile

Joined: 2014-07-18 18:32:13
Posts: 28
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


2014-09-02 17:31:46
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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


2014-09-02 20:07:22
Profile

Joined: 2014-07-18 18:32:13
Posts: 28
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


2014-09-05 15:39:49
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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


2014-09-06 07:00:16
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 10 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:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software