Save to / Save as to a particular file format

Get help using and writing Nisus Writer Pro macros.
Post Reply
vwnisus
Posts: 10
Joined: 2016-04-24 00:13:31

Save to / Save as to a particular file format

Post by vwnisus » 2018-04-12 07:11:24

I would like to use in a Nisus macro command to save to a particular file format.

Looking at the Macro Reference document the Save To and Save To commands to not appear to have a parameter which allows the setting of a file format.

Is this possible, or is there an equivalent way of doing this?

Using Nisus Writer Pro 2.1.8 / macOS 10.13.4

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

Re: Save to / Save as to a particular file format

Post by phspaelti » 2018-04-12 21:25:20

Is the file data that you are hoping to save already in the right format? Or were you expecting Nisus to convert it on the way out?

Nisus macro language has commands "File.writeStringToPath" and "File.writeDataToPath" which should allow you to save data that is not '.rtf' But you will be responsible to make sure that what you save is in the right format. So for example if you wanted to save text data in '.csv' format I think that should work. I have used these commands to change the text encoding of non-Nisus files, but this was always just plain text. The 'writeData' command might also work for images, or such, but I haven't tried that.

Otherwise you are probably going to need some way to convert the data to the appropriate format. Depending on the situation it might be possible to do something with AppleScript…
philip

vwnisus
Posts: 10
Joined: 2016-04-24 00:13:31

Re: Save to / Save as to a particular file format

Post by vwnisus » 2018-04-13 02:52:53

Thank you for the response.

The situation is as follows:

1. I download legal case reports from a supplier of legal information. They have a .doc extension.
2. They reports are really .rtf files, but the software they use creates slightly odd rtf files (odd in the sense there is something in the rtf code which means they are not read properly by some Mac/iOS apps). Whether the extension is .doc or .rtf they will not preview in some apps such as DevonThink.
3. Once the report is opened in a word processor (any) and saved as rtf the case report will then preview correctly.

My aim was to create a macro in Nisus to change formatting of the case report (change type face, size of type) and then save as and choose the format as rtf (because if opened in Nisus with the .doc extension Nisus will wish to save as a Word .doc format).

However, I have found a solution: with Keyboard Maestro, in Finder first rename as rtf, open in Nisus, run the macro, and then save and the file is properly saved as a rtf. This seems to do what I need.

Þorvarður
Posts: 234
Joined: 2012-12-19 05:02:52

Re: Save to / Save as to a particular file format

Post by Þorvarður » 2018-04-13 06:58:52

vwnisus wrote:
2018-04-13 02:52:53
if opened in Nisus with the .doc extension Nisus will wish to save as a Word .doc format
Hmm, if you have the default format in Nisus Preferences set to 'Rich Text Format (RTF)', then Nisus should offer Rich Text Format as the first saving option.
Nisus Writer Pro –> Preference –> Saving –> File Formats –> 'Rich Text Format (RTF)'
My aim was to create a macro in Nisus to change formatting of the case report (change type face, size of type) and then save as and choose the format as rtf
The following macro should do what you want.

Code: Select all

$doc = Document.active
if $doc == undefined
	exit
end
$fileName = Read Selection
if $fileName == ''
	$fileName = $doc.displayName
end
$ext = 'rtf'
$path = User Property 'home folder path'
$path &= '/Desktop/' & $fileName
$path &= '.' & $ext
Save As $path
# End of macro
It gives you two convenient saving options. [1] You can select some text in the document and use it as new file name. [2] Or, if you don't select any text, the "display name" will be used instead.
Example: If the original file name is "Legal case report.doc", the display name, after you open it in Nisus, will be "Legal case report (converted)". Now, if you run the macro without prior selecting any text in the document, the macro will save a copy of it with the name "Legal case report (converted).rtf"

Þorvarður
Posts: 234
Joined: 2012-12-19 05:02:52

Re: Save to / Save as to a particular file format

Post by Þorvarður » 2018-04-13 09:37:04

Here is an improved version of the macro which eliminates the " converted" part of the display name. If nothing is selected it will use the original file name and save the file as .rtf

Code: Select all

$doc = Document.active
if $doc == undefined
	exit
end
# You can select some text in the document and use as a new file name. If you don't select anything, the original file name will be used
$fileName = Read Selection
if $fileName == ''
	$fileName = $doc.displayName
	$characterCountFullName = $fileName.length
	$characterCountTruncated = $characterCountFullName-12
	$delete = $characterCountFullName-$characterCountTruncated
	$range = Range.new($characterCountTruncated, $delete)
	$fileName.deleteInRange($range)
end
$ext = 'rtf'
$path = User Property 'home folder path'
$path &= '/Desktop/' & $fileName
$path &= '.' & $ext
Save As $path
# End of macro

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

Re: Save to / Save as to a particular file format

Post by phspaelti » 2018-04-13 18:28:24

Hello Þorvarður,
I agree with you that the task at hand could easily be achieved with a Nisus macro as well. Let me just make a few suggestions.

First to process several documents in this fashion, a better interface than trying to read filenames/paths out of a document would be to let the user choose them in the finder. Nisus macro language has the commands Choose File, Choose Files and Choose Folder, which very conveniently return file paths. It will also allow you to easily add a loop to your code to process a whole group of files.

Code: Select all

$filePaths = Choose Files
foreach $filePath in $filePaths
…
end
Second if you want to manipulate filepaths and filenames I would generally recommend using the macro language commands. They will avoid you having to mess around with adding "/" and "." etc. For example:

Code: Select all

$newFilePath = $oldFilePath.filePathByChangingExtension 'rtf'
Third if you want to do something like removed " (Converted)" from a file path, then a much more straightforward approach would be to use ".findAndReplace". This will also make your code much more maintainable, since you won't have to look at this later and think "What was I trying to do here."

Code: Select all

# Delete the " (Converted)" from the file path
$newFilePath.findAndReplace ' (Converted)', ''
Note that this code will also work correctly in case there is no such text.
philip

Þorvarður
Posts: 234
Joined: 2012-12-19 05:02:52

Re: Save to / Save as to a particular file format

Post by Þorvarður » 2018-04-15 10:44:25

Thank you very much for these interesting suggestions and examples, Philip.

vwnisus
Posts: 10
Joined: 2016-04-24 00:13:31

Re: Save to / Save as to a particular file format

Post by vwnisus » 2018-04-16 03:43:02

Thank you Þorvarður and Philip for the helpful and detailed replies.

I am a novice at Nisus macros, but you have provided will prompt to learn a bit more.

Post Reply