nisus.com
https://www.nisus.com/forum/

Save as PDF macro
https://www.nisus.com/forum/viewtopic.php?f=17&t=6710
Page 1 of 1

Author:  Emma [ 2017-03-28 12:48:49 ]
Post subject:  Save as PDF macro

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.

Author:  Kino [ 2017-04-05 08:56:06 ]
Post subject:  Re: Save as PDF macro

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:
$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

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/