Save as PDF macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
Emma
Posts: 1
Joined: 2017-03-28 12:41:04

Save as PDF macro

Post by Emma » 2017-03-28 12:48:49

I am trying to write a macro to export the active document to pdf. Nothing fancy, just the equivalent of the Menu command File:Export as PDF... I just want it to export the pdf file in the same folder where the rtf file is, and with the same name (and pdf extension of course).
Whatever I do, my simple macro creates the pdf file in the place of the original rtf file. That is, the rtf file gets replaced by the pdf, while keeping the same rtf extension. When in fact I expect the command Save as PDF to create the pdf as another file next to the rtf file that gets converted.

I am doing things like :

$doc = Document.active
$outputDir=$doc.filePath.filePathByRemovingLastComponent & '/'
Save as PDF $outputDir

and variations on the above, all without success.

Can somebody help ?

Many thanks.

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Save as PDF macro

Post by Kino » 2017-04-05 08:56:06

Emma wrote: $outputDir=$doc.filePath.filePathByRemovingLastComponent & '/'
Save as PDF $outputDir
As Save As PDF command is not clever enough, you have to give it a full path of the pdf to be generated. Something like this would work.

Code: Select all

$doc = Document.active
$docPath = $doc.filePath

# The next three line is unnecessary if you are 100% sure you run this macro
# on saved documents only.
if $docPath == @undefined
	exit 'The document has never been saved, exiting...'
end

# As this macro will change the content of the surrouding folder,
# you may get an error if you don't “require” the access to it. 
# Remove the next two lines if you are running an earlier version of
# Nisus Writer Pro not understanding “File.requireAccessAtPath” command
$folderPath = $docPath.filePathByRemovingLastComponent # Get the folder path
File.requireAccessAtPath $folderPath # For Sandbox access

# Get the path of the pdf file this macro will generate.
# For example, '/Users/you/Documents/sample.pdf'
# for '/Users/you/Documents/sample.rtf'.
$pdfPath = $docPath.filePathByChangingExtension('pdf')

Save As PDF $pdfPath

Post Reply