Page 1 of 1

OneDrive: script for dealing with local and Cloud-only files

PostPosted: Fri Aug 22, 2025 6:41 am
by warm.bunch8795
I created a script to use with Hazel on my camera images that are sync'ed to OneDrive. In my case I want to mark files as Cloud-only 6 months after they've been created to free up disk space.

You can use in your Hazel conditions to match downloaded or Cloud-only files. For example, to only match files that are present locally I use this condition:
Code: Select all
Passes shell script → Embedded Script → /path/to/onedrive.sh is_downloaded "$1"

All usages:
  • onedrive.sh is_downloaded: to check if the file is present locally
  • onedrive.sh is_online: to check if the file is Cloud-only
  • onedrive.sh pin: ask OneDrive to keep a copy locally
  • onedrive.sh unpin: ask OneDrive to not keep a copy locally (only a preview)

The onedrive.sh script:
Code: Select all
#!/usr/bin/env bash

DELAY="1,1,2,3,5,8"

CMD=$1
FILE=$2

function print_usage() {
  echo "usage: $0 (pin|unpin|is_downloaded|is_online) /path/to/file" >&2
  exit -1
}

if [[ $# != 2 || ! -f "$FILE" ]]; then
  print_usage
fi

if [[ "$CMD" == "is_downloaded" ]]; then
  $(/usr/bin/fileproviderctl evaluate "$FILE" | grep -q "isDownloaded = 1")
  exit $?
fi

if [[ "$CMD" == "is_online" ]]; then
  $(/usr/bin/fileproviderctl evaluate "$FILE" | grep -q "isDownloaded = 0")
  exit $?
fi

if [[ "$CMD" == "pin" ]]; then
  /Applications/OneDrive.app/Contents/MacOS/OneDrive /pin "$FILE"
  /opt/homebrew/bin/retry --delay "$DELAY" --times 5 "$0" is_downloaded "$FILE"
  exit $?
fi

if [[ "$CMD" == "unpin" ]]; then
  /Applications/OneDrive.app/Contents/MacOS/OneDrive /unpin "$FILE"
  /opt/homebrew/bin/retry --delay "$DELAY" --times 5 "$0" is_online "$FILE"
  exit $?
fi

print_usage


I see now that it uses /opt/homebrew/bin/retry (brew install retry) for when operations don't succeed at first. Feel free to remove those lines if you don't want to have retries.