Macro style in MS Word

Hi,
I have a weird question:
Is there a way to have a macro in MS Word and make it look the same way it does in the macro editor? ie for and if in blue, the comments in green, variables in black, commands in yellow, etc…
image

The reason being that I want to have macros in text documents with the classic macro style and to be able to copy it if necessary (hence I can’t just put an image as above)

Thanks a lot

@LPUoO,

Easiest / most reliable is a high-quality screenshot of the macro editor.

Not sure there exists a better way, but will defer to the script editor experts here.

John

1 Like

Hello John -

EXCEPT … Screenshots of text are EVIL. You can’t search or
copy-paste.

(Of course, Word is EVIL too, but screenshots of text in Word are
still EVIL …)

Thanks, mm

1 Like

Some editors have the option to export text with syntax highlighting as RTF or HTML. The colors may differ from the original ImageJ macro editor. I use PSPad for this.
Here an example, text copied from the editor in Word (screenshot to keep the exact formatting):

image
Peter

I tried to hack something together using the script editor content and feeding it to the itextpdf library (included in Fiji) – mostly based off of this stackoverflow answer – but I didn’t get it working:

#@ ImageJ ij
#@ File (style="save") outputFile

import org.scijava.ui.swing.script.TextEditor

editor = new TextEditor(ij.context())

//println editor

editor.setVisible(true)

pane = editor.getEditorPane()
pane.setLanguageByFileName(".groovy")

rsdoc = pane.getRSyntaxDocument()

rsdoc.getContent().insertString(0, "import ij.IJ\nprintln 42")

println rsdoc
println rsdoc.getContent().getString(0, 15)

// itext stuff
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.Document
import com.itextpdf.awt.PdfGraphics2D

document = new Document()
outputStream = new FileOutputStream(outputFile)

writer = PdfWriter.getInstance(document, outputStream)
document.open()

contentByte = writer.getDirectContent()

g2d = new PdfGraphics2D(contentByte, 725, 400)

println g2d

// RSyntaxTextArea print utils
import org.fife.print.RPrintUtilities
import java.awt.print.PageFormat
import java.awt.Graphics

RPrintUtilities.printDocumentMonospacedWordWrap(
		g2d,
		rsdoc,
		12,
		0,
		new PageFormat(),
		4
	)

document.close()

outputStream.close()

It throws:

com.itextpdf.text.exceptions.IllegalPdfSyntaxException: Unbalanced save/restore state operators.

(see also the comments on this unanswered stackoverflow question)


Maybe someone else can figure out how to fix this. I think an Export to PDF button would be a great addition to the script editor :slight_smile:

3 Likes

Thank you so much for helping @imagejan.

I wish I could help with but unfortunately it is way beyond my capabilities.

I agree, having an export to PDF or Word button in the script editor would be fantastic.