Appearance
Mastering Conditional & Looping Routines with Brainwrite Swift
Hello everyone! I hope you're always safe and healthy wherever you are. Today, I’ll be talking about a powerful feature in Brainwrite Swift: "Conditional & Looping" routines. If you're familiar with building routines using blocks, you might encounter scenarios that require more complex logic, such as conditions and loops. Let's dive into some practical examples to see how Brainwrite Swift can handle these tasks efficiently.
Example 1: Saving Images from Google Image Search
Let's start with an example of saving images from Google Image search results by looping through multiple image elements and saving them automatically.
Step-by-Step Routine:
Create a New Routine:
- Name it "Google Images".
- Add a New Tab block with the URL
https://images.google.com
. - Connect it with the Trigger block and save.
Search for Images:
- Use the Element Selector to identify the search input form.
- Add a Forms block, paste the selector, and set the value to "baby cat" with a typing delay of 160 ms.
- Add a Press Key block to simulate the Enter keypress for form submission.
Loop Through Image Results:
- Use the Element Selector to capture the list of image elements.
- Add a Loop Data block, set the ID to "images", loop through the elements, and set the max data to loop (e.g., 5 images).
- Add a Click Element block to click on each image element, using the selector
{{ loopData@images }}
{{ loopData@images }}
Save Images:
- Use the Element Selector to identify the image element for saving.
- Add a Save Assets block with the appropriate selector.
- Add a Loop Breakpoint block to close the loop.
Execute the Routine:
- Save and execute the routine to search for
baby cat
and save 5 images automatically.
- Save and execute the routine to search for
Example 2: Nested Loops for Multiple Searches
Now, let's enhance the previous routine to perform multiple searches (e.g., "baby cat", "dog", "hamster") and save images for each search term.
Step-by-Step Routine:
- Add a Loop Data Block for Keywords:
- Set the ID to "keywords" and loop through custom data.
- Insert the keywords:
["baby cat", "dog", "hamster"]
["baby cat", "dog", "hamster"]
in JSON format.
- Update Search Input:
- Update the Forms block value to
{{ loopData@keywords }}
{{ loopData@keywords }}
Close Tabs and Loops:
- Add a Close Tab block after saving images.
- Add a Loop Breakpoint block to close the "keywords" loop.
Execute the Routine:
- Save and execute to search for each keyword and save 5 images per search term.
Example 3: Creating a Simple WhatsApp Bot
Let’s create a simple WhatsApp bot that replies to messages based on specific conditions.
Step-by-Step Routine:
Create a New Routine:
- Name it "Simple Bot".
- Add a New Tab block with the URL
https://web.whatsapp.com
. - Connect it with the Trigger block and save.
Verify Chat List Elements:
- Use the Element Selector to identify chat list elements.
- Add an Element Exists block to ensure the element exists, with a retry mechanism.
Loop Through Chats:
- Add a Loop Data block, set the ID to "chats", and loop through the chat list elements.
- Add a Trigger Event block to simulate clicking on each chat.
Identify Messages:
- Use the Element Selector to differentiate between incoming and outgoing messages.
- Add an Attribute Value block to get the class attribute of the last message and assign it to a variable (e.g., "classes").
- Add Conditions to check if the last message contains "message-in".
Reply to Messages:
- Add a Get Text block to extract the message text.
- Add Conditions to check if the message text equals "hello" or "hi".
- Add a Forms block to type the reply (e.g., "Hello again! 😃").
- Add a Click Element block to send the message.
Set Interval Trigger:
- Change the trigger to an interval (e.g., every 1 minute).
- Update New Tab with Switch Tab block to keep WhatsApp Web active.
Execute the Routine:
- Save and execute to simulate the bot replying "Hello again! 😃" to messages containing "hello" or "hi".
Conclusion
Brainwrite Swift enables you to automate complex routines with ease by utilizing conditional logic and looping mechanisms. Whether you're saving images from Google, performing multiple searches, or creating a WhatsApp bot, Brainwrite Swift's powerful features can significantly enhance your productivity.
If you have any questions or seek further insights into Brainwrite Swift's capabilities, we encourage you to join our Discord server and share your thoughts. We're here to help you unlock the full potential of automation with Brainwrite Swift.
(Note: The images and URLs provided in the screenshots are placeholders and should be replaced with actual screenshots and URLs from the video for an authentic blog post.)