Scripting BetterTouchTool using Apple Script

BetterTouchTool has a small but powerful Apple Script interface which will be described here.

The most current documentation for all supported Apple Script calls can directly be accessed via Apple's Script Editor app on macOS. Go to File => Open Dictionary => BetterTouchTool to access it there.

The available scripting interfaces are:

  • trigger_named
  • trigger_named_async_without_response
  • cancel_delayed_named_trigger_execution
  • update_touch_bar_widget
  • update_stream_deck_widget
  • trigger_action
  • execute_assigned_actions_for_trigger
  • refresh_widget
  • get_trigger
  • get_triggers
  • update_trigger (to create or if exists update a trigger)
  • add_new_trigger
  • delete_trigger
  • delete_triggers
  • get_dock_badge_for
  • get_active_touch_bar_group
  • is_true_tone_enabled
  • get_location
  • set_persistent_string_variable
  • set_string_variable
  • set_persistent_number_variable
  • set_number_variable
  • get_number_variable
  • get_string_variable
  • export_preset
  • paste_text
  • set_clipboard_content

Note: There are some built-in variables, which can also be quite helpful:

Built-In Scripting Variables

Security

In general every application that is allowed to execute Apple Script could trigger these scripting functions. If you do not want this, you can define a shared secret which then must be included in all calls as "shared_secret" parameter.

The shared secret can be defined in the advanced preferences shared_secret

Available Scripting Interfaces

trigger_named

This method will trigger the specified named trigger (which can be configured in the "Other" tab in BetterTouchTool.)

Standard Apple Script Example:

tell application "BetterTouchTool"

trigger_named "TriggerName" 

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.trigger_named("TriggerName");

trigger_named_async_without_response

This method will trigger the specified named trigger (which can be configured in the "Other" tab in BetterTouchTool.). In contrast to the trigger_named it does not wait for a result - this should always be used if you do not need a response. cancel_delayed_named_trigger_execution Optional parameter: delay This lets you set a delay before the named trigger is executed (in seconds). While the trigger has not been executed you can cancel the execution by calling the cancel_delayed_named_trigger_execution function

Standard Apple Script Example:

tell application "BetterTouchTool"

trigger_named_async_without_response "TriggerName" delay 0.2

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.trigger_named_async_without_response("TriggerName", {delay: 0.2});

---

cancel_delayed_named_trigger_execution

This method will cancel the execution of a delayed named trigger (see previous function)

Standard Apple Script Example:

tell application "BetterTouchTool"

trigger_named_async_without_response "TriggerName" delay 0.2

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.trigger_named_async_without_response("TriggerName", {delay: 0.2});

update_touch_bar_widget

This method will update the contents of a Touch Bar Script Widget (identified by its uuid). You can provide a new text to show, a new icon and a new background color.

For the icon you can either provide it directly using the icon_data parameter (must be base64 encoded) or you can provide a file path (via the icon_path parameter) that points to the new icon.

You can get the uuid by right-clicking any script widget in BTT.

Standard Apple Script Example:

tell application "BetterTouchTool"

update_touch_bar_widget "9990CE09-9820-4D67-9C52-8BABAB263056" text "newButtonText" icon_path "~/Desktop/005-ShoppingBasket@2x.png" background_color "255,100,100,255"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.update_touch_bar_widget("9990CE09-9820-4D67-9C52-8BABAB263056", 
{
    text: "hi there!",
    icon_path: "/Users/andi/Desktop/test.png",
    background_color: "200,100,100,255"    
});

update_stream_deck_widget

This method will update the contents of a Stream Deck Widget (identified by its uuid). You can provide a new text to show, and update any of it's configuration properties by providing an (escaped) JSON string.

You can get the uuid by right-clicking any widget in BTT. To get a list of the configuration properties copy an existing Stream Deck trigger to a text editor and have a look at the BTTTriggerConfig part in the JSON.

Standard Apple Script Example:

tell application "BetterTouchTool"

update_stream_deck_widget "9990CE09-9820-4D67-9C52-8BABAB263056" text "newButtonText" json "{\"BTTStreamDeckBackgroundColor\": \"255,85,100,255\"}"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');
var updateDefinition = {
    "text": "newButtonText"
    "BTTStreamDeckBackgroundColor" : "255,85,100,255"
    }
BetterTouchTool.update_stream_deck_widget("2F34005D-4537-464D-94E9-A7F42DA39DF1", {json: JSON.stringify(updateDefinition)});

trigger_action

This method will trigger any of BetterTouchTool's predefined actions (or any combination of them).

You need to provide a JSON description of the action you want to trigger. The easiest way to get such a JSON description is to right-click the trigger you have configured in BetterTouchTool and choose "Copy JSON". This will copy the complete JSON (including the configuration for the trigger itself), but this action will ignore anything that's not needed. (or you can delete the not needed parts)

Standard Apple Script Example:

tell application "BetterTouchTool"

    trigger_action "{\"BTTPredefinedActionType\":100}"

end tell

Java Script for Automation Example:

Hint: don't forget to use JSON.stringify() before passing the actionDefinition object.

var BetterTouchTool = Application('BetterTouchTool');
var actionDefinition = {
  "BTTPredefinedActionType" : 153, 
  "BTTPredefinedActionName" : "Move Mouse To Position", 
  "BTTMoveMouseToPosition" : "{10, 10}", 
  "BTTMoveMouseRelative" : "6"
};


BetterTouchTool.trigger_action(JSON.stringify(actionDefinition));

execute_assigned_actions_for_trigger

This method execute all the assigned actions for a given trigger (i.e. gesture, shortcut, drawing etc.) identified by its uuid.

You can get the uuid by right-clicking any configured trigger in BetterTouchTool.

Standard Apple Script Example:

tell application "BetterTouchTool"
    execute_assigned_actions_for_trigger "2F34005D-4537-464D-94E9-A7F42DA39DF1"
end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.execute_assigned_actions_for_trigger("2F34005D-4537-464D-94E9-A7F42DA39DF1");

refresh_widget

This method will execute all scripts assigned to a script widget and update its contents accordingly.

The widget is identified by its uuid, which you can get by right-clicking the widget in BetterTouchTool.

Standard Apple Script Example:

tell application "BetterTouchTool"
    refresh_widget "2F34005D-4537-464D-94E9-A7F42DA39DF1"
end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.refresh_widget("2F34005D-4537-464D-94E9-A7F42DA39DF1");

get_trigger

This allows you to retrieve the JSON description of any trigger in BTT (e.g. Touch Bar item, Gesture, Keyboard Shortcut etc.) based on its UUID.

You can get the UUID by right-clicking any trigger in BTT.

Standard Apple Script Example:

tell application "BetterTouchTool"

    return get_trigger "9D189F2C-6866-4955-9D8E-FEC96C5C7E30"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.get_trigger("9D189F2C-6866-4955-9D8E-FEC96C5C7E30");


get_triggers

This allows you to retrieve the JSON description of multiple triggers in BTT (e.g. Touch Bar item, Gesture, Keyboard Shortcut etc.) based on a few properties.

You can get the properties of a specific trigger by selecting it in BTT and copy pasting it to some text editor.

The get_triggers() function supports these parameters (all optional):

  • trigger_type (e.g. BTTTriggerTypeMagicMouse)
  • trigger_id (e.g. 643 for named triggers)
  • trigger_parent_uuid (if you want to get the items of a folder)
  • trigger_uuid (to get a specific trigger)
  • trigger_app_bundle_identifier (to get triggers assigned to a specific app)

Java Script for Automation Example:

This will load the names of all "named triggers" (trigger id 643):

let BetterTouchTool = Application('BetterTouchTool');
let allNamedTriggersJSONString = BetterTouchTool.get_triggers({trigger_id: 643});
let allTriggersJSONArray = JSON.parse(allNamedTriggersJSONString);

let allNamedTriggerNames = [];
for(let trigger of allTriggersJSONArray) {
    allNamedTriggerNames.push(trigger["BTTTriggerName"]);
}

allNamedTriggerNames


update_trigger

This method will create or update the configuration of any specified trigger (i.e. gestures, shortcuts, touchbar items etc.).

You need to provide the uuid of the trigger you want to update (get by right-clicking it in BTT) and a JSON object defining the updates. To know how the JSON object should look like, right-click the trigger in BTT and choose "Copy JSON".

Optional Parameter: trigger_parent_uuid (if you want to add the trigger to a group)

Standard Apple Script Example:

tell application "BetterTouchTool"

update_trigger "2F34005D-4537-464D-94E9-A7F42DA39DF1" json "{\"BTTTouchBarButtonName\" : \"New Name\",  \"BTTTouchBarItemIconHeight\" : 25}"

end tell

Java Script for Automation Example:

Hint: don't forget to use JSON.stringify() before passing the actionDefinition object.

var BetterTouchTool = Application('BetterTouchTool');
var updateDefinition = {
    "BTTTouchBarButtonName" : "New Name",
    "BTTTouchBarItemIconHeight" : 25
}
BetterTouchTool.update_trigger("2F34005D-4537-464D-94E9-A7F42DA39DF1", {json: JSON.stringify(updateDefinition)});

add_new_trigger

This method will add a new trigger to BTT (i.e. gestures, shortcuts, touchbar items etc.).

You need to provide a JSON object defining the new trigger. To know how the JSON object should look like, right-click any existing trigger in BTT and choose "Copy JSON".

Optional parameter: parent_uuid

Standard Apple Script Example:

tell application "BetterTouchTool"

add_new_trigger "{ \"BTTTriggerClass\" : \"BTTTriggerTypeKeyboardShortcut\", \"BTTPredefinedActionType\" : 5, \"BTTPredefinedActionName\" : \"Mission Control\", \"BTTAdditionalConfiguration\" : \"1179658\", \"BTTTriggerOnDown\" : 1, \"BTTEnabled\" : 1, \"BTTShortcutKeyCode\" : 2, \"BTTShortcutModifierKeys\" : 1179648, \"BTTOrder\" : 3 }" 

end tell

Java Script for Automation Example:

Hint: don't forget to use JSON.stringify() before passing the actionDefinition object.


// this will add a new keyboard shortcut to BTT.
var BetterTouchTool = Application('BetterTouchTool');

var newTriggerDefinition = {
  "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
  "BTTPredefinedActionType" : 5,
  "BTTPredefinedActionName" : "Mission Control",
  "BTTAdditionalConfiguration" : "1179648",
  "BTTTriggerOnDown" : 1,
  "BTTEnabled" : 1,
  "BTTShortcutKeyCode" : 2,
  "BTTShortcutModifierKeys" : 1179648,
  "BTTOrder" : 3
}

BetterTouchTool.add_new_trigger(JSON.stringify(newTriggerDefinition))

delete_trigger

This method will delete a trigger from BetterTouchTool. You need to provide the uuid of the trigger you want to delete (get by right-clicking it in BTT).

Standard Apple Script Example:

tell application "BetterTouchTool"

delete_trigger "2F34005D-4537-464D-94E9-A7F42DA39DF1"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.delete_trigger("2F34005D-4537-464D-94E9-A7F42DA39DF1");

---

delete_triggers

This allows you to delete multiple triggers in BTT based on a few properties. If you want to "test" your query before deleting, use the get_triggers function first - it uses the exact same parameters.

You can get the properties of a specific trigger by selecting it in BTT and copy pasting it to some text editor.

The delete_triggers() function supports these parameters (all optional): • trigger_type (e.g. BTTTriggerTypeMagicMouse) • trigger_id (e.g. 643 for named triggers) • trigger_parent_uuid (if you want to get the items of a folder) • trigger_uuid (to get a specific trigger) • trigger_app_bundle_identifier (to get triggers assigned to a specific app)

Java Script for Automation Example:

This will delete all "named triggers" (trigger id 643):

let BetterTouchTool = Application('BetterTouchTool');
BetterTouchTool.delete_triggers({trigger_id: 643});


set_persistent_string_variable

This allows you to set a variable to a given string that persists over BTT relaunches.

Standard Apple Script Example:

tell application "BetterTouchTool"

    set_persistent_string_variable "variableName" to "this is a test value"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.set_persistent_string_variable("variableName", {to: "this is a test value"});

set_string_variable

This allows you to set a variable to a given string that persists over BTT relaunches.

Standard Apple Script Example:

tell application "BetterTouchTool"

    set_string_variable "variableName" to "this is a test value"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.set_string_variable("variableName", {to: "this is a test value"});

set_persistent_number_variable

This allows you to set a variable to a given string that persists over BTT relaunches.

Standard Apple Script Example:

tell application "BetterTouchTool"

    set_persistent_number_variable "variableName" to 12345

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.set_persistent_number_variable("variableName", {to: 1345});

set_number_variable

This allows you to set a variable to a given string that persists over BTT relaunches.

Standard Apple Script Example:

tell application "BetterTouchTool"

    set_number_variable "variableName" to 12345

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.set_number_variable("variableName", {to: 1345});

get_number_variable

This allows you to retrieve the contents of a number variable with the given name

Standard Apple Script Example:

tell application "BetterTouchTool"

    return get_number_variable "variableName"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.get_number_variable("variableName");

get_string_variable

This allows you to retrieve the contents of a string variable with the given name

Standard Apple Script Example:

tell application "BetterTouchTool"

    return get_string_variable "variableName"

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.get_string_variable("variableName");

get_dock_badge_for

This method will efficiently get the current Dock badge for a specific application. Running this function multiple times will return the previously cached value until the specified update_interval has been reached.

When an update interval is provided, BTT will internally refresh the badges for all apps every x seconds. In this case consecutive "get_dock_badge_for" function calls are basically free, as they will just return the last cached value. This is the recommended way to do it, and the update_interval should not be too small.

If you need to refresh the cache immediately for some reason, you can leave the update_interval function out. Then BTT will immediately refresh all the dock badges and return the latest value.

Standard Apple Script Example:

tell application "BetterTouchTool"

    get_dock_badge_for "Calendar" update_interval 5

end tell

Java Script for Automation Example:

var BetterTouchTool = Application('BetterTouchTool');

BetterTouchTool.get_dock_badge_for("Calendar", {'update_interval': 5});

export_preset

Exports a preset by name.

includeSettings will in addition to the configured triggers add general preferences to the preset export.

link, comment and compression is optional.

Standard Apple Script Example:

tell application "BetterTouchTool"

    export_preset "some-example-preset" outputPath "~/Documents/preset.bttpreset" comment "someComment" link "https://someLinkToShowWhenImporting" with includeSettings and compress

end tell

paste_text

This pastes some text

Parameters:

text: The text to paste. insert_by_pasting: If set to true BTT will use cmd+v to paste the text. Otherwise it will actually try to type it (only possible for standard formats) move_cursor_left_by_x_after_pasting: If specified BTT will move the cursor by the given amount to the left. format: Optional. The format the text will be interpreted as when pasting (e.g. if you want to paste a table, you can use set the string to

...
and specify the format NSPasteboardTypeHTML)

Default formats are (more are possible): NSPasteboardTypeString NSPasteboardTypeTIFF NSPasteboardTypePNG NSPasteboardTypeRTF NSPasteboardTypeRTFD NSPasteboardTypeHTML NSPasteboardTypeTabularText NSPasteboardTypeFont NSPasteboardTypeRuler NSPasteboardTypeColor NSPasteboardTypeSound NSPasteboardTypeMultipleTextSelection NSPasteboardTypeTextFinderOptions NSPasteboardTypeURL NSPasteboardTypeFileURL

Standard Apple Script Example:

tell application "BetterTouchTool"

    paste_text "<strong>Hello world</strong>" format "NSPasteboardTypeHTML" insert_by_pasting true

end tell

set_clipboard_content

This changes the clipboard contentt

Parameters:

content: The text to paste. format: Optional. The format the text will be interpreted as when pasting (e.g. if you want to paste a table, you can use set the string to

...
and specify the format NSPasteboardTypeHTML)

Default formats are (more are possible):

  • NSPasteboardTypeString
  • NSPasteboardTypeTIFF
  • NSPasteboardTypePNG
  • NSPasteboardTypeRTF
  • NSPasteboardTypeRTFD
  • NSPasteboardTypeHTML
  • NSPasteboardTypeTabularText
  • NSPasteboardTypeFont
  • NSPasteboardTypeRuler
  • NSPasteboardTypeColor
  • NSPasteboardTypeSound
  • NSPasteboardTypeMultipleTextSelection
  • NSPasteboardTypeTextFinderOptions
  • NSPasteboardTypeURL
  • NSPasteboardTypeFileURL

Standard Apple Script Example:

tell application "BetterTouchTool"

    set_clipboard_content "<strong>Hello world</strong>" format "NSPasteboardTypeHTML"

end tell

Floating Menus

See Scripting FLoating Menus for details.

update_menu_item

Apple Script Example With Item UUID:

tell application "BetterTouchTool"
    update_menu_item "044E5D2B-5661-4A1F-AB39-8D1EA2C1DBA1" json "{BTTMenuItemBackgroundColor: '20, 200, 20, 255', BTTMenuItemText: 'test' }"
end tell

Apple Script Example With Menu Name And Item Name:

tell application "BetterTouchTool"
    update_menu_item menu_name "TheMenuName" item_name "TheItemName" json "{BTTMenuItemBackgroundColor: '20, 200, 20, 255', BTTMenuItemText: 'test' }"
end tell

get_menu_item_value

Gets the current value of a floating menu item. For textfields the value is the entered text. For sliders it's the slider's value. For standard items, it's any files that have been dropped onto the item.

By menu name and item name:

tell application "BetterTouchTool" to get_menu_item_value menu_name "TheMenuName" item_name "TheItemName"

By item UUID:

tell application "BetterTouchTool" to get_menu_item_value "9DDA72BA-4968-4AC5-907E-FDE743B2F6E8"

set_menu_item_value

Sets the current value of a floating menu item.

By menu name and item name:

tell application "BetterTouchTool" to get_menu_item_value menu_name "TheMenuName" item_name "TheItemName" value "testvalue"

By item UUID:

tell application "BetterTouchTool" to get_menu_item_value "9DDA72BA-4968-4AC5-907E-FDE743B2F6E8" value 0.5

webview_menu_item_load_html_url_js

Loads the provided html or url in the specified webview item in a floating menu.

tell application "BetterTouchTool"
    # instead of the uuid you can also use the item_name and menu_name parameters.
    webview_menu_item_load_html_url_js "9E2989CF-77C7-4F14-A646-49342D2F8331" html_or_url "https://apple.com"
end tell
tell application "BetterTouchTool"
    # instead of the uuid you can also use the item_name and menu_name parameters.
    webview_menu_item_load_html_url_js "9E2989CF-77C7-4F14-A646-49342D2F8331" javascript_to_execute "alert('hello world')"
end tell

results matching ""

    No results matching ""