Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I pass selected text to a user macro parameter?


I have a user macro that ideally would take as one parameter the text that the user has selected in the editor. I know that many macros added via the macro browser will take the user's selection and turn it into the body of the macro, e.g. wrapping an excerpt macro around it. Is there a way to fill one parameter, instead of the body, with the user's selected text? 

In other words, my ideal situation would be something like:

## @Param text:title=Text|type=string|required=true|desc=Body text|default=[some way of accessing the text selected by the user in the editor]

3 answers

1 accepted

1 vote
Answer accepted

I answered my own question, but ended up answering a couple of other questions along the way. It's not possible to define the user macro itself to pull the selection into a parameter. But!

What I really wanted to do was create a custom button on the editor toolbar that opened the macro browser and fed any selected text to the first parameter of my user macro. I don't have to ability to do the kind of plugin development that would "properly" add a button to the toolbar, so I had to resort to JavaScript hacks to make it all happen.

I'll leave out the JS that adds the custom button to the editor toolbar (basically: use an existing button to create the right HTML, use .after() to put it after an existing button). Here's the click event function that I attach to that custom button, with abstracted values.

AJS.$('#id-of-custom-button').click( function(){
var settings = {};
settings.selectedMacro = {
settings.onComplete = tinyMCE.confluence.macrobrowser.macroBrowserComplete;
settings.onCancel = tinyMCE.confluence.macrobrowser.macroBrowserCancel;;
function setFocus() {
if (!AJS.MacroBrowser.fields.param2) {
} else {;

Here's the breakdown:

  • Create an empty settings object
  • Add a selectedMacro object to the settings, with some minimum required items
    • including feeding the value of the currently selected text to the first parameter
  • Add the standard complete and cancel functions to the settings (which tells the macro browser what to do when you click "Save" or "Cancel", and if not specified, it causes problems)
  • Open the macro browser with the constructed settings, which fills the text parameter properly
  • Set focus on the second parameter, so the user isn't redundantly focused on the pre-filled text parameter. (But the focus setter is wrapped in a function that waits until the macro browser is rendered, because otherwise: problems.)

All of this JS goes at the very bottom of the Main Layout template. 

Again, this is just the click event handler. There's some preceding JS that adds the custom button.

So that's one way, at least, to pass selected text into a parameter, not the body, of a macro.

P.S. My user macro has two required parameters. If it had only one required parameter, it would be possible to use a different MacroBrowser function to insert it immediately on button click.

Bill Bailey Community Leader Jun 25, 2019

Well you might be surprised that that there is the ability to turn/package a user macro into a plug in (uses the same macro code, with some reworking). And within the same atlassian-plugin.xml file, you can add links to your macro in the insert macro menu.

For example, here is the Insert menu with two of my user macro (table title and figure title added)

Screenshot_2019-06-25 Edit - super test - test - MarketCom Wiki.png

This is a much cleaner solution and a good way to package user macros.

You can start here:

That's exactly what I was referring to when I wrote

I don't have to ability to do the kind of plugin development that would "properly" add a button to the toolbar, so I had to resort to JavaScript hacks to make it all happen.

Cleaner, yes, but outside of what I have access to. Because I don't want it in the macro list menu. I wanted an actual button in the editor toolbar, right up there next to bold, italic, etc.

Bill Bailey Community Leader Jun 25, 2019

Ah, but my point is I am stupid and was still able to do it. ;-) You just need to do the one tutorial on plugin development to get your environment set, and the community can help with questions.

Bill Bailey Community Leader Oct 04, 2019

Still don't see how selecting the text, then inserting the macro won't work. Any selected text is transferred to the body of the macro, which is saved as $body. You can then make the needed parameter equal to $body,

in Confluence Cloud I was able to do this by highlighting text, typing '{', selecting "Open Macro Browser", and selecting a macro like "No Format".


Sadly, other similar methods of macro selection (e.g., selecting text, then selecting macro from the drop down; or selecting text, typing '{' and typing macro name) didn't work the same, and replaced my highlighted text.

0 votes
Bill Bailey Community Leader Jun 25, 2019

This is a case where there is no documentation, and I had never thought about it as it becomes so automatic when I use macros. It is quite simple: all the selected text is written to the variable $body. You need no parameter at all.

(moved reply to correct location above)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Confluence Cloud

🏠 Say hello to the new Confluence Home!

Hi Atlassian Community, My name is DJ Chung, and I’m a Product Manager on the Confluence Cloud team. Today, I’m excited to share a new and improved version of Home. The new Home helps you ...

42,851 views 29 134
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you