Thanks a lot Nick! This works perfectly. And nice choice for the icon. I like it.
I have been a PopClip user for a long time but I was not aware of the ‘extension snippet’. What a wonderful addition for a wonderful app this is!
I wanted to do it in Python because this is the only language I feel slightly comfortable with. If I have one extension as a basis, I can keep creating new and possibly more complex ones to suit my needs. What I had tried, following the tutorials/documentation I found was the following:
# #popclip
# { name: Clean URL, icon: micona, interpreter: python3 }
# #popclip
# { name: Clean URL, icon: micona, interpreter: python3 }
import clipboard
import re
def clean_url(text):
url_pattern = r"(https?://[^\s]+)\?.*"
match = re.match(url_pattern, text)
if match:
return match.group(1)
return text
def main():
text = clipboard.get()
cleaned_text = clean_url(text)
clipboard.set(cleaned_text)
if __name__ == "__main__":
main()
I see that, besides the specific Python code you used, there are other differences in the
Because I want to understand how this works, after you provided me with the solution, I tried with the following:
# #popclip
# name: TEST URL CLEAN
# icon: iconify:healthicons:clean-hands
# interpreter: python3
# requirements: [url]
# after: copy-result
import re
import pyperclip
url_pattern = r"(https?://[^\s]+)\?.*"
def clean_url(text):
match = re.match(url_pattern, text)
if match:
return match.group(1)
return text
def main():
text = pyperclip.paste()
cleaned_text = clean_url(text)
pyperclip.copy(cleaned_text)
if __name__ == "__main__":
main()
The reason I tried with this is because I am confident that the script works when executed from the terminal. What it does is to process whatever is currently in the clipboard and return the “cleaned” version applying the regular expression.
Now, even though yours worked, I’d like to understand why this other alternative does not work. One important difference (and I think here lies the explanation) is that this script does not assume that you are selecting anything with your cursor. It assumes that you have already copied the string to your clipboard. The other important difference is the part where the script returns the results to PopClip. In the JavaScript version this seems obvious because reference to popclip is part of the return statement. In the Python version, I imagine this is done via the final print statement.
Still, what I’m still curious about is how does PopClip does what is not specified in the script (whether it is Python or JavaScript). I don’t know JavaScript but the script is simple enough to figure out what it is doing. The first thing I’m curious about is how does the script in the extension get the string (the url) it has to process. In neither of the two scripts there is anything that indicates that a copy of the string is made. Is this part of the ‘internals’ of PopClip? I mean, independently of what option you are going to choose, PopClip copies whatever you select to the clipboard?
The second has to do with how PopClip receives the output of the script. I’ve asked chatGPT to help me understand what the print statement does and it says:
" The last line of the code prints the cleaned URL to the console. The clean_url()
function takes a URL as input, removes the query string from it, and returns the cleaned URL as a string. The last line of the code calls the clean_url()
function with the POPCLIP_TEXT
environment variable, which contains the URL that was selected in the previous step by the user. The print()
statement then prints the cleaned URL to the console, with the end
parameter set to an empty string to avoid adding a newline character at the end of the printed string."
I’ve tried to replicate this in my code by adding the print statement as follows but it doesn’t seem to work. I realize that you work with JavaScript and that this might be a Python related problem that has nothing to do with how PopClip works but I’m taking this as an opportunity to learn so that I don’t have to bother other people in the future or I can myself be able to help others. If you can think of a reason why this doesn’t work with Python, this might be helpful in the future.
# #popclip
# name: TESTIN URL
# icon: TESTIN
# interpreter: python3
# requirements: [url]
# after: copy-result
import re
import pyperclip
url_pattern = r"(https?://[^\s]+)\?.*"
def clean_url(text):
match = re.match(url_pattern, text)
if match:
return match.group(1)
return text
def clean_url_clipboard():
text = pyperclip.paste()
cleaned_text = clean_url(text)
pyperclip.copy(cleaned_text)
if __name__ == "__main__":
clean_url_clipboard()
print(clean_url_clipboard(os.environ['POPCLIP_TEXT']), end='')