Paste clean links from clipboard

Links often contain many different query parameters.

How do I create an action that pastes a cleaned version of a link from the clipboard?

Notes

  1. I will assign the action to fn+Shift+V
  2. I do not want the text in clipboard to be altered (I still want ⌘+V to paste the dirty version)

Example of dirty link
https://www.amazon.com/International-Brompton-Bike-Bag-Foldon/dp/B00KKLAJPK/?_encoding=UTF8&pd_rd_w=PFhtA

Example of cleaned link
https://www.amazon.com/International-Brompton-Bike-Bag-Foldon/dp/B00KKLAJPK/

You can use the "Run Real Java Script" action and do something like this:

async function someJavaScriptFunction() {
	const url = await get_clipboard_content();
	const index = url.indexOf('?');

    // If the '?' character is found, return the substring from the beginning to the position of '?'
    // Otherwise, return the original URL
    return index > -1 ? url.substring(0, index) : url;
}

Hi Andreas, thank you for your help.

It looks like the javascript works and did clean the link in my clipboard (see Script Result in my screenshot).

However, it seems the action is lacking some kind of paste function. When I press shift+fn+V on my keyboard (which is the trigger) in TextEdit, nothing happens.

What should I do?

ah sorry, use this instead:

async function someJavaScriptFunction() {
	const url = await get_clipboard_content();
	const index = url.indexOf('?');

    // If the '?' character is found, return the substring from the beginning to the position of '?'
    // Otherwise, return the original URL
    const cleanedURL = index > -1 ? url.substring(0, index) : url;
    await paste_text({text: cleanedURL, insert_by_pasting: true})
    return cleanedURL;
}
2 Likes

Works well, but the code also cleans the link in my clipboard.

This means after that BTT action fires, my clipboard also becomes the cleaned.

Is there a way for the javascript to leave the clipboard's original link, but allow me to paste a clean version?

To put it simply:
• I want shift+fn+V to paste a cleaned link
• I want ⌘+V to continue to give me the original dirty link

Ah yes, the paste_text action will put the new link n the clipboard. Starting with version 4.404 it should work like this:

async function someJavaScriptFunction() {
	const url = await get_clipboard_content();
	const index = url.indexOf('?');

    // If the '?' character is found, return the substring from the beginning to the position of '?'
    // Otherwise, return the original URL
    const cleanedURL = index > -1 ? url.substring(0, index) : url;
		
	
    await paste_text({text: cleanedURL, insert_by_pasting: true});
    await set_clipboard_content({content:url, format: 'NSPasteboardTypeString'});

 
    return cleanedURL;
}
3 Likes