Reply to topic  [ 2 posts ] 
Save as PDF macro 
Author Message

Joined: 2017-03-28 12:41:04
Posts: 1
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.


2017-03-28 12:48:49
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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


2017-04-05 08:56:06
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software