Batch Conversion

Get help using and writing Nisus Writer Pro macros.
Post Reply
bjast
Posts: 39
Joined: 2010-01-06 10:58:07

Batch Conversion

Post by bjast »

Now that I'm using NWP, and am impressed by its many features, I'd like to convert all of my Emac files to open in NWP.

Is there a way to batch convert all of my Emacs files to NWP - assuming they are all in the same directory?
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Batch Conversion

Post by Kino »

Most likely that is feasible and perhaps without a macro. But we have to know more about those emacs files. What file extension do they have? Is it *.txt? In which encoding are they written? In UTF-8? In MacRoman? And am I right in assuming that you want to convert them into rtf? Are there some tags, e.g. <i>…</i> for Italic style, in those files, that you want to transform into real style attributes?

Or do you simply want to change the default application for those files so that they will be opened in NWP when you double click on them?
bjast
Posts: 39
Joined: 2010-01-06 10:58:07

Re: Batch Conversion

Post by bjast »

Let's see if I can answer your questions:

Q. What file extension do they have?

A. They have no creator or type code. When listed in Terminal they have no file extension.


Q. Are you wanting to convert them into .rtf?

A. Yes


Q. Are there some tags, e.g. <i>…</i> for Italic style, in those files, that you want to transform into real style attributes?

A. No


I hope this helps.
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Batch Conversion

Post by martin »

bjast wrote:Q. Are you wanting to convert them into .rtf?

A. Yes
I might wonder if you really want to convert them to RTF. Nisus Writer can open plain text files just as easily as formatted files. If you plan to keep to your document's content as plain text, converting the file to RTF would be unnecessary. Your workflow may be served just as well by associating these plain text files with NWP.
Q. What file extension do they have?

A. They have no creator or type code. When listed in Terminal they have no file extension.
I'm not sure if OSX will oblige, but have you tried selecting such a creator-less, extension-less file in the Finder and clicking the "Change All" button under "Open with". That might do the trick in getting OSX to open all your plain text files in NWP, regardless of their creator/extension.
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Batch Conversion

Post by Kino »

In Snow Leopard, it seems to be impossible to associate all text files having no file extension to NWP. Even if possible, NWP complains “Unrecognized file format” each time you attempt to open it. Then, if you want to edit them in NWP without changing the file format, it would be convenient to add “.txt” to their file names. And if you really want to convert them to RTF format, you can use something like this.

Code: Select all

 $paths = Choose Files '~/Documents', 'Convert Selected Files to RTF'
if $paths == undefined
	exit  # cancelled
end

$report = "Following files have been created:\n\n"

foreach $path in $paths
	$data = ''
	$enc = 'UTF-8'
	Set Exported Perl Variables 'data', 'path', 'enc'
	begin Perl
		my $conv_err = `mktemp /tmp/conv_err_XXXXXX`;
		chomp $conv_err;
		my $data = `textutil -convert txt -inputencoding utf-8 -stdout "$path" 2> $conv_err`;
		if ( -s $conv_err ) { # then, the conversion has failed
			$data = `textutil -convert txt -inputencoding macintosh -stdout "$path" 2> /dev/null`;
			$enc = 'MacRoman'
		};
		unlink $conv_err;
		$data = decode_utf8($data);
		if ( $data =~ /\b\xEC[^\xEC\xEE]+\xEE\b|\b\xEB[^\xEB]+\xED\b/ ) { # check for single/double curly quotes
			unless ( $data =~ /[\xE7\xE8\xEA\xF9]/ ) { # check for invalid CP-1252 characters
				$data = `textutil -convert txt -inputencoding windows-1252 -stdout "$path" 2> /dev/null`;
				$data = decode_utf8($data);
				$enc = 'Latin 1'
			};
		};
	end
	$doc = Document.new
	$doc.clearAndDisableUndoHistory
	$range = Range.new 0, $doc.text.length
	$doc.text.replaceInRange $range, $data
	$rtfPath = $path.filePathByChangingExtension 'rtf'
	Save As $rtfPath
	$doc.close true
	$report &= $rtfPath & " (from $enc)\n"
end

exit $report
TextFiles2RTF_nwm.zip
(5.15 KiB) Downloaded 765 times
• It is theoretically impossible to detect the encoding of a text file reliably. The macro above assumes that your files are encoded either in UTF-8 (≥ ASCII), or in MacRoman, or in Windows Latin 1 (≥ ISO 8859-1). But some Latin 1 files may be mistaken for MacRoman files.

• With textutil, you can convert text files to RTF format directly. However, the macro opens a new document and puts the content of a text file in it in order to make all styles in your Nisus New File.dot available in it.
bjast
Posts: 39
Joined: 2010-01-06 10:58:07

Re: Batch Conversion

Post by bjast »

Once again, Kino, you have hit the target. This does exactly what I needed it to do. It will be very helpful to have this macro to convert the many plain text docs I created while using Emacs.

Thanks
Post Reply