How to cut first paragraph of a document in AppleScript

Everything related to our flagship word processor.
Post Reply
paulcruice
Posts: 17
Joined: 2006-06-04 15:42:19
Location: Toowoomba, Australia

How to cut first paragraph of a document in AppleScript

Post by paulcruice » 2015-03-22 02:30:01

I need to know the AppleScript I must write in Apple Script Editor to cut the first paragraph of a NWPro document called, File 1.
I presume it goes something like this.

tell application "Nisus Writer Pro"
activate "File 1"
???
end tell

Thanks in anticipation.
Also does anyone know a good book/program which adequately shows how to read an AppleScript dictionary?
Jesus heals the rift between Man and Creator.

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

Re: How to cut first paragraph of a document in AppleScript

Post by phspaelti » 2015-03-22 06:31:27

Why do you need to write this in AppleScript? It's much easier to write such things with Nisus' own macro language. What is your goal?

For Example the following as a Nisus Writer Macro will select the first paragraph of your document:

Code: Select all

Find Next '\A.*\n', 'E'
The following will delete the first paragraph:

Code: Select all

Find and Replace '\A.*\n', '', 'E'
The following will cut the paragraph (and place it on the clipboard):

Code: Select all

Find Next '\A.*\n', 'E'
Cut
You could probably use any of these in an Applescript, by packing them into a do Menu Macro with macro line in your AppleScript. For instance the first example could be used like this (note the need for doubling the backslashes):

Code: Select all

tell application "Nisus Writer Pro"
		activate
		Do Menu Macro with macro "Find Next '\\A.+\\n', 'E'"
	end tell
philip

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

Re: How to cut first paragraph of a document in AppleScript

Post by phspaelti » 2015-03-22 07:42:10

Also the following works for a pure AppleScript approach:

Code: Select all

tell application "Nisus Writer Pro"
	set doctext to text of front document
	set para to paragraph 1 of doctext
end tell
philip

paulcruice
Posts: 17
Joined: 2006-06-04 15:42:19
Location: Toowoomba, Australia

Re: How to cut first paragraph of a document in AppleScript

Post by paulcruice » 2015-03-22 13:50:01

Philip, thank you for the help. I need to do this in AppleScript as it is part of a process of getting something into another program.
I had this working many years ago for an old Nisus Writer but it needed revision. As I don't use AppleScript much I need to relearn some things.
I guess I don't understand what Do Menu Macro means. The addition of the word Macro would seem to imply I am doing a Macro already set up in the Macro Menu. Obviously it doesn't mean that. So what do the words actually mean. Also the "with Macro text" option leaves me wondering what this is all for.
It is meaningful no doubt to programers but not to me.
It would be wonderful if someone has written a book on how to read the AppleScript dictionaries.
Once again,
Thanks for the help.
Would appreciate any comment you might like to make on the above.
Cheers.
Paul
Jesus heals the rift between Man and Creator.

adryan
Posts: 208
Joined: 2014-02-08 12:57:03
Location: Australia

Re: How to cut first paragraph of a document in AppleScript

Post by adryan » 2015-03-22 15:47:26

G'day, Philip et al

I was interested to see Philip's use of "\A" as a metacharacter for the Special Position "Start of Document". I had been looking for such an object for a long time but couldn't find one. It appears to be undocumented (in both NWP Help and the Macro Language Reference). By analogy, "\Z" appears to find the end of a document.

(I’ve now found a reference to a brief discussion on this: <http://nisus.com/forum/viewtopic.php?p=10262#p10262>. So what follows is what I have discovered about the implementation of these metacharacters in Nisus Writer Pro. Note that I have done these tests with the metacharacters directly in the Find/Replace dialog box rather than via macros.)

However, things are not so simple as they at first appear. With the insertion point in the main document, successive iterations of Find '\A' place the insertion point at the beginning of text areas in the following order: Comments → Header → Footer → Main document. The next iteration then cycles back to Comments. Note that Footnotes are excluded from this cycle. Replacement text will be inserted as one might expect.

"\Z" behaves differently, however. With the insertion point in the main document, Find '\Z' first finds the end of the footnotes area if there are any footnote markers after the insertion point; if there are none, it finds the end of the main document. In either case, all subsequent iterations find the end of the main document only. Furthermore, replacement text is inserted at the end of the main document only, not at the end of the footnotes.

There may well be subtleties that I have not uncovered in this brief foray, but I know I feel better now that it's at least documented this far!

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

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

Re: How to cut first paragraph of a document in AppleScript

Post by phspaelti » 2015-03-22 17:36:42

adryan wrote:I was interested to see Philip's use of "\A" as a metacharacter for the Special Position "Start of Document". I had been looking for such an object for a long time but couldn't find one. It appears to be undocumented (in both NWP Help and the Macro Language Reference). By analogy, "\Z" appears to find the end of a document.…
Yes, you noticed that point that I kinda glossed over :wink:
The '\A' metacharacter is not strictly speaking 'Beginning of Document', but more like 'Beginning of Text Object'. I haven't tried this, but I assume that if your document has tables, comments, and/or text boxes, (besides mutliple headers/footers) that it would cycle through those as well. As for the order, I suspect it will be the order also returned by 'Document.allTexts', which may not be well defined.
You can however control which Text objects to search using the 'Where' option (see the Macro Language Reference for details).

Code: Select all

Find Next '\A.+', 'E', '-am'
Here the final '-am' means (not All, only Main text).
philip

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

Re: How to cut first paragraph of a document in AppleScript

Post by phspaelti » 2015-03-22 18:11:24

paulcruice wrote:I need to do this in AppleScript as it is part of a process of getting something into another program.
I had this working many years ago for an old Nisus Writer but it needed revision. As I don't use AppleScript much I need to relearn some things.
I guess I don't understand what Do Menu Macro means. The addition of the word Macro would seem to imply I am doing a Macro already set up in the Macro Menu. Obviously it doesn't mean that. So what do the words actually mean. Also the "with Macro text" option leaves me wondering what this is all for.
The "do Menu Macro with macro" command will run any text given as if it were a macro. Since a single menu command on a line is already a Nisus Macro, this gives it the ability, with a single AppleScript 'hook', to access every feature of Nisus.
This is no doubt based on NWP's ability to interpret any text as a macro on the fly. But the net effect is that you can write an entire Nisus Macro as a variable in AppleScript and then pass this to Nisus via the 'do Menu Macro' command. The one real note of caution is the issue of interpolation. As my earlier example showed, if you write this in AppleScript you will need to worry about the escapes, such as backslashes. I don't know enough about AppleScript if there is anything besides backslashes that will come into the picture. If the macro you want to run is more complex than one or two statements, I would recommend writing a macro in Nisus and then just calling that, instead of writing it as a variable in AppleScript.
Another note of caution is that anytime the macro fails, it fails silently, but generally continues uninterrupted, so you can funny results.
paulcruice wrote:It would be wonderful if someone has written a book on how to read the AppleScript dictionaries.
Tell that to Apple.
I think the problem here is that AppleScript sits uneasily with Apple's general philosophy of making computers that anybody can use without specialised knowledge (or 'for the rest of "us"' as they like to call it :) ). So they are not truly committed to supporting this feature.
Last edited by phspaelti on 2015-03-22 18:55:04, edited 1 time in total.
philip

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

Re: How to cut first paragraph of a document in AppleScript

Post by phspaelti » 2015-03-22 18:44:18

With all of the above cleared up (I hope) I'd still return to the question of what your workflow is supposed to achieve.

1. If you want to get hold of what is in some Nisus Document (to pass on to another document), use the AppleScript method, with the AppleScript 'container' syntax:

Code: Select all

set doctext to text of front document
set para to paragraph 1 of doctext
2. If you want to edit a Nisus Document (from AppleScript), use a Nisus macro, triggered by AppleScript.

3. If you want to pass info back and forth use the clipboard. For example

Code: Select all

tell application "Nisus Writer Pro"
	Do Menu Macro with macro "Write Clipboard 'Hi there!'"
end tell
display dialog (the clipboard)
So one way to cut the first paragraph of the Nisus Document would be to write the following Nisus Writer Macro:

Code: Select all

$doc = Document.active
$para = TextSelection.new $doc.text, $doc.text.rangeOfParagraphAtIndex(0)
$doc.setSelection $para
Cut
If you save this as "Cut First Paragraph of Document", then you can write the following AppleScript:

Code: Select all

tell application "Nisus Writer Pro"
	Do Menu Macro with macro "Cut First Paragraph of Document"
end tell
display dialog (the clipboard)
Hope this helps
philip

paulcruice
Posts: 17
Joined: 2006-06-04 15:42:19
Location: Toowoomba, Australia

Re: How to cut first paragraph of a document in AppleScript

Post by paulcruice » 2015-03-22 21:07:47

Many thanks Philip.
All the best,
Paul
Jesus heals the rift between Man and Creator.

Post Reply