Markup bold word on clipboard

Is it possible to surround a bold word in a sentence on the clipboard with asterisks?

I want to paste the clipboard content as plain text without any formatting, but indicate which words where bold with *.

I don’t know of any way to convert styled text to Markdown (or other structured format). There might be an application that could do that that might be scriptable, but off hand I have no idea how you would do it without actual code (or maybe JXA could do it).

You might checkout this web site to see if it will help:
Markdown Services

This is a set of OS X services that contain the following commands:

  • Markdown to HTML: Replaces selected text written in Markdown with the corresponding HTML.
  • HTML to Markdown: The reverse of the previous: replaces selected HTML with the corresponding Markdown.
  • Copy Markdown as RTF: Copies the selected Markdown text to the clipboard as rich text, suitable for pasting into TextEdit or a word processor.
  • Paste as Markdown: The reverse of the previous: converts rich text on the clipboard into Markdown and pastes it into a document.

Thank you for pointing me in the right direction!

I installed the scripts Copy Markdown as RTF and Paste as Markdown and they are indeed very useful to me.

However, when I copy German characters (äöüÄÖÜß), these are converted wrong by Copy Markdown as RTF.

Would it be possible to have these characters treated correctly? I know of course that this exceeds the scope of this forum, but perhaps it's a a matter of adding äöüÄÖÜß somewhere ;).

I also have an issue with Copy Markdown as RTF: it doesn't appear in the Services menu (whereas Paste as Markdown is visible). Do you have any idea why?

Just got this response from the developer of the services:

I’m not sure why “Copy Markdown as RTF” wouldn’t appear for you in the Services menu – it does for me. It will only appear if you have text selected, though.

It does look like there’s a simple solution – I’ll have to update the archive when I get a chance, but you can actually make the change to the service directly using Automator. If you open “Copy Markdown as RTF”, there are three “Run Shell Script” blocks, and the last one is just one line that reads

/usr/bin/textutil -stdin -stdout -convert rtf -format html | /usr/bin/pbcopy

What you need to do is change that to:

/usr/bin/textutil -inputencoding UTF-8 -stdin -stdout -convert rtf -format html | /usr/bin/pbcopy

(I didn’t know about the “-inputencoding” option until just now!)

It doesn’t look like “Paste as Markdown” has the same problem, so hopefully it won’t need any changes.


That’s just great!

Now I have another question: How can I call these services from Keyboard Maestro? (I can simulate the key press, but there must be a more robust solution). I want to integrate them into a workflow.

Here is a variant which requires the excellent Pandoc to be installed from the standard .pkg installation
http://pandoc.org/installing.html

(but doesn't require anything else beyond Keyboard Maestro)

Paste rich text as markdown (using pandoc).kmmacros (20.3 KB)

Source of the JavaScript for Automation action

(function () {
	'use strict';

	ObjC.import('AppKit');

	// Types: 'public.rtf', 'public.html' etc
	function pboardUnpacked(strType) {
		return ObjC.unwrap(
			$.NSPasteboard.generalPasteboard.stringForType(
				strType
			)
		)
	}
	var a = Application.currentApplication(),
		sa = (a.includeStandardAdditions = true, a),
		strRTF = pboardUnpacked('public.rtf');

	if (strRTF) {

		var strTempPath = sa.pathTo('temporary items', {
			from: 'user domain'
		}).toString() + '/tmp.rtf';

		// remove any existing temp file
		sa.doShellScript('rm -f "' + strTempPath + '"');

		// Write a temporary file
		$.NSString.alloc.initWithUTF8String(strRTF)
			.writeToFileAtomically(strTempPath, true);

		// Update clipboard with markdown
		return sa.doShellScript(
			'textutil -format rtf -convert html -inputencoding UTF-8  -stdout ' +
			'"' + strTempPath + '" | /usr/local/bin/pandoc -r html -w markdown_mmd'
		)
		.replace(/<br>/g, '')
		.replace(
			/<\/?span[^>]*>/g, ''
		);

	} else return sa.theClipboard();

})();
2 Likes

Thanks Rob. This looks very interesting, maybe just what I needed.

Unfortunately I'm getting this error:

2016-02-04 20:37:20.939 osascript[33374:2469754] warning: failed to get scripting definition from /usr/bin/osascript; it may not be scriptable.

I did verify that pandoc is installed at /usr/local/bin/pandoc

I copied text from this web page (the Linux section).
http://pandoc.org/installing.html

And the opened TextWrangler, and ran the macro.

Running KM 7.0.3 on Yosemite (10.10.5)

Suggestions?

Hi JMichael

Have you restarted between the install and your test?
In my experience some software is only properly recognised by the system after a restart.

1 Like

That's a generic error message which osascript always seems to put out with the -l JavaScript switch. The first thing to do is to use the settings control at the top left of the action to disable the inclusion of error messages in the output stream.

I'm not seeing that message myself, even with Include Errors checked, so I guess my first questions might be:

  • Is that OS X 10.10 or 10.11 ? (ok, I've seen your edit - Yosemite could be an issue, but unchecking 'include errors' should still work, I think.
  • Is the JavaScript in the action showing syntax-highlighting colours, or black and white ?

( Peter will have a deeper grasp of the mechanics here, but I notice that compilation of a script (and the switch to syntax coloring) seems to happen as soon as a macro loses focus. I sometimes briefly select another macro, and then return).

( PS I've uploaded a version 0.2 (above), to improve the regex which aimed to remove redundant span tags)

PS not sure whether you were using Safari or Chrome, but I notice that Chrome doesn’t put public.rtf content into the clipboard from the page which you used for your test.

One exercise might be to test for formats on the clipboard, and pass any public.html straight to pandoc (possibly through tr if anything needs to be cleaned up), falling back to a textutil rtf -> html convert if there is rtf but no html in the clipboard, and falling straight through to plain text if there is neither.

That removed the error msg from the output, but it is still distrubing that it shows in Yosemite but not El Capitan.

Color. What does it mean if it is in B&W?

Thanks. May I suggest that when you make an update to a Macro, that you post the date updated and ver# in the text above the macro, and in the macro comments.

I have tried both. The macro works for Safari, but not for Chrome. No markdown is output for Chrome.

I would love to, but I can't follow your JXA code because it is packed so tightly, combining multiple commands on one line. This tight packing makes it very hard to read, test, and modify. Perhaps for instructional/learning purposes in this forum, you could separate things out a bit. More comments would also be helpful. It would be a great help to all of us less schooled in JXA than you. Remember, what is obvious to you may not be obvious to others. :wink:

But I do really appreciate all of the code and macros that you share here. Just trying to learn.

Hello ComplexPoint:

Can you do this to Latex?
I was using http://rtf2latex2e.sourceforge.net to convert my rtf to Latex: by exporting my notes in Tinderbox to rtf. But, a direct pasting of RTF to the Latex editor would be a super fast process. Can you adopt this for pasting in Latex format please?
(I already have Pandoc)