I wanted to write a program that was extremely wasteful, rewriting the copied text on a regular basis, so I'll try it. The source code is so short that it's probably for beginners.
However, only a part of the content will be rewritten.
First, get the text copied to the clipboard. You can use pyperclip for this.
command prompt
pip install pyperclip
You should be able to install it by typing in the command prompt. Let's copy the previous sentence and run the program.
sample01.py
import pyperclip, re
copy_text = str(pyperclip.paste())
print(copy_text)
Execution result
However, it is too obvious and not interesting to rewrite everything.
So why not change only part of it?
I succeeded in getting the copied text, so I try copying the text to the clipboard.
sample02.py
copy_text = 'hello world!'
print(copy_text)
pyperclip.copy('\n'.join(copy_text))
Execution result
My name is Alice
hello world!
Let's rewrite the copied text.
It's not interesting to change everything (you don't even need to get the text in the first place), so I want to change only a part. For example, numbers. When the numbers come out, replace them with random numbers.
I think that the sentences to be copied are often in Japanese, so let's change the alphabet. This will be rewritten only when a specific character string appears.
sample03.py
import pyperclip, re
copy_text = str(pyperclip.paste())
print(copy_text)
new = re.sub(r'[\d]', '*', copy_text)
new = re.sub(r'day|best|hello', '****', new)
if len(copy_text) > 0:
pyperclip.copy('\n'.join(new))
print('Rewrite:', new)
else:
print('Nothing')
Replace strings with re.sub. r''is a regular expression. I was thinking about replacing it with a random number or changing it to a random string, but it didn't work. I'm sure someone will think good about it.
Execution result
I will do my best day to day!
Rewrite: I will do my **** **** to ****!
By the way, the English text I copied is a Google translation of "I'll do my best today!" I'm looking forward to the second term of New Game.
The last oshigoto. By executing Thread from Thread, it will be executed every 5 seconds. The program up to the above was partially changed.
copyChange.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
u"""Rewrite the text on the Hoge clipboard without permission
Example sentence: I will do my best day to day 2017!
"""
import pyperclip, re, threading, random
def task():
copy_text = str(pyperclip.paste())
new = re.sub(r'[\d]', str(random.randint(0, 9)), copy_text)
new = re.sub(r'day|best|hello', '****', new)
if len(copy_text) > 0:
pyperclip.copy(''.join(new))
print('---Rewrite Complete!---')
else:
print('---Failure---')
th = threading.Timer(5, task)
th.start()
t = threading.Thread(target=task)
t.start()
I rewrote it to change the number using random so that I can see that it is repeating regularly. At the same time, 2017 has been added to the example sentences. Also, I deleted it because it would be an obstacle to print the copied text when executing it.
Execution result
---Rewrite Complete!---
---Rewrite Complete!---
---Rewrite Complete!---
Clipboard
I will do my **** **** to **** 4444!
I will do my **** **** to **** 7777!
I will do my **** **** to **** 8888!
Thank you for your hard work.
The clipboard cannot be rewritten unless this program is started. Therefore, I will present a method to start it automatically in order to further increase the degree of violence.
This time it is assumed to be Windows, so you can use the task scheduler. I think that other OSs have their own automatic startup methods, so if you want to be bullied by your own PC, you should definitely search and try it. Start Task Scheduler
You may be able to do it with schtasks at the command prompt. I've never used it, but I don't know.
--I received an edit request that the code is not displayed correctly. It has been fixed now. Even if it is displayed on kobito, it may not work well on Qiita. Thank you for your report and correction.
Recommended Posts