Find and replace function

Trying to create a find and replace all function

IDK if there's a better, easier, more direct way of doing this.

Tried using KM, it works well and it's fast, it just doesn't process selected text, I need to copy it..

Hey @Andreas_Hegenberg

Still need help with this :slightly_smiling_face:

This is what I have so far:
Arc 2024-11-06 2.04.14 PM

Arc 2024-11-06 2.04.17 PM

Used ChatGPT to generate the script, IDK if it's correct

what happens when you try this? The code does look fine

Tried in google Docs, it doesn't do anything... IDK What's wrong..

could you copy & paste the whole trigger? Then I can try here

Sure thing

[
  {
    "BTTActionCategory" : 0,
    "BTTLastUpdatedAt" : 1730897656.013896,
    "BTTTriggerType" : 773,
    "BTTTriggerTypeDescription" : "Standard Item",
    "BTTTriggerParentUUID" : "C5007534-F241-42A2-912B-96E73AA5C0C2",
    "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
    "BTTUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
    "BTTPredefinedActionType" : 403,
    "BTTPredefinedActionName" : "Ask For Input (Save To Variable)",
    "BTTAdditionalActionData" : {
      "BTTActionAskForInputVariableName" : "replacetext_find",
      "BTTActionAskForInputPrompt" : "FIND:"
    },
    "BTTAdditionalConfiguration" : "Menu Item: _lowercaps",
    "BTTEnabled" : 1,
    "BTTOrder" : 11,
    "BTTDisplayOrder" : 1,
    "BTTMenuItemActions" : [
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1730898522.105094,
        "BTTTriggerType" : 773,
        "BTTTriggerTypeDescription" : "Standard Item",
        "BTTTriggerParentUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
        "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
        "BTTUUID" : "94F6068C-CD71-4E7E-BE37-206DD2940288",
        "BTTPredefinedActionType" : 284,
        "BTTPredefinedActionName" : "Transform & Replace Selection With Java Script",
        "BTTAdditionalActionData" : {
          "SelectedTransformerTab" : 1,
          "BTTClipboardTransformerJS" : "async (clipboardContentString) => {\n    const findText = await BetterTouchTool.get_string_variable(\"replacetext_find\");\n    const replaceText = await BetterTouchTool.get_string_variable(\"replacetext_replace\");\n\n    if (findText) {\n        return clipboardContentString.replace(new RegExp(findText, 'g'), replaceText || \"\");\n    }\n    return clipboardContentString;\n}"
        },
        "BTTGenericActionConfig" : "find and replace all",
        "BTTAdditionalConfiguration" : "Menu Item: FIND & Replace",
        "BTTEnabled" : 1,
        "BTTOrder" : 1,
        "BTTDisplayOrder" : 1,
        "BTTTriggerName" : "Menu Item: FIND & Replace",
        "BTTMenuConfig" : {
          "BTTMenuItemImageHeight" : 40,
          "BTTMenuItemIconColor1" : "255.000000, 255.000000, 255.000000, 255.000000",
          "BTTMenuItemImageOffsetX" : 0,
          "BTTMenuItemDisplayOrder" : 1,
          "BTTMenuItemBackgroundColor" : "0.000000, 0.000000, 0.000000, 155.322861",
          "BTTMenuItemBackgroundType" : 4,
          "BTTMenuItemBlurredBackground" : 0,
          "BTTMenuSelectedTab" : 0,
          "BTTMenuItemMaxHeight" : 170,
          "BTTMenuItemPaddingLeft" : 5,
          "BTTMenuItemMaxWidth" : 170,
          "BTTMenuCategoryItemIcon" : 1,
          "BTTMenuItemPaddingTop" : 5,
          "BTTMenuCategoryShadow" : 0,
          "BTTMenuItemIconColor2" : "255.000000, 255.000000, 255.000000, 255.000000",
          "BTTMenuCategoryItemSpacing" : 0,
          "BTTMenuItemIconTypeDark" : 2,
          "BTTMenuCategoryModifiers" : 0,
          "BTTLastChangeUUID" : "37CB7CE9-E15D-46C5-BF50-833313BD9BB8",
          "BTTMenuItemSFSymbolName" : "magnifyingglass",
          "BTTMenuItemImageOffsetY" : 0,
          "BTTMenuItemIconPosition" : 1,
          "BTTMenuItemSFSymbolWeight" : 8,
          "BTTMenuItemMinHeight" : 170,
          "BTTMenuItemSFSymbolStyle" : 0,
          "BTTMenuItemVisibleWhileActive" : 1,
          "BTTMenuElementIdentifier" : "FIND & Replace",
          "BTTMenuItemVisibleWhileInactive" : 1,
          "BTTMenuItemMinWidth" : 170,
          "BTTMenuItemPaddingRight" : 5,
          "BTTMenuAttributedText" : "{\\rtf1\\ansi\\ansicpg1252\\cocoartf2820\n\\cocoatextscaling0\\cocoaplatform0{\\fonttbl\\f0\\fnil\\fcharset0 SFPro-Regular;}\n{\\colortbl;\\red255\\green255\\blue255;\\red255\\green255\\blue255;}\n{\\*\\expandedcolortbl;;\\cssrgb\\c100000\\c100000\\c100000;}\n\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc\\partightenfactor0\n\n\\f0\\fs50 \\cf2 FIND & Replace}",
          "BTTMenuItemIconType" : 2,
          "BTTMenuItemIconColor3" : "255.000000, 255.000000, 255.000000, 255.000000",
          "BTTMenuAppearanceStyle" : 1,
          "BTTMenuItemPaddingBottom" : 5,
          "BTTMenuAlwaysUseLightMode" : 1,
          "BTTMenuCategoryBackground" : 1,
          "BTTMenuCategoryItemSizing" : 1
        },
        "BTTMenuAvailability" : 0,
        "BTTMenuName" : "FIND & Replace",
        "BTTGestureNotes" : "Standard Item"
      },
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1730897633.6531181,
        "BTTTriggerParentUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
        "BTTUUID" : "096D48D7-3659-4F43-B0F3-8D0DB600BF32",
        "BTTPredefinedActionType" : 403,
        "BTTPredefinedActionName" : "Ask For Input (Save To Variable)",
        "BTTAdditionalActionData" : {
          "BTTActionAskForInputVariableName" : "replacetext_replace",
          "BTTActionAskForInputPrompt" : "Replace with:"
        },
        "BTTAdditionalConfiguration" : "Menu Item: _lowercaps",
        "BTTEnabled" : 1,
        "BTTOrder" : 0,
        "BTTMenuAvailability" : 0,
        "BTTMenuName" : "096D48D7-3659-4F43-B0F3-8D0DB600BF32"
      }
    ],
    "BTTMenuConfig" : {
      "BTTMenuItemImageHeight" : 40,
      "BTTMenuItemIconColor1" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTMenuItemImageOffsetX" : 0,
      "BTTMenuItemDisplayOrder" : 1,
      "BTTMenuItemBackgroundColor" : "0.000000, 0.000000, 0.000000, 155.322861",
      "BTTMenuItemBackgroundType" : 4,
      "BTTMenuItemBlurredBackground" : 0,
      "BTTMenuSelectedTab" : 0,
      "BTTMenuItemMaxHeight" : 170,
      "BTTMenuItemPaddingLeft" : 5,
      "BTTMenuItemMaxWidth" : 170,
      "BTTMenuCategoryItemIcon" : 1,
      "BTTMenuItemPaddingTop" : 5,
      "BTTMenuCategoryShadow" : 0,
      "BTTMenuItemIconColor2" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTMenuCategoryItemSpacing" : 0,
      "BTTMenuItemIconTypeDark" : 2,
      "BTTMenuCategoryModifiers" : 0,
      "BTTLastChangeUUID" : "37CB7CE9-E15D-46C5-BF50-833313BD9BB8",
      "BTTMenuItemSFSymbolName" : "magnifyingglass",
      "BTTMenuItemImageOffsetY" : 0,
      "BTTMenuItemIconPosition" : 1,
      "BTTMenuItemSFSymbolWeight" : 8,
      "BTTMenuItemMinHeight" : 170,
      "BTTMenuItemSFSymbolStyle" : 0,
      "BTTMenuItemVisibleWhileActive" : 1,
      "BTTMenuElementIdentifier" : "FIND & Replace",
      "BTTMenuItemVisibleWhileInactive" : 1,
      "BTTMenuItemMinWidth" : 170,
      "BTTMenuItemPaddingRight" : 5,
      "BTTMenuAttributedText" : "{\\rtf1\\ansi\\ansicpg1252\\cocoartf2820\n\\cocoatextscaling0\\cocoaplatform0{\\fonttbl\\f0\\fnil\\fcharset0 SFPro-Regular;}\n{\\colortbl;\\red255\\green255\\blue255;\\red255\\green255\\blue255;}\n{\\*\\expandedcolortbl;;\\cssrgb\\c100000\\c100000\\c100000;}\n\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc\\partightenfactor0\n\n\\f0\\fs50 \\cf2 FIND & Replace}",
      "BTTMenuItemIconType" : 2,
      "BTTMenuItemIconColor3" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTMenuAppearanceStyle" : 1,
      "BTTMenuItemPaddingBottom" : 5,
      "BTTMenuAlwaysUseLightMode" : 1,
      "BTTMenuCategoryBackground" : 1,
      "BTTMenuCategoryItemSizing" : 1
    },
    "BTTMenuAvailability" : 0,
    "BTTMenuName" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
    "BTTGestureNotes" : "Standard Item"
  }
]

try this instead:

async (clipboardContentString) => {
    const findText = await get_string_variable("replacetext_find");
    const replaceText = await get_string_variable("replacetext_replace");

    if (findText) {
        const regex = new RegExp(findText, 'g');
        return clipboardContentString.replace(regex, replaceText || "");
    }
    return clipboardContentString;
}

[
  {
    "BTTActionCategory" : 0,
    "BTTLastUpdatedAt" : 1730927244.6270862,
    "BTTTriggerType" : 773,
    "BTTTriggerTypeDescription" : "Standard Item",
    "BTTTriggerParentUUID" : "FBC77E7A-90EF-4D00-9BB0-354553194C7C",
    "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
    "BTTUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
    "BTTPredefinedActionType" : 403,
    "BTTPredefinedActionName" : "Ask For Input (Save To Variable)",
    "BTTAdditionalActionData" : {
      "BTTActionAskForInputVariableName" : "replacetext_find",
      "BTTActionAskForInputPrompt" : "FIND:"
    },
    "BTTAdditionalConfiguration" : "Menu Item: _lowercaps",
    "BTTEnabled" : 1,
    "BTTOrder" : 9,
    "BTTDisplayOrder" : 1,
    "BTTTriggerName" : "Menu Item: E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
    "BTTMenuItemActions" : [
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1730927296.984045,
        "BTTTriggerParentUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
        "BTTUUID" : "E394C5E3-0C54-4933-890A-64596E4B0DE8",
        "BTTPredefinedActionType" : 345,
        "BTTPredefinedActionName" : "Pause Execution  or  Delay Next Action (async  or  not blocking)",
        "BTTDelayNextActionBy" : "0.1",
        "BTTEnabled" : 1,
        "BTTOrder" : 1,
        "BTTMenuAvailability" : 0,
        "BTTMenuName" : "E394C5E3-0C54-4933-890A-64596E4B0DE8"
      },
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1730927251.4466329,
        "BTTTriggerParentUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
        "BTTUUID" : "7ABA0BBD-9FE3-459F-9BC1-1C5451A56D17",
        "BTTPredefinedActionType" : 284,
        "BTTPredefinedActionName" : "Transform & Replace Selection With Java Script",
        "BTTAdditionalActionData" : {
          "BTTClipboardTransformerExampleInput" : "thello quak",
          "BTTClipboardTransformerJS" : "async (clipboardContentString) => {\n    const findText = await get_string_variable(\"replacetext_find\");\n    const replaceText = await get_string_variable(\"replacetext_replace\");\n\n    if (findText) {\n        const regex = new RegExp(findText, 'g');\n        return clipboardContentString.replace(regex, replaceText || \"\");\n    }\n    return clipboardContentString;\n}\n"
        },
        "BTTEnabled" : 1,
        "BTTOrder" : 2,
        "BTTMenuAvailability" : 0,
        "BTTMenuName" : "7ABA0BBD-9FE3-459F-9BC1-1C5451A56D17"
      },
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1730927251.4466381,
        "BTTTriggerParentUUID" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeFloatingMenu",
        "BTTUUID" : "096D48D7-3659-4F43-B0F3-8D0DB600BF32",
        "BTTPredefinedActionType" : 403,
        "BTTPredefinedActionName" : "Ask For Input (Save To Variable)",
        "BTTAdditionalActionData" : {
          "BTTActionAskForInputVariableName" : "replacetext_replace",
          "BTTActionAskForInputPrompt" : "Replace with:"
        },
        "BTTAdditionalConfiguration" : "Menu Item: _lowercaps",
        "BTTEnabled" : 1,
        "BTTOrder" : 0,
        "BTTMenuAvailability" : 0,
        "BTTMenuName" : "096D48D7-3659-4F43-B0F3-8D0DB600BF32"
      }
    ],
    "BTTMenuConfig" : {
      "BTTMenuItemImageHeight" : 40,
      "BTTMenuItemIconColor1" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTMenuItemImageOffsetX" : 0,
      "BTTMenuItemDisplayOrder" : 1,
      "BTTMenuItemBackgroundColor" : "0.000000, 0.000000, 0.000000, 155.322861",
      "BTTMenuItemBackgroundType" : 4,
      "BTTMenuItemBlurredBackground" : 0,
      "BTTMenuSelectedTab" : 0,
      "BTTMenuItemMaxHeight" : 170,
      "BTTMenuItemPaddingLeft" : 5,
      "BTTMenuItemMaxWidth" : 170,
      "BTTMenuCategoryItemIcon" : 1,
      "BTTMenuItemPaddingTop" : 5,
      "BTTMenuCategoryShadow" : 0,
      "BTTMenuItemIconColor2" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTMenuCategoryItemSpacing" : 0,
      "BTTMenuItemIconTypeDark" : 2,
      "BTTMenuCategoryModifiers" : 0,
      "BTTLastChangeUUID" : "37CB7CE9-E15D-46C5-BF50-833313BD9BB8",
      "BTTMenuItemSFSymbolName" : "magnifyingglass",
      "BTTMenuItemImageOffsetY" : 0,
      "BTTMenuItemIconPosition" : 1,
      "BTTMenuItemSFSymbolWeight" : 8,
      "BTTMenuItemMinHeight" : 170,
      "BTTMenuItemSFSymbolStyle" : 0,
      "BTTMenuItemVisibleWhileActive" : 1,
      "BTTMenuElementIdentifier" : "FIND & Replace",
      "BTTMenuItemVisibleWhileInactive" : 1,
      "BTTMenuItemMinWidth" : 170,
      "BTTMenuItemPaddingRight" : 5,
      "BTTMenuAttributedText" : "{\\rtf1\\ansi\\ansicpg1252\\cocoartf2820\n\\cocoatextscaling0\\cocoaplatform0{\\fonttbl\\f0\\fnil\\fcharset0 SFPro-Regular;}\n{\\colortbl;\\red255\\green255\\blue255;\\red255\\green255\\blue255;}\n{\\*\\expandedcolortbl;;\\cssrgb\\c100000\\c100000\\c100000;}\n\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc\\partightenfactor0\n\n\\f0\\fs50 \\cf2 FIND & Replace}",
      "BTTMenuItemIconType" : 2,
      "BTTMenuItemIconColor3" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTMenuAppearanceStyle" : 1,
      "BTTMenuItemPaddingBottom" : 5,
      "BTTMenuAlwaysUseLightMode" : 1,
      "BTTMenuCategoryBackground" : 1,
      "BTTMenuCategoryItemSizing" : 1
    },
    "BTTMenuAvailability" : 0,
    "BTTMenuName" : "E6FB96CB-D0F9-43F6-87B0-7A7083E9A6E7",
    "BTTGestureNotes" : "Standard Item"
  }
]

The short delay is important because apparently it takes a few milliseconds until the input field is hidden - if it is still visible when the transform action is called, it will try transform the contents of the textfield.

1 Like

It works perfectly now!
Many Thanks for the support!

Another quick question @Andreas_Hegenberg

I have an issue with this

Arc 2024-11-11 6.29.02 PM

I can't use my clipboard manager paste while the input fields is up or even go copy and paste something in that field, as Soon click away, the input fields submits automatically and disappears...

image
I don't see any setting here to "Keep floating on top"

Please let me know if there's a feature like that coming soon, or if there's a workaround in the meanwhile..

Thanks!

Hey @Andreas_Hegenberg
Any input here would be greatly appreciated.

I'll add an option to only close when hitting enter with the next version!

Amazing!
I'm using paste as a clipboard manager, hopefully I'm gonna be able to paste text from it into that field.

Many Thanks!!!

The option is now available in the 4.904 alpha:

That's awesome, the only problem I have with it is, it doesn't accept text from my clipboard manager directly, it just replaces the selected text I wanna process.

Also, when I copy something to my clipboard while having the text selected, it doesn't work, the replacement doesn't work, I have to leave that text unselected until and copied and pasted everything for it to work..., I suppose the clipboard is the temp storage for those variables?

This is still my setup:


I think I might not fully understand your usecase. Could you maybe post the steps I should try? (or create a quick screen capture)

This is a loom detailing my use case:

Ah I see, most likely the clipboard manager doesn't "see" that BetterTouchTool input window because it doesn't capture full focus. However the clipboard manager seems to capture focus, which leads to a confusing state.

In v4.906 I added an option to capture focus, maybe this will help in your scenario:

1 Like

That works perfectly!
Thank you so much! :pray: