Skip to content

Commit

Permalink
cron: Take screenshots of all displays daily at 3:00 PM.
Browse files Browse the repository at this point in the history
If run `~/.local/bin/screenshot_all_displays.sh` directly in crontab,
it can only capture the desktop wallpaper, not the apps that are
running. This is because crontab does not have the permission to record
the screen.

Therefore, [Automator][1] was used to solve this problem. It
can run shell scripts, and after saving it as an application, it can
use `open` to call and run it. The first time it runs, it will ask you
for permission as an application. This way, it can take screenshots in
crontab.

[1]: https://en.wikipedia.org/wiki/Automator_(macOS)
  • Loading branch information
kang8 committed Jan 1, 2025
1 parent 4200966 commit e4f6b94
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bin/.local/bin/screenshot_all_displays.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

SAVE_DIR="$HOME/kang8/screenshots"

if [ ! -d "$SAVE_DIR" ]; then
mkdir --parents "$SAVE_DIR"
fi

DISPLAY_COUNT=$(/usr/sbin/system_profiler SPDisplaysDataType -json | /usr/bin/jq '.SPDisplaysDataType[].spdisplays_ndrvs | length')

for ((i=1; i<=DISPLAY_COUNT; i++)); do
if [ $i -eq 1 ]; then
DISPLAY_NAME="main"
else
DISPLAY_NAME="secondary_$((i-1))"
fi

FILENAME="$(date '+%Y%m%d')_${DISPLAY_NAME}_screen.png"

/usr/sbin/screencapture -D $i -xC "$SAVE_DIR/$FILENAME"

echo "Screenshot saved to $SAVE_DIR/$FILENAME"
done
3 changes: 3 additions & 0 deletions crontab
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
# Run at 11:05pm every day
5 11 * * * cd ~/Library/Rime && git pull

# Run at 15:00pm every day
0 15 * * * ~/.local/bin/screenshot_all_displays.sh && open ~/Applications/ScreenshotAllDisplays.app

# Run at 11:31pm every Tuesday
31 11 * * 2 cd ~/.config/nvim && git fetch && git rebase origin/master && ~/.local/share/bob/nvim-bin/nvim --headless "+Lazy! restore" +qa

0 comments on commit e4f6b94

Please sign in to comment.