Clipboard modification on copy by regex?

Is there a good way to automatically check copied content (on copy, not necessarily just copied WITH Command-C) & if it's plain text (basically if it's a url) & do some js regex replace on it?
I found an old thread, but they were basically looking fur a way to run something on a shortcut in a certain app. I'm more looking to see if I can hook into Clipboard Manager to trigger it on copy?

1 Like

I think this is currently not possible, but I think „on clipboard change“ would make a nice trigger. I’ll check on Monday!

1 Like

3.354 alpha now has a trigger for this, which can be combined with some simple BTT Java Script:

async function someJavaScriptFunction() {
		let clip = await get_clipboard_content();
		let clipTransformed = clip.toUpperCase();
		
		await set_clipboard_content(clipTransformed);
		
		return clipTransformed;
}
3 Likes

Amazing, thanks fam!

This is heccin dope, something I've been wanting for quite a while now…

Here's my code:

async function someJavaScriptFunction() {
	let clip = await get_clipboard_content();
	let clipTransformed = clip.replace(/https:\/\/www.youtube.com\/watch\?v=([^&]+).*/, 'https://youtu.be/$1')
	clipTransformed = clip.replace(/\/x.com\/([^\?]+).*/, '/x.com/$1');
	await set_clipboard_content(clipTransformed);
	return clipTransformed;
}

I'm minifying YouTube links & removing query parameters from x.com (TWITTER COUGH) URLs

2 Likes

I did more, launching IINA with like video URLs in clipboard!!

async function openIINAUrl() {
	let inp = `open -a IINA "$(pbpaste)"`,
	shellScriptWrapper = { script: inp, launchPath: '/bin/zsh' };
	let result = await runShellScript(shellScriptWrapper);
	console.log(inp);
	return inp;
}
async function clipboardManip() {
	let clip = await get_clipboard_content(), clipTransformed = clip+'';
	if (clip.match(/static1\.e621\..*\.(webm|mp4)/)||clip.match(/_v1[^\[]/)) {
		await set_clipboard_content(clip.replace(/\.(webm|mp4)/gi, 'mp4'));
		return await openIINAUrl();
	} else {
		//do things with clipboard
		clipTransformed = clipTransformed
		.replace(/https?:..(m\.|www\.)?youtube.com.watch\?v=([^&]+)(&t=[^&]+)?&?.*/, 'https://youtu.be/$2$3')
		.replace(/\/((www.)?twitter|x).com\/([^\?]+).*$/, '/fixvx.com/$3');
		// alert(clipTransformed);
		await set_clipboard_content(clipTransformed);
		return clipTransformed;
	}
}
1 Like

Nice! I have also mentioned this post briefly in this blog post folivora.ai - Great Tools for your Mac!

2 Likes

As I've already failed numerous attempts and still don't know what I'm doing lol - which method or scripting would you recommend?

I'd like to transform/replace certain words in the clipboard (i.e., abbreviating with medical terminology in a medical note, such as cardiology with cards or continue/continues/continued/continuing with cont) and automatically paste the transformed text. The vocab list is quite long, if that matters.

That's a great idea! I would change some stuff, like only applying those transformations if the copied text starts with a key phrase or something, but you could use two arrays that are associated with each other, like one with the shorthand and one with the extended medical terminology, then just select all, copy, then paste again to replace with the updated text? I'm not sure how to call a paste again from the JavaScript but I could set up a quick example for you. SOON™... :woman_shrugging:t3: I'm tired still :laughing:

Here we go, I put it in a message but I'll share it here, too:

I made an example preset to show how I'd handle it… I put an example in the bottom of the Run Real Javascript action. You'd put the replacements in the object at the top named, coincidentally, replacements.

Let me know if you have any problems?
Here's my example:
medical example trigger.bttpreset (4.6 KB)

1 Like

Here's the code from my solution posted above to anyone curious:

let replacements = {
  d5ASD: "DSM 5 Autistic Spectrum Disorder",
  DID: "Dissociative Identity Disorder",
  "some space from": "Just to show how it looks with spaces in between from items",
};
async function clipboardManip() {
  let clip = await get_clipboard_content();
  console.log(clip);
  if (clip.match(/Med5/)) {
    let text = clip;
    Object.keys(replacements).forEach(function eachKey(from) {
      to = replacements[from];
      text = replaceGlobally(text, from, to);
    });
    //alert(text);
    await set_clipboard_content(text);
    return "Replacements done, please paste again.";
  }
}
function replaceGlobally(original, s, re) {
  return original.replace(new RegExp(s, "g"), re);
}

/* copy from here */
let fail = `
Med5
This is a test, I have d5ASD.
DID is a thing a few of my friends have
I did be cute
catch
some space from
unchanging
`;
/*to here*/
1 Like

Bless you, sweet angel :innocent:

@Andreas_Hegenberg Can you add these text replacement stuff as a THING in BTT maybe? So we can edit a list of text replacements to modify on copy with maybe other options to specify when to apply the replacements? Or maybe keep an eye on what's being typed & modify on the fly? :woman_shrugging:t3:

key sequences basically do that (monitor what you are typing).

Combining a key sequence with the paste/typed text action will replace what was typed.
I’m working on an easy to use import format for these