Action

Grocery Lister

Posted by 0xnuminous, Last update about 2 months ago

UPDATES

about 2 months ago

Added instructions and warning

A simple grocery list generator using Google Gemini 1.5 Flash. Highlight a meal name, run the action, and it will generate a shopping list for that meal.

WARNING: It does replace current draft

Steps

  • script

    // Function to get the meal input from the user
    function getMealInput() {
        let p = new Prompt();
        p.title = "Grocery List Generator";
        p.message = "What meal are you preparing? Please enter the meal name:";
        p.addTextField("meal", "Meal", "");
        p.addButton("Generate List");
    
        if (p.show()) {
            return p.fieldValues["meal"];
        } else {
            context.cancel(); // Stop the script if the prompt is canceled
            return null;
        }
    }
    
    // Extract dish name from user input
    let input = draft.content;
    let match = input.match(/Grocery list for (.+)/);
    let dish;
    
    if (match) {
        dish = match[1];
    } else {
        // If no valid input is found, prompt the user
        dish = getMealInput();
    }
    
    if (dish) {
        // Create GoogleAI object (no API key needed here)
        let ai = new GoogleAI(); // Drafts will prompt for the API key and store it in its credentials system
    
        // Build the prompt to send to Gemini AI
        let chatPrompt = `Generate a markdown-formatted grocery list for ${dish}. The title should be "## ${dish}'s Shopping List" and the list should include only the ingredients, each formatted as a checklist item with "[ ]". Properly capitalize the dish name in the title and the list items. Respond only with the formatted list, including the title.`;
    
        // Use quickPrompt to get the AI's response
        let markdownList = ai.quickPrompt(chatPrompt, "models/gemini-1.5-flash");
    
        if (markdownList) {
            // Directly use the AI's response in the draft
            draft.content = markdownList;
            
            // Tag the draft with the meal name, "AI Shopping List," and "AI Grocery"
            draft.addTag(dish);
            draft.addTag("AI Shopping List");
            draft.addTag("AI Grocery");
    
            // Save the changes to the draft
            draft.update();
        } else {
            alert("Failed to generate the grocery list from Gemini AI.");
        }
    }
    

Options

  • After Success Nothing
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.