I'm working on a Custom Context Menu that retrieves its items from an applescript (returns a list). And then when the item gets selected it would pass it's name to another action (in this case, pasting the selected string).
I'm trying to do this but my java isn't really good (Most of what I do is AppleScript) and I can't seem to put the actions to work, I get nothing when triggered and shows back BTT if the configuration window is open.
Just amazed at this new functionalities of BTT. So many posibilities. And hoping for the Logi support to ditch LogiOptions for good. Big kudos to devs!
you can define an item & action like this, it would paste some text
{
"title": "test",
"action": {
btt: "paste_text",
args: {
text: "THETEXTYOUWANTTOPASTE",
insert_by_pasting: true,
}
}
}
Thanks, looks like the click action was conflicting with BetterMouse. As soon as unchecked "Right click-through" in BM, the action behaves as expected. Thanks!
Could you point to some direction as for how to assign the AppleScript list to items and pass the selected value to a unique action? (instead of repeating the same action block in every item declaration).
can you post the script you are using?
Sure, this is what I've got so far! I got a "Paste text" action right after "Show Custom Context Menu" to paste the the variable set.
The applescript returns a list that I would like to parse as the items that would populate the menu. The selected menu item would return it's own name/title to the variable to be pasted afterwards.
async function retrieveJSON() {
let items = [
{
"title": "test",
"action":
{
setvariable: {
name: "CustomSBJCT",
value: "some text",
}
},
},
{
"title": "Get Recent Subjects",
"action":
{
js: `(async () => {
let appleScript = \`
tell application "Mail"
tell application "System Events" to tell process "Mail"
set FrontMostWin to window 1
set TargetRecipient to value of text field 1 of text field "To:" of FrontMostWin
end tell
set theMessages to subject of messages of mailbox "Sent" of account "MyAccount" whose ¬
all headers contains TargetRecipient and date sent > ((current date) - days * 3)
return theMessages
end tell
\`;
let appleScriptResult = await runAppleScript(appleScript);
await callBTT('set_string_variable', { variable_name: 'CustomSBJCT', to: appleScriptResult });
returnToBTT(appleScriptResult);
})()
`,
},
},
];
return JSON.stringify(items);
}
something like this?
Your apple script seems to return a string in this format {"messageA", "messageB", ...}
So I'm splitting that up. However the Apple Script takes quite a while to return
async function retrieveJSON() {
let appleScript = `tell application "Mail"
tell application "System Events" to tell process "Mail"
set FrontMostWin to window 1
end tell
set theMessages to subject of messages of mailbox "Sent" of account "folivora.AI GmbH" whose ¬
all headers contains "andreas@folivora.ai" and date sent > ((current date) - days * 3)
return theMessages
end tell`;
let mailItems = await runAppleScript(appleScript);
// Remove curly braces and split by comma, then trim extra quotes and whitespace
const mailItemArray = mailItems
.replace(/^{|}$/g, '') // Remove the outer curly braces
.split(',') // Split by comma
.map(str => str.trim().replace(/^"|"$/g, '')); // Remove surrounding quotes and trim whitespace
let contextMenuItems = [];
for (item of mailItemArray) {
contextMenuItems.push({
title: item,
action: {
btt: "paste_text",
args: {
text: item,
insert_by_pasting: true,
},
},
});
}
return JSON.stringify(contextMenuItems);
}
Awesome, Thank you so much! (Changing the order to search first for date and then "all headers" makes speed tolerable... don't know if there's a faster way to collect subjects from sent mails to a specific recipient)
Can I ask you a different question of a different topic? I've been using a Floating Menu with "Modifier Based Visibility" using modifiers. But I would like it not to activate when those modifiers are triggered in a specific application (Sketchup). I added the app name in "Does NOT match...." of the "Conditional Visibility" section but doesn't seem to catch... Do those two cancel each other? Or am I missing something.