Jump to content

Java Script - Find and Replace in Clipboard?


Recommended Posts

Please excuse my lack of knowledge about java as I am just a regular user. I use TextExpander and found a JavaScript that can help me edit my reports. This is what I use in TextExpander

// Start with the clipboard content
var result = TextExpander.pasteboardText;

// Replace double ..
result = result.replace(/\".."/g,". ");

// Replace duplicate words
result = result.replace(/\b(\w+)\s+\1\b /g, "$1 ");;

// Replace double spaces
result = result.replace(/  /g, " ");

Is there any way to make Alfred to the same? I know I can create a javascript workflow that can be triggers using a keyboard shortcut but how do I make it work with clipboard content?

Edited by slyfox
Link to comment

Do I sue keyword to script?

 

Do I use regular bin/bash language or choose JS in Alfred? 

var app = Application('System Events');
app.includeStandardAdditions = true;
var result = app.theClipboard();

// Replace double ..
result = result.replace(/\".."/g,". ");

// Replace duplicate words
result = result.replace(/\b(\w+)\s+\1\b /g, "$1 ");;

// Replace double spaces
result = result.replace(/  /g, " ");
Link to comment

Set a Hotkey with /usr/bin/osascript (JS) and nothing happens when I copy specific text to clipboard in text editor.

var app = Application('System Events');
app.includeStandardAdditions = true;
var result = app.theClipboard();

// Replace double ..
result = result.replace(/\".."/g,". ");

// Replace duplicate words
result = result.replace(/\b(\w+)\s+\1\b /g, "$1 ");;

// Replace double spaces
result = result.replace(/  /g, " ");
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...