AppleScript: I can't save the file with a different name

Get help using and writing Nisus Writer Pro macros.
Post Reply
bogdan77
Posts: 2
Joined: 2015-01-06 07:05:46

AppleScript: I can't save the file with a different name

Post by bogdan77 » 2015-01-06 07:16:25

Hi there, first-time post,

As part of a larger script, I'm trying to automate saving the current document with a different name (docname + "_v01"), but Nisus throws a "Couldn't save document, AppleEvent handler failed" error:

Code: Select all

tell application "Nisus Writer Pro"
	if (count documents) is not equal to 0 then
		set d1 to document 1
		set fullPath to path of d1 as string
		set parentPath to characters 1 thru -5 of fullPath as string -- here I get rid of the last four letters (the extension and its point)
		save d1 in parentPath & "_v01.rtf"
	end if
end tell
Any idea what am I doing wrong? :oops:
Last edited by bogdan77 on 2015-01-08 13:12:28, edited 1 time in total.

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

Re: AppleScript: I can't save the file with a different name

Post by Þorvarður » 2015-01-07 05:55:44

bogdan77 wrote:Hi there, first-time post
Hello bogdan77,

Welcome on board! I hope you will like it here.
bogdan77 wrote:As part of a larger script, I'm trying to automate saving the current document with a different name (docname + "_v01"), but Nisus throws a "Couldn't save document, AppleEvent handler failed" error
I tried your script, and it's flawless, as far as I can see. I did not get any error message.

How did you run it? Did you save it as a macro instead of saving it with AppleScript Editor as .scpt?

You can create a special AppleScript folder for your AppleScripts (in case you want to separate your AppleScripts from the Nisus macros), and put that special folder inside the Macro folder. Then you can run the scripts directly from the Macro menu. You can also assign a keyboard shortcut to your scripts in the same way as you would do to any normal macro.

You say your script is part of a larger script. Usually people write a macro if the process is going to take place within Nisus, and they only resort to an AppleScript if they need to exchange data with other programs. Thus you could fetch addresses from Contacts with an AppleScript, and then use this command at the end of the AppleScript in order to switch to NWP and hand over the data to a Nisus macro:
tell application "Nisus Writer Pro"
activate
Do Menu Macro with macro "Name of a Nisus macro as it appears in your macro folder"
end tell

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

Re: AppleScript: I can't save the file with a different name

Post by phspaelti » 2015-01-07 07:20:13

bogdan77 wrote:Any idea what am I doing wrong? :oops:
Using applescript :P
Þorvarður wrote:I tried your script, and it's flawless, as far as I can see. I did not get any error message
Hmmm… I haven't had any luck running the script either.

Generally I'd go along with Þorvarður's advice of "Only use applescript if you really need it." The script you posted could just as easily be written as a Nisus Macro.

Anyhow for me the script hangs at the "Save…" line. You can replace that line with the following:

Code: Select all

Do Menu Macro with macro "Save To \"" & parentPath & "_v01.rtf\""
and then it should work.

Anyhow here is what the Nisus macro code for the same would look like:

Code: Select all

$doc = Document.active
$filePath = $doc.filePath
$folder = $filePath.filePathByRemovingLastComponent
$fileName = $filePath.lastFilePathComponentWithoutExtension
$fileExt = $filePath.filePathExtension
$fileName &= '_v01'
$newFilePath = $folder.filePathByAppendingComponent $fileName
$newFilePath = $newFilePath.filePathByAppendingExtension $fileExt
Save To $newFilePath
philip

bogdan77
Posts: 2
Joined: 2015-01-06 07:05:46

Re: AppleScript: I can't save the file with a different name

Post by bogdan77 » 2015-01-08 13:03:51

@ Þorvarður – thanks for the welcome, I really like Nisus and its regex potential. :-)

Well, I ran it from the AppleScript Editor as .scpt, and to me with NWP 2.0.7 on Mavericks it doesn't work… no idea why. :?

(Yes, I run search and replace macros from inside a larger AppleScript, placed in the Macros folder).
phspaelti wrote:
bogdan77 wrote:Any idea what am I doing wrong? :oops:
Using applescript :P
I get what you mean, but AppleScript has its strong ties into the system, better dialog windows, and… it's the only coding language I know at the moment. :lol:
phspaelti wrote:
Þorvarður wrote:I tried your script, and it's flawless, as far as I can see. I did not get any error message
Hmmm… I haven't had any luck running the script either.
[…]
Anyhow for me the script hangs at the "Save…" line. You can replace that line with the following:

Code: Select all

Do Menu Macro with macro "Save To \"" & parentPath & "_v01.rtf\""
and then it should work.
It works with this macro, indeed, thank you. :)
phspaelti wrote:Anyhow here is what the Nisus macro code for the same would look like:

Code: Select all

$doc = Document.active
$filePath = $doc.filePath
$folder = $filePath.filePathByRemovingLastComponent
$fileName = $filePath.lastFilePathComponentWithoutExtension
$fileExt = $filePath.filePathExtension
$fileName &= '_v01'
$newFilePath = $folder.filePathByAppendingComponent $fileName
$newFilePath = $newFilePath.filePathByAppendingExtension $fileExt
Save To $newFilePath
No comment on this one – it's a little bit too advanced for me atm… :)

Thank you, guys.

Post Reply