Skip to main content
Glama

click_menu

Click menu bar items in macOS applications using a path format like "File > Save As..." to automate desktop interactions.

Instructions

Click a menu bar item in an application. Specify the menu path as "Menu > Submenu > Item" (e.g., "File > Save As...", "View > Sort By > Name").

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
appYesApplication name
pathYesMenu path, e.g. "File > Save As..."

Implementation Reference

  • The handleClickMenu function processes the input, builds an AppleScript command to click the menu item, and executes it.
    async function handleClickMenu(
      args: Record<string, unknown>,
    ): Promise<CallToolResult> {
      const parsed = ClickMenuInputSchema.parse(args);
      const parts = parsed.path.split(PATH_DELIMITER);
    
      if (parts.length < MIN_PATH_SEGMENTS) {
        return {
          content: [
            {
              type: "text" as const,
              text: `Invalid menu path: expected at least ${MIN_PATH_SEGMENTS} segments separated by "${PATH_DELIMITER}", got ${parts.length}. Example: "File > Save As..."`,
            },
          ],
          isError: true,
        };
      }
    
      const app = await resolveAppName(parsed.app);
      const script = buildMenuClickScript(app, parts);
      await runAppleScript(script);
    
      return {
        content: [
          {
            type: "text" as const,
            text: JSON.stringify({
              success: true,
              app: parsed.app,
              path: parsed.path,
            }),
          },
        ],
      };
    }
  • The ClickMenuInputSchema defines the expected input structure for the click_menu tool.
    const ClickMenuInputSchema = z.object({
      app: z.string().max(1_000).describe("Application name"),
      path: z.string().max(1_000).describe('Menu path, e.g. "File > Save As..."'),
    });
  • The menuToolDefinitions array contains the MCP tool definition for click_menu.
    export const menuToolDefinitions: Tool[] = [
      {
        name: "click_menu",
        description:
          'Click a menu bar item in an application. Specify the menu path as "Menu > Submenu > Item" (e.g., "File > Save As...", "View > Sort By > Name").',
        inputSchema: zodToToolInputSchema(ClickMenuInputSchema),
        annotations: {
          readOnlyHint: false,
          destructiveHint: true,
        },
      },
    ];
  • The buildMenuClickScript helper constructs the AppleScript command used to perform the menu click.
    export function buildMenuClickScript(app: string, parts: string[]): string {
      const safeApp = escapeAppleScriptString(app);
      const escaped = parts.map(escapeAppleScriptString);
    
      const root = escaped[0];
      const leaf = escaped[escaped.length - 1];
    
      // Start with the leaf item
      let chain = `click menu item "${leaf}"`;
    
      // Traverse intermediate submenus from second-to-last down to index 1
      for (let i = escaped.length - 2; i >= 1; i--) {
        chain += ` of menu "${escaped[i]}" of menu item "${escaped[i]}"`;
      }
    
      // Attach to root menu bar item
      chain += ` of menu "${root}" of menu bar item "${root}" of menu bar 1`;
    
      return [
        'tell application "System Events"',
        `  tell process "${safeApp}"`,
        `    ${chain}`,
        "  end tell",
        "end tell",
      ].join("\n");
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/antbotlab/mac-use-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server