Clever way to insert last word of current sentence at current cursor position


What would be a smart way to insert the last word of a sentence (which is placed on the clipboard) at the current cursor position?

E.g. Zwölf Boxkämpfer jagen Viktor zum Spaß quer über den Sylter Deich.

Cursor at the start of ‘Viktor’, execution of the macro results in:

Zwölf Boxkämpfer jagen Deich Viktor zum Spaß quer über den Sylter.

Preferably the macro would include smart space handling too :).

Many thanks,


Dear Christophe,

Could your beloved Satimage perhaps provide a solution here?

I’ve found this:

find text “(.+) (.+)” in “Mickey Mouse” using {“Dest: Mr \2”, “Dear \1,”} with regexp and string result


First, count the number of words on the clipboard.
Then determine the number of the word before which the text cursor is positioned: Viktor.
Finally, swap word d with word word k. Viktor with Deich.
Correct the spaces.

Probably better to handle spaces together with the words to swap. But how :smile:

Hey Hans,

It seems like I've posted something similar to this before.

This macro processes the current line according to your example above. (It does not use previously entered clipboard content, although of course it could be modified to do so.)

Keep in mind that cursor position is not something OSX keeps track of, so if you're not using a text editor that is a scriptable as BBEdit (or TextWrangler) you have to use a work-around.

Best Regards,

NOTE: Script REQUIRES the Satimage.osax AppleScript Extension.

Insert Last Word of Sentence at Cursor Position.kmmacros (4.1 KB)

Thank you Chris, I will study and test this. I just opened this thread again to post my newest idea for an approach: type $$ and then let Keyboard Maestro replace $$ by the first word from the right. I think you have used that approach too :slight_smile: will have a a careful look.

Thanks again!


Hello Chris,

Is it sufficient to install Satimage398.pkg or should I register the Satimage Dictionary (?Library) in the ScriptEditor too?

I’m not sure whether the Satimage is working okay now.

I’ve opened TextWrangler, typed a sentence, placed the correct ^, copied the sentence to the clipboard and ran everything from the Script down to the end of the macro (I’ve disabled 3 Keystroke commands). The whole sentence is inserted now, instead of replacing the ^with the last word.


Hey Hans,


What do you mean by this?

The Satimage.osax installer package installs 1 OSAX (Open Scripting Architecture Extension) file.

It’s a good idea to reboot after installing it.

Nothing else needs to be done to register it.

Run this in the Applescript Editor:

set _text to "Now is the time for all good men to come to the aid of their country."
change ".*" into "•" in _text with regexp without case sensitive

If everything is working correctly the result should be:


The macro won’t work in TextWrangler, because TextWrangler doesn’t respect Cocoa app’s Emacs keybindings completely. You usually work in CafeTran, so I planned for that. If you’re using TextWrangler then other things are possible.

You don’t place the “ˆ”. The macro inserts it as a placeholder for the cursor location.

You don’t copy the sentence to the clipboard. The macro does it.

Why would you do that and think the macro would work?

Take the original macro and run it on this sentence in TextEdit or CafeTran:

Dietrich was a total fox Marlene.

First place the cursor before the “D” in “Dietrich”.

Best Regards,

Hello Chris,

Yep, the macro is meant for CafeTran. Why did I disable the keystroke commands? Wanted to understand. Thinking too much, knowing too little. Anyway, I have the macro working now for TextEdit. Will have to fine tune it for CafeTran (Java), which almost certainly will be possible (all other Keyboard Maestro macros work perfect in CafeTran).

What a nice regular expressions library this is. I’m glad that I finally installed it :).

Have nice day!


BTW: After I had installed Satimage OSAX on my iMac too, I noticed that my CafeTran macros did work anymore. I had to reselect CafeTran under ‘Available in the following applications’ (then they worked again). Can this be caused by adding the new software?

Hey Hans,

I can’t think of any reason the Satimage.osax would interfere with Keyboard Maestro’s Application linkages.

Best Regards,

Hello Chris,

Later I remembered that I have updated CafeTran to a special version for Java 8 on my MacBook Pro, perhaps this causes the effect? (On my iMac I still use the version for Java 6, for production.)

One other thing, the CafeTran implementation is ready now. It works, but occasionally I get this error:

/var/folders/sz/jcwhxcn10wzbctnwkn4901bh0000gn/T/Keyboard-Maestro-Script-7638715B-F709-4510-916B-FC77EBAD07F8:47:129: execution error: No result was returned from some part of this expression. (-2763

Does this ring any bell?



[quote=“HansL, post:11, topic:1006”]One other thing, the CafeTran implementation is ready now. It works, but occasionally I get this error:

…execution error: No result was returned from some part of this expression. (-2763

Does this ring any bell?[/quote]

Hey Hans,

Sorry to take so long to respond - this got lost in my browser tabs.

I’m guessing that for some reason the find text command fails. When it does it throws an error.

Try replacing the AppleScript with this:

set _text to the clipboard
  set wordToInsert to find text "(\\w+)[[:punct:]]*$" in _text using "\\1" with regexp and string result
on error
end try
set _text to change "[[:blank:]]*\\w+([[:punct:]]*$)" in _text into "\\1" with regexp
set _text to change "ˆ" in _text into wordToInsert & " " with regexp

At least you’ll have some feedback.

If you have more failure send me the text that fails off-list to, so I can test to see why the regex doesn’t work in that case.

Best Regards,

Hello Chris,

Thanks for looking into this again!

I now have CafeTran working for 100 % on my new Retina. Obviously, there was a problem with permissions inside the app package. It took me a very long time to eliminate all possible causes. Yesterday Igor sent me a new package and after installing it, I could open an individual file through the native OS X file chooser that now is called by CafeTran. I can now use these nice dialogue boxes with their Favourites, preview etc. :).

One thing that has changed by "migrating" CafeTran to Java 8, however, is the fact that it can no longer be detected by other apps. It is visible in Yosemite's Alt+Tab cycle, but apps like Witch, TextExpander or Keyboard Maestro cannot detect it.

So there is now way back for me, once leaving CafeTran, e.g. for executing an AppleScript.

Luckily I was able to rewrite and even simplify most of my CafeTran macros, using variables instead of clipboard manipulations. Changed text is now typed into CafeTran, instead of pasting it from the clipboard. It looks a little clumsy, but hey, it works :).

Here is an example:



Hey Hans,

I can force AppleScript to see it, but things that should work don’t.

This works:

set the clipboard to "My Text!"

tell application "Keyboard Maestro Engine"
end tell

set cafeTranAppPath to ((path to startup disk as text) & "Applications:Applications_Chris:Language and Translation")

tell application cafeTranAppPath
  if running = false then
  end if
end tell

This did NOT work properly:

tell application "CafeTran"
end tell

This did NOT work properly:

tell application id "com.cafetran.CafeTran"
end tell

Keyboard Maestro can see jEdit (a java-based text editor) just fine.

Likewise AppleScript can see it just fine via both bundle ID and app-name.

My conclusion is that the author has made a mistake somewhere, and this issue cannot simply be chalked up to Java 8.

Best Regards,

1 Like

Hey Chris,

Thanks for looking into this! I can conform that jEdit can be activated (although I had to choose it manually, since it isn't listed in KM's list of recent apps either).

With your great help I've made some progress in reverting my macros to the clipboard (instead of send keys) approach again (see example). The part with the S/R is for handling of any tags in the Target Language pane content.


Make uppercase copy.kmmacros (9.3 KB)