How do I create a shortcut that truncates/shortens a URL copied to the clipboard?

I'd like to create a shortcut (actually for use on my iOS devices but also on my Mac) that simply grabs a URL from the clipboard, erases/truncates everything including and after the "?" symbol (i.e. removing the question mark itself and everything that comes after), and then makes the new URL available for me to copy, or even better, copies the new URL straight to the clipboard.


Unfortunately I cannot figure out how to do this and I am far from an advanced coder so I'm not sure what a lot of the commands do and how to adjust the variables to get Shortcuts to do what I want. Anyone have any ideas? I feel like this could be a simple fix, just need some pointing in the right direction.


Here's what I came up with after trying but it does not seem to be doing anything to the URL when I run it:



Thanks so much!

MacBook Pro 15″

Posted on Jun 1, 2023 11:58 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 1, 2023 01:40 PM

The following is tested on Ventura 13.4 only:



When you split a URL on ?, it creates an interim list and you need to tell the Shortcut that you want just the first item (trimmed URL) from that list.


That Shortcut takes this URL:


https://www.foobar.com/webstuff?lang=en


and puts the following back on the clipboard:


https://www.foobar.com/webstuff



6 replies
Question marked as Top-ranking reply

Jun 1, 2023 01:40 PM in response to Mike Amin

The following is tested on Ventura 13.4 only:



When you split a URL on ?, it creates an interim list and you need to tell the Shortcut that you want just the first item (trimmed URL) from that list.


That Shortcut takes this URL:


https://www.foobar.com/webstuff?lang=en


and puts the following back on the clipboard:


https://www.foobar.com/webstuff



Jun 1, 2023 01:20 PM in response to Mike Amin

From your description, it seems like you're looking to create an Apple Shortcut that truncates or shortens a URL copied to the clipboard. While it's a great idea to streamline this process, I'll guide you through the steps to achieve your desired outcome.


1. Open the Shortcuts app on your iOS device or Mac.

2. Tap the "+" button to create a new shortcut.

3. Search for and add the action called "Get Clipboard" to retrieve the URL from the clipboard.

4. Add the "Find Text" action. In the "Find" field, input a question mark ("?"). Leave the "Replace With" field empty.

5. Next, add the "Set Clipboard" action. Connect the output of the "Find Text" action to the input of the "Set Clipboard" action.

6. Optionally, you can add the "Copy to Clipboard" action after the "Set Clipboard" action if you want the shortened URL to be automatically copied to the clipboard.

7. Tap the play button at the bottom to test your shortcut. It should now truncate the URL from the clipboard and either save it or copy it to the clipboard, depending on the previous step.


You should now be able to run this shortcut whenever you need to quickly remove everything after the question mark in a URL.


For more information and assistance, you can refer to the following resources:

- Apple Support: Shortcuts User Guide

- Apple Support: Shortcuts for Mac User Guide

- MacStories: A Comprehensive Guide to All 120+ Settings URLs Supported by iOS and iPadOS 14.3 << A bit dated, but this article provides additional information on working with URLs in shortcuts.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I create a shortcut that truncates/shortens a URL copied to the clipboard?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.