Background
ChatGPT's answer
In Apple Shortcuts, you can create a workflow that performs an action if the clipboard matches a certain regex (regular expression) pattern by using a combination of the following actions:
- Get Clipboard: To retrieve the current content of the clipboard.
- Match Text: To match the clipboard content against a regular expression.
- If: To define the condition for what should happen if the regex matches.
Here’s a simple summation of how you can build the workflow step-by-step:
- Get Clipboard
- Match Text (with your regex)
- If (Match Text has any value)
- Then (Action if regex matches)
- Otherwise (Optional action if no match)
This will allow you to build a shortcut that conditionally runs actions based on whether the clipboard content matches your regular expression!
I gave this a try, and it worked! The important point was using the "IF" statement on a previous MATCH TEXT action, where if a match is found it will have a value, and if no match is found it will have no value. So you can set it accordingly to "If Matches has any value".
See how I arranged the shortcut as it appears in the image that goes with this post.
Shortcut overview in text
Or written out in text form it would look like:
GET CLIPBOARD
MATCH [{desired regex}] in [Clipboard]
IF [Matches] [has any value]
SHOW ALERT [{Your match is found, so stopping this shortcut}]
OTHERWISE
{...}
Final thought
I'm really starting to get a lot of use out of custom Shortcuts to do a variety of tasks quickly with minimal keystrokes. So glad Apple replaced the old Automator with this new format. They are great for running quick regex replacements that you need frequently. Lots of other stuff too. I also use a Shortcut to connect to my Google Home speaker, set the volume, launch my Podcasts, and automatically disconnect from the speaker after X minutes.
Happy shortcutting.
Comments