Page 1 of 1

Save to / Save as to a particular file format

Posted: 2018-04-12 07:11:24
by vwnisus
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

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

Posted: 2018-04-12 21:25:20
by phspaelti
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…

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

Posted: 2018-04-13 02:52:53
by vwnisus
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.

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

Posted: 2018-04-13 06:58:52
by Þorvarður
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"

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

Posted: 2018-04-13 09:37:04
by Þorvarður
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

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

Posted: 2018-04-13 18:28:24
by phspaelti
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.

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

Posted: 2018-04-15 10:44:25
by Þorvarður
Thank you very much for these interesting suggestions and examples, Philip.

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

Posted: 2018-04-16 03:43:02
by vwnisus
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.

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

Posted: 2019-09-05 02:33:48
by vwnisus
I am trying to use one of the macros provided below but I am getting an error message.

The macro is the following one:

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
What I am doing (with Nisus Writer Pro, version 3.0.3, macOS 10.14.6) is

1. open file.
2. run above macro.
3. get an error message: 'could not save the file'.

Attached is the macro as I saved it and a screen shot.

I would be grateful for help in solving this issue.

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

Posted: 2019-09-05 13:52:37
by martin
vwnisus wrote: 2019-09-05 02:33:48 I am trying to use one of the macros provided below but I am getting an error message.
The problem with that old macro is that it doesn't take sandboxing into account. The macro will need to explicitly state that it requires access to your Desktop, so it can save the file there. Here's an updated macro that fixes this:

Code: Select all

# This macro saves the active document to your Desktop using "Save As".
# The name of the new file will match whatever text is selected in the active document.
# If no text is selected, the existing file name is used (removing "converted" suffix if it exists).
$doc = Document.active
If ! $doc
	Exit
end

# determine file name
$fileName = Read Selection
If ! $fileName
	$fileName = $doc.displayName
	$fileName = $fileName.filePathByChangingExtension("")	# remove extension
	If $fileName.hasSuffix(" (converted)")
		$range = Range.new( 0, $fileName.length - 12 )
		$fileName = $fileName.substringInRange($range)
	End
end

# save to desktop
$folder = "~/Desktop"
File.requireAccessAtPath($folder)

$path = "$folder/$fileName.rtf"
Save As $path

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

Posted: 2019-09-09 22:49:53
by vwnisus
Martin,

Thank you very much. Problem solved...

Victor