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:
(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:
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:
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:
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
Insert Link $link.substring
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.