Jayson
-
Posts
2 -
Joined
-
Last visited
Content Type
Blogs
Gallery
Downloads
Events
Profiles
Forums
Articles
Media Demo
Posts posted by Jayson
-
-
I've created a small workflow that copies selected text—an email address—and when triggered by a hotkey, it either ignores the email if valid, or appends, following a tab space "Invalid email address". What I really wanted to do was instead of taking in one line of text as input, and outputting that text plus the appended text, I would have preferred it work on multiple lines, such that if I were to select a column of email addresses, it would validate them all. I couldn't figure out how to make it work on multiple lines. I'm perfectly open to the idea of validating in some other way, but currently I'm using "Selection in macOS" with a hotkey trigger, which runs the python script, the output of which passes as {query} to "Copy to Clipboard" with Automatically paste to frontmost app selected. Any ideas are welcome.
import sys import re def validate_email(email): pattern = re.compile(r'(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\0c\0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\0c\0e-\x7f])+)\])') return pattern.match(email) is not None query = "{query}" emails = query.split('\n') # split query into a list of strings valid_emails = [] invalid_emails = [] for email in emails: if validate_email(email.strip()): # validate each email address valid_emails.append(email.strip()) else: invalid_emails.append(email.strip()) # add invalid email addresses to a separate list # write invalid email addresses to stdout with tab space and "Invalid email address" message for email in invalid_emails: query = sys.stdout.write(email + "\t" + "Invalid email address\n") # write valid email addresses to stdout with newline separator for email in valid_emails: query = sys.stdout.write(email + "\n")
How to make python clipboard workflow work line by line
in Workflow Help & Questions
Posted · Edited by Jayson
Added `re.IGNORECASE` to regex to eliminate emails with uppercase getting invalidated.
Thank you @vitor, that helped me. I went with the following to maintain the order of which I was copying and pasting.