Reply to topic  [ 9 posts ] 
URL list (plain text) to list of hyperlinks 
Author Message

Joined: 2007-04-12 02:17:08
Posts: 14
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


2013-07-25 20:28:49
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
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:
$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:
$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:
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:
$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


2013-07-26 08:01:23
Profile

Joined: 2007-04-12 02:17:08
Posts: 14
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


2013-07-29 16:39:53
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
Insert Link $link.substring.transliterateInRange($link.range, $urlEncodingHashMap)


should work (Haven't tested it though :wink: )

best
Philip

_________________
philip


2013-07-29 17:19:23
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
$range = Range.new 0, $link.substring.length
Insert Link $link.substring.transliterateInRange($range, $urlEncodingHashMap)

_________________
philip


2013-07-29 17:23:19
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
$link = "http://nisus.com/folder with spaces/some file.txt"
Set Link $link

Clicking that applied link will open in Safari as:
Code:
http://nisus.com/folder%20with%20spaces/some%20file.txt


2013-07-30 08:27:13
Profile WWW

Joined: 2012-12-19 05:02:52
Posts: 226
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?


2014-05-20 08:54:44
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
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


2014-05-20 09:47:00
Profile

Joined: 2012-12-19 05:02:52
Posts: 226
phspaelti wrote:
Change the loop for inserting links like this:
Code:
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.


2014-05-21 14:32:16
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


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:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software