Skip to content

Commit 90200b6

Browse files
committed
try to fix duplicate process handling
1 parent 4c6a180 commit 90200b6

6 files changed

Lines changed: 32 additions & 60 deletions

File tree

Casks/screenshot-renamer.rb

Lines changed: 0 additions & 36 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,13 @@ without touching unrelated files.
2121

2222
## Installation
2323

24-
### Option 1: Homebrew
25-
26-
```bash
27-
brew install --cask https://raw.githubusercontent.com/glenrobertson/macos-ai-screenshot-renamer/main/Casks/screenshot-renamer.rb
28-
```
29-
30-
You'll be prompted to pick a folder for screenshots (defaults to `~/Screenshots`). Then click **"Add Shortcut"** when prompted, and grant any permissions in **System Settings > Privacy & Security**. Uninstall with `brew uninstall --cask screenshot-renamer`.
31-
32-
### Option 2: Installer Package
24+
### Option 1: Installer Package
3325

3426
Download `Screenshot-Renamer.pkg` from the [latest release](https://github.com/glenrobertson/macos-ai-screenshot-renamer/releases) and double-click to install.
3527

3628
You'll be prompted to pick a folder for screenshots (defaults to `~/Screenshots`). Then click **"Add Shortcut"** when prompted, and grant any permissions in **System Settings > Privacy & Security**.
3729

38-
### Option 3: Command Line
30+
### Option 2: Command Line
3931

4032
```bash
4133
git clone https://github.com/glenrobertson/macos-ai-screenshot-renamer.git
@@ -123,8 +115,6 @@ Logs are written to `~/Library/Logs/rename-screenshot.log`.
123115
124116
## Uninstall
125117
126-
If you installed via Homebrew: `brew uninstall --cask screenshot-renamer`.
127-
128118
If you installed via the `.pkg`, double-click `Uninstall-Screenshot-Renamer.pkg` from the [latest release](https://github.com/glenrobertson/macos-ai-screenshot-renamer/releases). Otherwise:
129119
130120
```bash

Rename Screenshot.shortcut

46 Bytes
Binary file not shown.

install.sh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,42 @@ cat > "$WATCHER_SCRIPT" <<WATCHER
9797
9898
SCREENSHOTS_DIR="$SCREENSHOTS_DIR"
9999
TIMESTAMP_FILE="\$HOME/Library/Scripts/.rename-screenshot-lastrun"
100+
LOCK_DIR="\$HOME/Library/Scripts/.rename-screenshot.lock"
100101
101102
if [[ ! -f "\$TIMESTAMP_FILE" ]]; then
102103
touch -t 197001010000 "\$TIMESTAMP_FILE"
103104
fi
104105
106+
# WatchPaths fires multiple fsevents per screenshot save. Without a lock the
107+
# parallel runs race the Shortcut's move and produce "item with the same
108+
# name already exists" errors. Stale locks >5min get reclaimed.
109+
if ! mkdir "\$LOCK_DIR" 2>/dev/null; then
110+
if [[ -z \$(find "\$LOCK_DIR" -maxdepth 0 -mmin -5 2>/dev/null) ]]; then
111+
rmdir "\$LOCK_DIR" 2>/dev/null
112+
mkdir "\$LOCK_DIR" 2>/dev/null || exit 0
113+
else
114+
exit 0
115+
fi
116+
fi
117+
trap 'rmdir "\$LOCK_DIR" 2>/dev/null' EXIT INT TERM
118+
105119
sleep 2
106120
107-
MARKER=\$(mktemp)
108-
while IFS= read -r file; do
109-
[[ -f "\$file" ]] || continue
110-
shortcuts run "Rename Screenshot" --input-path "\$file" >/dev/null
111-
done < <(find "\$SCREENSHOTS_DIR" -maxdepth 1 -name "Screenshot *.png" -newer "\$TIMESTAMP_FILE" ! -newer "\$MARKER")
112-
touch -r "\$MARKER" "\$TIMESTAMP_FILE"
113-
rm -f "\$MARKER"
121+
# Loop so screenshots arriving while the Shortcut is running are still
122+
# picked up. MARKER bounds each pass so files landing after find but before
123+
# the timestamp advance aren't skipped.
124+
while :; do
125+
MARKER=\$(mktemp)
126+
found=0
127+
while IFS= read -r file; do
128+
[[ -f "\$file" ]] || continue
129+
found=1
130+
shortcuts run "Rename Screenshot" --input-path "\$file" >/dev/null
131+
done < <(find "\$SCREENSHOTS_DIR" -maxdepth 1 -name "Screenshot *.png" -newer "\$TIMESTAMP_FILE" ! -newer "\$MARKER")
132+
touch -r "\$MARKER" "\$TIMESTAMP_FILE"
133+
rm -f "\$MARKER"
134+
(( found == 0 )) && break
135+
done
114136
WATCHER
115137
chmod +x "$WATCHER_SCRIPT"
116138
[ -n "$CHOWN" ] && chown "$CHOWN" "$WATCHER_SCRIPT"

pkg-build/build-pkg.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,3 @@ fi
8888

8989
echo
9090
echo "Done! Packages are in: $BUILD_DIR/"
91-
echo
92-
echo "SHA256 (paste into Casks/screenshot-renamer.rb):"
93-
shasum -a 256 "$BUILD_DIR/Screenshot-Renamer.pkg" | awk '{print $1}'

pkg-build/scripts/postinstall

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ STAGING="/usr/local/share/screenshot-renamer"
88

99
"$STAGING/install.sh" "$STAGING/Rename Screenshot.shortcut"
1010

11-
# Clean up install-only artifacts but leave uninstall.sh in place so the
12-
# Homebrew cask (and manual uninstall) can call it back later.
11+
# Clean up install-only artifacts but leave uninstall.sh for manual uninstall.
1312
rm -f "$STAGING/install.sh"
1413
rm -rf "$STAGING/Rename Screenshot.shortcut"
1514

0 commit comments

Comments
 (0)