Text Selection Did Change
Download the example preset here: https://folivora.ai/releases/text-selection-base.bttpresetzip

Introduction
Since BetterTouchTool 5.133, there's a trigger called "Text Selection Did Change" in the "Automations & Named & Other Triggers" section (group "General"). It fires whenever you select new text.
It's a simple trigger, but it becomes very powerful when combined with BetterTouchTool's Floating Menus and BTT's various scripting capabilities.
I know there are other great tools like PopClip that do similar things, but I think the power and flexibility of BTT's Floating Menus really make this shine.
Have a look at this example:
Some examples of actions you could add to such a menu:
- Search a selected address in Apple / Google Maps
- Search the selected text in your favorite search engine or on another website
- Quickly run a Shortcut from the Shortcuts app on the selected text
- Copy / Cut / Paste
- Open a link
- Change the formatting of the selected text, e.g. strike through
- Copy the formatting of one string and apply it to another
- Run AI (e.g. ChatGPT) on the selected text - e.g. to fix spelling or transform it.
- Show the number of characters in the selected text
- Download a YouTube video when you select a YouTube link
- Transform the selected text using JavaScript
- Run the selected text in Terminal
- Open a selected tracking number in the Parcel app (https://apps.apple.com/de/app/parcel-delivery-tracking/id639968404?l=en-GB&mt=12)
- Create a new note from the selected text
- Extract any kind of data and use it (e.g. phone numbers)
Useful BetterTouchTool Actions To Be Used
Of course you can use any of BetterTouchTool's actions or scripting capabilities. I just want to point out some actions that are especially useful with the text selection menu. Most of them are also demoed in the example preset below.
- Transform & Replace Selection With Java Script
- Transform & Replace Selection With ChatGPT (Or compatible AI API)
- Open URL / Open URL With Selection
- Change Formatting Of Selected Text
- Copy Text Style / Formatting
- Paste Text Style / Formatting
- Execute Shell Script / Task
- Run Real JavaScript
- Trigger Menu Bar Menu-Item
- (of course) sending keyboard shortcuts
Variables
Before the "Text Selection Did Change" trigger fires, BetterTouchTool sets the following variables, which you can use in any script and in many BTT actions:
-
BTTTextSelection: The selected text
-
BTTTextSelectionRTF: The selected text as rich text in RTF format.
-
BTTTextSelectionElementRole: The role of the element in which text was selected (e.g. AXTextArea). You could use this to show different floating menus depending on the element.
-
BTTTextSelectionElementSubRole: The sub-role of the element in which text was selected. You could use this to show different floating menus depending on the element.
-
BTTTextSelectionType: The type of event that triggered the text selection. Currently one of:
- double-click
- drag-left-up
- drag-left-down
- drag-right-up
- drag-right-down
- drag-left / drag-right (horizontal drag without vertical movement)
-
BTTTextSelectionCuttable: Cut is available in the current context
-
BTTTextSelectionEditable: The currently selected text can be modified
-
BTTTextSelectionStylable: "Paste and Match Style" is available in the current context
Example Base Preset (Download & Description)
Here you can download an example preset that shows various possibilities: https://folivora.ai/releases/text-selection-base.bttpresetzip
This is what it looks like when you select text:

These triggers are added to BTT when you import the preset:

Quick video walkthrough:
Let's go through the added items:
Floating Menu: text-selection
This is the menu that gets shown. It contains various items that work with the selected text in some way. Let's look at all the items included in the menu. You can also download the items separately here: Individual Items
This is a standard floating menu, so it's fully customizable. See: Floating Menus
Keyboard Shortcuts and Key Sequences:
Double Tap Right Option Key
This runs BTT's text selection detection manually. That's useful if you selected text with the keyboard, because BTT doesn't catch that automatically.
cmd+L
This shortcut is often used to select the address bar in browsers or other apps. It can be helpful to have the text selection menu pop up in this situation, so BTT overrides the shortcut and automatically triggers the text selection detection.
ctrl+opt+cmd+x
Sometimes it's helpful to show the menu even if no text is selected. This shortcut puts the current clipboard content into the BTTTextSelection variable (which the menu operates on) and then shows the menu. So when you press this shortcut, the menu works on the current clipboard contents.
Automations & Named & Other Triggers: Text Selection Did Change
This is the main trigger. It positions and shows the menu, and it fires whenever you select some text.
Included Items Overview
Item: Copy
This is a very simple item. It just executes the cmd+c shortcut to copy the selected text.
Item: Copy as HTML:
This item only shows while you hold the CMD key. It tries to copy the selected text as HTML. That isn't possible everywhere, but most browsers allow it.
Item: Paste
This is a very simple item. It just executes the cmd+v shortcut to paste the current clipboard content.
Item: Cut
This is a very simple item. It just executes the cmd+x shortcut to cut the selected text.
Item: Append
This item appends the currently selected text to the current clipboard contents. It uses the predefined action "Transform Clipboard Contents with Java Script" with this simple script:
async (clipboardContentString) => {
let selectedText = await get_string_variable("BTTTextSelection");
// the \n will add a new line, if you don't need that just remove it.
return clipboardContentString+"\n"+selectedText;
}
Item: Search With Google
This item searches Google for the currently selected text. It's easy to set up with the predefined action "Open URL / Open URL With Selection":

You can easily adapt this to other URLs. For example, to search Yahoo, the query would look like this:
https://search.yahoo.com/search?q={BTTTextSelection}
Item: Run in Terminal
This runs the currently selected text in Terminal, using the following AppleScript:
tell application "BetterTouchTool"
-- you can access the current text selection in AppleScript like this:
set theSelection to get_string_variable "BTTTextSelection"
end tell
tell application "Terminal"
activate
if (count of windows) < 1 then
do script "" -- opens a new Terminal window if none exists
end if
set theTab to selected tab of the first window
do script theSelection in theTab
end tell
Item: AI (ChatGPT)

This submenu contains a few items that let you transform the currently selected text with ChatGPT.

BTT comes with a limited free quota for the 4o-mini model. To use other models, you'll need to enter your own API key.
-
The first submenu item uses the predefined action "Transform & Replace Selection With ChatGPT (Or compatible AI API)". That action has various options. For example, you can provide your own API key, or a different URL if you're running a local model.
-
The second submenu item is something I've been using a LOT recently. It asks ChatGPT to generate JavaScript that transforms the selected text the way you want. It then transforms the selected text with that JS and saves the JS for later use.
-
This one reuses the last generated JavaScript to transform the selected text. It doesn't need an internet connection.
Item: Strike Through
This tries to strike through the currently selected text. It only works if the current app supports rich text.
Item: Comment Lines
This item adds two slashes at the beginning of every selected line. It's a good example of how to transform selected text. It uses the predefined action "Transform & Replace Selection With Java Script":

With this JavaScript:
async (clipboardContentString) => {
// Split the original string by new lines
const lines = clipboardContentString.split('\n');
// Prepend "// " to each line
const commentedLines = lines.map(line => `// ${line}`);
// Join the lines back together and return
return commentedLines.join('\n');
}
If you need a specific text transformation, ChatGPT can often help you generate the necessary JavaScript. Just give it this base function:
// the currently selected text is passed to this function in "clipboardContentString". You can modify that however you like. The currently selected text is replaced by whatever you return from this function. For example this would make the selected text upper case.
async (clipboardContentString) => {
return clipboardContentString.toUpperCase();
}
Item: Character Count
This item isn't super useful. It just shows the number of currently selected characters. But it's a good example of how to use the selected text to dynamically change how a floating menu item looks.
It uses this simple script as the menu item's script:
async function itemScript(itemUUID) {
// first get the selected text
let selectedText = await get_string_variable("BTTTextSelection");
// then return an update definition for the floating menu item. You can update any property a floating menu item has.
let content = {
BTTMenuItemBackgroundColor : "50,80, 190, 166.991",
BTTMenuItemText: selectedText.length + ""
};
// make sure to return a stringified version of that
return JSON.stringify(content);
}

Item: Open In Text Edit
This item opens the selected text in a new file in TextEdit. It uses the predefined action "Execute Shell Script / Task" to run this Terminal command:
FILE="/tmp/btt-selected-text.txt" && echo "{BTTTextSelection}" >| "$FILE" && open -a TextEdit "$FILE"
Item: Search In Apple Maps:
This searches for the selected text in Apple Maps, using the "Open URL / Open URL With Selection" action:

This is the Apple Maps search URL:
maps://maps.apple.com/?q={BTTTextSelection}
To open it in Apple Maps, use the "Use Browser" option with "Custom Open Command" selected. This is the custom open command:
/usr/bin/open -b com.apple.Maps '{url}'
Item: Open Selected URL

This item tries to extract the first link from the currently selected text and open it in your default browser. It only shows up if the selected text actually contains a link. It checks both the plain text version of the selection (saved in the variable BTTTextSelection) and the rich text version (saved in BTTTextSelectionRTF).
For this, the item needs two scripts. The first one is assigned to the floating menu item itself and hides the item if it can't find a link.
Item Visibility Script
function extractFirstLink(text) {
const urlRegex = /[a-zA-Z][a-zA-Z0-9+\-.]*:\/\/[^\s"'`<>]+/;
const match = text.match(urlRegex);
if (!match) {
return null;
}
return match[0];
}
async function itemScript(itemUUID) {
// by default update the item to be invisible
let content = {
BTTMenuItemVisibleWhileActive: 0,
BTTMenuItemVisibleWhileInactive: 0,
BTTMenuItemMinWidth: 1
};
// first try to get the selected text in plain text
let selectedText = await get_string_variable("BTTTextSelection");
let link = extractFirstLink(selectedText);
if(!link) {
// if no link could be extracted, try to get the rich text RTF format instead. This might contain rich text or HTML links.
selectedText = await get_string_variable("BTTTextSelectionRTF");
link = extractFirstLink(selectedText);
}
if (link !== null) {
// make the item visible if a link was found
content = {
BTTMenuItemVisibleWhileActive: 1,
BTTMenuItemVisibleWhileInactive: 1,
BTTMenuItemMinWidth: 1
};
}
return JSON.stringify(content);
}
Action Script:
To actually open the found URL, we use the predefined action "Run Real JavaScript" with this script:
function extractFirstLink(text) {
const urlRegex = /[a-zA-Z][a-zA-Z0-9+\-.]*:\/\/[^\s"'`<>]+/;
const match = text.match(urlRegex);
if (!match) {
return null;
}
return match[0];
}
async function openLink() {
let selectedText = await get_string_variable("BTTTextSelection");
let link = extractFirstLink(selectedText);
if(!link) {
selectedText = await get_string_variable("BTTTextSelectionRTF");
link = extractFirstLink(selectedText);
}
// use BTT's standard scripting interface to trigger the open url action:
let result = await trigger_action({json: JSON.stringify({
BTTPredefinedActionType: 59,
BTTPredefinedActionName: 'Open URL or Open URL With Selection',
BTTOpenURL: link,
BTTOpenURLBrowser: 'Default',
}), wait_for_reply: true});
// for debugging return the selected text
return selectedText
}
Item: Download YouTube Video
This item only shows up if a YouTube link is selected. Clicking it downloads the YouTube video to your Downloads folder.
Note: This requires yt-dlp and ffmpeg to be installed on your system via brew.
brew install yt-dlp
brew install ffmpeg
Like the previous link widget, this widget needs two scripts: one that hides the item if no YouTube link is selected (or shows it otherwise), and one that downloads the YouTube video when clicked. Since they're quite similar to the previous widget, I'll only show the action script here:
function extractFirstYoutubeLink(text) {
if(!text) {return null;}
const youtubeRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)(?:\/[^\s"'`<>]*)?/i;
const match = text.match(youtubeRegex);
if (!match) {
return null;
}
let found = match[0];
if (!/^https?:\/\//i.test(found)) {
found = 'https://' + found;
}
return found;
}
async function downloadYoutubeVideo() {
// first try to grab link from plain text
let selectedText = await get_string_variable("BTTTextSelection");;
let link = extractFirstYoutubeLink(selectedText);
if(!link) {
// if no link was found in plain text try to extract from RTF / rich text
selectedText = await get_string_variable("BTTTextSelectionRTF");
link = extractFirstYoutubeLink(selectedText);
}
// run yt-dlp command line utility to download the video from the found link
let shellScript = `/opt/homebrew/bin/yt-dlp \
--output "~/Downloads/%(title)s.%(ext)s" \
-f "bestvideo+bestaudio/best" \
--no-part \
--prefer-ffmpeg \
--ffmpeg-location "/opt/homebrew/bin/ffmpeg" \
--remux-video mp4 \
"${link}"`;
let shellScriptWrapper = {
script: shellScript
};
let result = await runShellScript(shellScriptWrapper);
return shellScript;
}
Submenu: Format

This submenu contains various example formatting actions.
Submenu: Markdown


This submenu contains various Markdown example actions.
Extending / Modifying The Base Preset
If you want to use the preset above as a base for your own menu, be aware that by default any changes you make are lost when you import a newer version of the same preset. (Only changes to the enabled state, order and display order are preserved.)
To work around this, I recommend creating a separate preset in which you duplicate the menu from the example preset. Then disable the menu in the example preset itself:
You can even merge multiple menus if they have the same identifier - in that case you'll need to use the "display order" property to specify the order of items across menus.
Customizing The Popup Positioning
BetterTouchTool tries to position the menu above or below the mouse cursor depending on how you selected the text. You can easily modify this behavior.
Open the "Automations & Named & Other Triggers" section. There you'll find the "Text Selection Did Change" trigger. It's responsible for recognizing that text has been selected and for showing the menu.
When you select that trigger, you'll see that it contains multiple if conditions. They check the "BTTTextSelectionType" variable, which can have these values:
- double-click
- drag-left-up
- drag-left-down
- drag-right-up
- drag-right-down
- drag-left / drag-right (horizontal drag without vertical movement)
Depending on BTTTextSelectionType, the predefined action "Update Floating Menu Properties" is called. It updates the menu's positioning properties. Then, in the last step of the action sequence, the menu is finally shown.

Disable Text Selection Menu For Specific Apps / Windows
This trigger can interfere with some apps, so you might want to disable it in specific situations. Please also report any app where it causes issues on https://community.folivora.ai
Disabling the menu for specific apps or windows is easy.
The example preset puts everything related to the text selection trigger in a Conditional Activation Group. To exclude apps, edit its conditions:

The default conditions already exclude one app: "Blender". This is done with the condition bundleIdentifier is not "org.blenderfoundation.blender".
You can also exclude apps by name or window name (or any other available condition). Hold Option to create nested rules.
For example, this would exclude Blender (default), Finder and any window that contains "Untitled" in its title.

Showing Specific Items Only In Specific Apps
Sometimes it's useful to show certain items only in specific apps, for example coding assistance items only in code editors, or browser-specific items only in browsers.
To do this, create additional Conditional Activation Groups. If you add a floating menu with the same identifier (in the preset it's called text-selection), its items are merged into your main preset.
You might need to use the "Display Order" property to define the position of these merged items.
The default preset already contains one example of a conditional activation group that adds an item to your text-selection menu.

It's active if any of the conditions shown here are met (TextEdit, Sublime Text, Code and Xcode):

The example contains the menu with the correct identifier and one example item (which is disabled, though):
