I asked Google bard A.I. & Chatgpt4 to help me do this. I almost succeeded, but the script no matter how many times I ask A.I. to improve it, It will only save the file as ".png" without even a single character file name.
My goal is to make a taskbar icon/one click button that executes a script that converts the image copied into the clipboard into PNG, saving it always as a PNG image file, and also give it a random filename ranging from 10 to 12 characters/letters or numbers. And to double check & make sure no other file in that folder has the same file name. Thats it!
Here is the script I have so far, what is wrong with it? -Thanks!
#!/usr/bin/env bash
while true; do
# Generate a random filename with 10-12 characters, ensuring at least one character
filename=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10-12).png
# Check if the file exists
if [[ ! -f "/home/user/Downloads/$filename" ]]; then
# File doesn't exist, proceed to save
xclip -selection clipboard -t image/png -o - | convert - png:/home/user/Downloads/"$filename"
break
fi
done
Instead of trying bash, I gave google bard A.I., swarfendor's github link and asked it to create a python script instead of a bash script. I then created a desktop icon for it. And it works perfect! For some reason google bard & chatgpt4 are better at making python scripts than bash. Also the bash script that A.I. was making wouldnt ever kill its own task & was unknowingly running in my background & feedback looping itself? and causing my CPU to run & stay at 95%. LoL.
---Also thanks to @Mr_Magoo , & @Aravisian for the advice also, but i figured out the python solution before you guys replied.
Here is the python script that A.I. made for me that works great:
import os
import random
import string
from PIL import ImageGrab
def generate_random_filename(length):
letters_and_digits = string.ascii_letters + string.digits
return ''.join(random.choice(letters_and_digits) for _ in range(length)) + ".png"
def save_clipboard_image_to_png(save_path):
image = ImageGrab.grabclipboard()
if image:
while True:
filename = generate_random_filename(random.randint(10, 12))
file_path = os.path.join(save_path, filename)
if not os.path.exists(file_path):
image.save(file_path)
print(f"Image saved to {file_path}")
break
else:
print("No image found in clipboard.")
save_path = "/home/user/Downloads" # Replace with your desired save path
save_clipboard_image_to_png(save_path)