no more uncertain crashes of screenshot script
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
<img src="https://raw.githubusercontent.com/Uyanide/dotfiles/refs/heads/main/assets/works-on-my-machine.png" alt="Works on my machine" width="200" />
|
<img src="https://raw.githubusercontent.com/Uyanide/dotfiles/refs/heads/main/assets/works-on-my-machine.png" alt="Works on my machine(s)" width="200" />
|
||||||
|
|
||||||
## How it looks like...
|
## How it looks like...
|
||||||
|
|
||||||
|
|||||||
@@ -54,27 +54,27 @@ random_name=$(tr -dc 'a-zA-Z0-9' </dev/urandom | head -c 16)
|
|||||||
current_dir="$HOME/.local/share/wallpaper/current"
|
current_dir="$HOME/.local/share/wallpaper/current"
|
||||||
image_copied="$current_dir/wallpaper-${random_name}.${ext}"
|
image_copied="$current_dir/wallpaper-${random_name}.${ext}"
|
||||||
|
|
||||||
mkdir -p "$current_dir" || (
|
mkdir -p "$current_dir" || {
|
||||||
echo "Could not create directory $current_dir"
|
echo "Could not create directory $current_dir"
|
||||||
exit 1
|
exit 1
|
||||||
)
|
}
|
||||||
|
|
||||||
temp_img=$(mktemp --suffix=."$ext") || exit 1
|
temp_img=$(mktemp --suffix=."$ext") || exit 1
|
||||||
trap 'rm -f "$temp_img"' EXIT
|
trap 'rm -f "$temp_img"' EXIT
|
||||||
cp "$image" "$temp_img" || exit 1
|
cp "$image" "$temp_img" || exit 1
|
||||||
rm -f "${current_dir:?}"/wallpaper-*
|
rm -f "${current_dir:?}"/wallpaper-*
|
||||||
cp -f "$temp_img" "$image_copied" || (
|
cp -f "$temp_img" "$image_copied" || {
|
||||||
echo "Could not copy image to $current_dir"
|
echo "Could not copy image to $current_dir"
|
||||||
exit 1
|
exit 1
|
||||||
)
|
}
|
||||||
|
|
||||||
# Generate blurred wallpaper
|
# Generate blurred wallpaper
|
||||||
|
|
||||||
blur_dir="$HOME/.local/share/wallpaper/blurred"
|
blur_dir="$HOME/.local/share/wallpaper/blurred"
|
||||||
mkdir -p "$blur_dir" || (
|
mkdir -p "$blur_dir" || {
|
||||||
echo "Could not create cache directory"
|
echo "Could not create cache directory"
|
||||||
exit 1
|
exit 1
|
||||||
)
|
}
|
||||||
rm -f "${blur_dir:?}"/blurred-*
|
rm -f "${blur_dir:?}"/blurred-*
|
||||||
blurred_image="$blur_dir/blurred-${random_name}.$ext"
|
blurred_image="$blur_dir/blurred-${random_name}.$ext"
|
||||||
|
|
||||||
@@ -93,14 +93,14 @@ blurred_image="$blur_dir/blurred-${random_name}.$ext"
|
|||||||
### use a temporary file to avoid incomplete file being used
|
### use a temporary file to avoid incomplete file being used
|
||||||
temp_blurred=$(mktemp --suffix=."$ext") || exit 1
|
temp_blurred=$(mktemp --suffix=."$ext") || exit 1
|
||||||
trap 'rm -f "${temp_blurred}"' EXIT
|
trap 'rm -f "${temp_blurred}"' EXIT
|
||||||
magick "$image_copied" -blur 0x"$sigma" "$temp_blurred" || (
|
magick "$image_copied" -blur 0x"$sigma" "$temp_blurred" || {
|
||||||
echo "Could not create blurred image"
|
echo "Could not create blurred image"
|
||||||
exit 1
|
exit 1
|
||||||
)
|
}
|
||||||
mv -f "$temp_blurred" "$blurred_image" || (
|
mv -f "$temp_blurred" "$blurred_image" || {
|
||||||
echo "Could not move blurred image to cache directory"
|
echo "Could not move blurred image to cache directory"
|
||||||
exit 1
|
exit 1
|
||||||
)
|
}
|
||||||
|
|
||||||
if [ "$XDG_CURRENT_DESKTOP" = "niri" ]; then
|
if [ "$XDG_CURRENT_DESKTOP" = "niri" ]; then
|
||||||
swww img -n backdrop "$blurred_image" --transition-type fade --transition-duration 2 > /dev/null 2> /dev/null
|
swww img -n backdrop "$blurred_image" --transition-type fade --transition-duration 2 > /dev/null 2> /dev/null
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ def take_screenshot(filepath: Path, typeStr: str):
|
|||||||
}
|
}
|
||||||
process = subprocess.run(f"{cmd[type]}{filepath.name}", shell=True)
|
process = subprocess.run(f"{cmd[type]}{filepath.name}", shell=True)
|
||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
raise RuntimeError("Failed to take screenshot.")
|
raise RuntimeError("Failed to take screenshot: hyprshot command failed.")
|
||||||
wait_until_file_exists(filepath)
|
wait_until_file_exists(filepath)
|
||||||
elif "niri" in currentDesktop:
|
elif "niri" in currentDesktop:
|
||||||
cmd = {
|
cmd = {
|
||||||
@@ -71,19 +71,17 @@ def take_screenshot(filepath: Path, typeStr: str):
|
|||||||
# if os.system(cmd[type]):
|
# if os.system(cmd[type]):
|
||||||
process = subprocess.run(cmd[type], shell=True)
|
process = subprocess.run(cmd[type], shell=True)
|
||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
print("Failed to take screenshot.")
|
raise RuntimeError("Failed to take screenshot: niri built-in screenshot command failed.")
|
||||||
exit(1)
|
|
||||||
wait_until_file_exists(niriScreenshotPath)
|
wait_until_file_exists(niriScreenshotPath)
|
||||||
if niriScreenshotPath.exists():
|
if niriScreenshotPath.exists():
|
||||||
# niriScreenshotPath.rename(filepath)
|
# niriScreenshotPath.rename(filepath)
|
||||||
copy2(niriScreenshotPath, filepath)
|
copy2(niriScreenshotPath, filepath)
|
||||||
else:
|
else:
|
||||||
print("Failed to take screenshot.")
|
raise RuntimeError("Failed to take screenshot: screenshot file nto found after niri command.")
|
||||||
exit(1)
|
|
||||||
wait_until_file_exists(filepath)
|
wait_until_file_exists(filepath)
|
||||||
else:
|
else:
|
||||||
print("Unsupported desktop environment.")
|
# print("Unsupported desktop environment.")
|
||||||
exit(1)
|
raise RuntimeError("Unsupported desktop environment.")
|
||||||
|
|
||||||
|
|
||||||
def edit_screenshot(filepath: Path):
|
def edit_screenshot(filepath: Path):
|
||||||
@@ -91,71 +89,78 @@ def edit_screenshot(filepath: Path):
|
|||||||
# subprocess.run(f"spectacle -l --edit-existing {filepath}", shell=True)
|
# subprocess.run(f"spectacle -l --edit-existing {filepath}", shell=True)
|
||||||
|
|
||||||
|
|
||||||
def file_name(dir: Path, prefix="screenshot", ext=".png"):
|
def gen_file_name(prefix="screenshot", ext=".png"):
|
||||||
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
return f"{prefix}_{timestamp}{ext}"
|
return f"{prefix}_{timestamp}{ext}"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Take screenshots with hyprshot.")
|
|
||||||
parser.add_argument(
|
|
||||||
"type",
|
|
||||||
choices=[t.value for t in ScreenshotType],
|
|
||||||
help="Type of screenshot to take.",
|
|
||||||
)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# file path
|
|
||||||
SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)
|
|
||||||
filename = file_name(SCREENSHOT_DIR)
|
|
||||||
filepath = SCREENSHOT_DIR / filename
|
|
||||||
|
|
||||||
# take screenshot
|
|
||||||
take_screenshot(filepath, args.type)
|
|
||||||
|
|
||||||
# check if successful
|
|
||||||
if not filepath.exists():
|
|
||||||
print("Failed to take screenshot.")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# create loop instance
|
|
||||||
loop = GLib.MainLoop()
|
|
||||||
editing = False
|
|
||||||
|
|
||||||
# callback on default action (edit)
|
|
||||||
def edit_callback(n, action, user_data):
|
|
||||||
try:
|
|
||||||
global editing
|
|
||||||
editing = True
|
|
||||||
edit_screenshot(filepath)
|
|
||||||
finally:
|
|
||||||
n.close()
|
|
||||||
loop.quit()
|
|
||||||
|
|
||||||
# callback on close
|
|
||||||
def close_callback(n):
|
|
||||||
global editing
|
|
||||||
if not editing:
|
|
||||||
loop.quit()
|
|
||||||
|
|
||||||
# notification
|
|
||||||
Notify.init("Screenshot Utility")
|
Notify.init("Screenshot Utility")
|
||||||
n = Notify.Notification.new(
|
|
||||||
"Screenshot taken",
|
|
||||||
# Mako doesn't have action buttons displayed with notification cards,
|
|
||||||
"Click to edit",
|
|
||||||
)
|
|
||||||
n.add_action(
|
|
||||||
# so default action is used here, which will be triggered on clicking the notification card
|
|
||||||
"default",
|
|
||||||
# But for my (or to be precise, Noctalia's) quickshell config, buttons will be displayed with label
|
|
||||||
"Open in Editor",
|
|
||||||
edit_callback,
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
|
|
||||||
# set timeout for close_callback
|
try:
|
||||||
GLib.timeout_add_seconds(10, close_callback, n)
|
# raise RuntimeError("Never gonna give you up, never gonna let you down.")
|
||||||
|
parser = argparse.ArgumentParser(description="Take screenshots with hyprshot.")
|
||||||
|
parser.add_argument(
|
||||||
|
"type",
|
||||||
|
choices=[t.value for t in ScreenshotType],
|
||||||
|
help="Type of screenshot to take.",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
n.show()
|
# file path
|
||||||
loop.run()
|
SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
filename = gen_file_name()
|
||||||
|
filepath = SCREENSHOT_DIR / filename
|
||||||
|
|
||||||
|
# take screenshot
|
||||||
|
take_screenshot(filepath, args.type)
|
||||||
|
|
||||||
|
# check if successful
|
||||||
|
if not filepath.exists():
|
||||||
|
raise RuntimeError("Failed to take screenshot: screenshot file not found.")
|
||||||
|
|
||||||
|
# create loop instance
|
||||||
|
loop = GLib.MainLoop()
|
||||||
|
editing = False
|
||||||
|
|
||||||
|
# callback on default action (edit)
|
||||||
|
def edit_callback(n, action, user_data):
|
||||||
|
try:
|
||||||
|
global editing
|
||||||
|
editing = True
|
||||||
|
edit_screenshot(filepath)
|
||||||
|
finally:
|
||||||
|
n.close()
|
||||||
|
loop.quit()
|
||||||
|
|
||||||
|
# callback on close
|
||||||
|
def close_callback(n):
|
||||||
|
global editing
|
||||||
|
if not editing:
|
||||||
|
loop.quit()
|
||||||
|
|
||||||
|
n = Notify.Notification.new(
|
||||||
|
"Screenshot Taken",
|
||||||
|
# Mako doesn't have action buttons displayed with notification cards,
|
||||||
|
"Click to edit",
|
||||||
|
)
|
||||||
|
n.add_action(
|
||||||
|
# so default action is used, which will be triggered on simply clicking the notification card
|
||||||
|
"default",
|
||||||
|
# But for my (or more accurate, Noctalia's) quickshell config, buttons will be displayed with label, even for the default action
|
||||||
|
"Open in Editor",
|
||||||
|
edit_callback,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
|
# set timeout for close_callback
|
||||||
|
GLib.timeout_add_seconds(10, close_callback, n)
|
||||||
|
|
||||||
|
n.show()
|
||||||
|
loop.run()
|
||||||
|
except Exception as e:
|
||||||
|
n = Notify.Notification.new(
|
||||||
|
"Screenshot Error",
|
||||||
|
str(e),
|
||||||
|
)
|
||||||
|
n.show()
|
||||||
|
|||||||
Reference in New Issue
Block a user