URL list (plain text) to list of hyperlinks

Get help using and writing Nisus Writer Pro macros.
Post Reply
roblgs
Posts: 14
Joined: 2007-04-12 02:17:08

URL list (plain text) to list of hyperlinks

Post by roblgs » 2013-07-25 20:28:49

I am completely new to Nisus Pro macros and could use some pointers on this...

If I have a list of links, one to a line, in a plain text format, I'd like to paste this into a nisus document and run a macro that will run through the selected lines and add hyperlinks to each one, where the hyperlink is the url as written in the plain text.

So, a static input of:

http://domain.com
http://website.com
http://website.com/somepage

Would become a list of active links in the nisus document, so that if I click on the displayed text, the target link will open in the browser.

In pseudo code:

If selection
For each line in selection
create hyperlink from visible text of current line
Add hyperlink to current line
EndFor
EndIf

I need to create that macro, but really not clear on how to get started.

Pointers appreciated.

Cheers


Rob

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

Re: URL list (plain text) to list of hyperlinks

Post by phspaelti » 2013-07-26 08:01:23

Hello Rob,

your basic pseudo code looks good. The place to get started is to look under Help > Macro Language Reference.
This will open a file that explains all the available commands, but it may be a bit daunting at first.

Here are the basic approaches that I use.

First of all remember that there are two basic approaches to writing macros. One is to use regular menu commands (spelled exactly as in the menus of NWP) one line at a time. This works for some simple things. The other approach is to use the macro specific commands detailed in the Reference. That is what you'll need if you want loops and such, and especially also for Find and Replace. You can mix the two approaches, for example you can have a loop which has menu commands inside the loop.

Okay. I generally start with a Find command. In your case you want to find links. A shortcut for creating a Find macro command is to build it using the Find and Replace dialog and then use the "Macroize" command (in the gear menu of the Find box.)

This should give you something like this:

Code: Select all

Find All ‘http\://[^ ]+’, ‘aE’
(Note that this uses Powerfind Pro. Ask again if this is unclear.)
The statement above will find all instances of "http://" and the following non-space characters. One could probably write something better.

Once my find statement works I add the following line to open the macro:

Code: Select all

$doc = Document.active
This creates a document object that allows you to interact with the document. Most useful command (such as selection commands) are tied to the document object.
So now you can "grab" the selections, that were created with the Find command like this:

Code: Select all

$links = $doc.textSelections
So now $links is an array of selection objects. You can now work with each one. In your case the crucial command (found in the Reference) is:

Code: Select all

Insert Link [i]url[/i]
So select each $link in the array of $links and insert it as a link using this command. Put together the whole thing looks like this:

Code: Select all

$doc = Document.active

Find All ‘http\://[^ ]+’, ‘aE’
$links = $doc.textSelections

foreach $link in reversed $links
    $doc.setSelection $link
    Insert Link $link.substring
end
Some final points:
the foreach … in … command is the most convenient way to create a loop from an array. When processing an array of selections to edit a document, it is generally safest to work in reverse (using reversed). This is because NWP remembers editing locations by index as counted from the start of the document. And if you edit the document, later indexes will be off, but earlier ones will still be accurate.
philip

roblgs
Posts: 14
Joined: 2007-04-12 02:17:08

Re: URL list (plain text) to list of hyperlinks

Post by roblgs » 2013-07-29 16:39:53

Hi phillip,

Thanks for the pointers... I'd actually got something of my own working, but not as efficiently as your suggested solution.

Your explanations were also a huge help, throwing new light into the murky areas of the formal documentation.

One remaining question... How necessary/feasible is it to incorporate url encoding into this, or is that something that NWP does in the background anyway?

Cheers


Rob

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

Re: URL list (plain text) to list of hyperlinks

Post by phspaelti » 2013-07-29 17:19:23

Hi Rob,

I guess Martin would have to speak the technical side of your question. But if you wanted to do URL encoding yourself and you had a suitable hash map, then this can be very easily added to the macro using .transliterateInRange.

Code: Select all

Insert Link $link.substring.transliterateInRange($link.range, $urlEncodingHashMap)
should work (Haven't tested it though :wink: )

best
Philip
philip

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

Re: URL list (plain text) to list of hyperlinks

Post by phspaelti » 2013-07-29 17:23:19

Oops, that will not work.
I was trying to take a short cut around the range hassle. You would have to do it in 2 steps after all.

Code: Select all

$range = Range.new 0, $link.substring.length
Insert Link $link.substring.transliterateInRange($range, $urlEncodingHashMap)
philip

User avatar
martin
Official Nisus Person
Posts: 4361
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: URL list (plain text) to list of hyperlinks

Post by martin » 2013-07-30 08:27:13

There's no need to percent encode the string you feed to the Add Link command, NWP will do it for you automatically. As an example, if you had the code:

Code: Select all

$link = "http://nisus.com/folder with spaces/some file.txt"
Set Link $link
Clicking that applied link will open in Safari as:

Code: Select all

http://nisus.com/folder%20with%20spaces/some%20file.txt

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

Re: URL list (plain text) to list of hyperlinks

Post by Þorvarður » 2014-05-20 08:54:44

Hello Philip,

I have a document with URLs. Everything is written in Verdana. When I run the macro the URLs will be transformed to Times 13 which isn't what I want. I found out that a Character Style named "Hyperlink" in the source document is responsible for this. The URLs will adopt the attributes from this style. If the style attributes are Helvetica 9, the URLs will also be in Helvetica 9. Attempts to remove font and size attributes from the style will inevitably result in Times 13 which seems to be a default in Nisus Writer Pro. An attempt to delete the style or change its name (for example to 'Hyperlinks') will also lead to the URLs being written in Times 13.

Is there any way to let the macro grab the original URL text attributes, store them somehow and then apply them again at the end?

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

Re: URL list (plain text) to list of hyperlinks

Post by phspaelti » 2014-05-20 09:47:00

Hello Þorvarður,

this is the one area where the Nisus macro language could really be improved a lot.

As long as the attributes are a few basic things (font, size, color) you're in luck. Change the loop for inserting links like this:

Code: Select all

foreach $link in reversed $links
    $attr = $link.displayTypingAttributes
    $doc.setSelection $link
    Insert Link $link.substring
    Set Font Size $attr.fontSize
    Set Font Name $attr.fontName
end
It would also be possible to transfer things like styles fairly easily. But you really have to take care of each attribute individually.

Note that one thing won't work in this case; the menu commands "Copy/Paste Character Attributes". This is because the link attribute is part of those character attributes, pasting them back in will erase the link you just created.
philip

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

Re: URL list (plain text) to list of hyperlinks

Post by Þorvarður » 2014-05-21 14:32:16

phspaelti wrote: Change the loop for inserting links like this:

Code: Select all

foreach $link in reversed $links
    $attr = $link.displayTypingAttributes
    $doc.setSelection $link
    Insert Link $link.substring
    Set Font Size $attr.fontSize
    Set Font Name $attr.fontName
end
Absolutely perfect. Thank you Philip.

Post Reply