Skip to content

Instantly share code, notes, and snippets.

@alexziskind1
Created February 7, 2025 14:14
Show Gist options
  • Select an option

  • Save alexziskind1/2ee79770658b9f76ed1fb8d422b0f478 to your computer and use it in GitHub Desktop.

Select an option

Save alexziskind1/2ee79770658b9f76ed1fb8d422b0f478 to your computer and use it in GitHub Desktop.
MacOS Quick Action File Diff code with options for VSCode FileMerge and Terminal Diff
#!/bin/bash
# We expect exactly two files
if [ $# -ne 2 ]; then
osascript -e 'display alert "Please select exactly two files." as critical'
exit 1
fi
# Use osascript to show a choice dialog (AppleScript).
CHOICE="$(osascript <<EOT
set userChoice to choose from list {"VSCode Diff", "FileMerge", "Terminal Diff"} ¬
with prompt "Select a diff method:" ¬
without empty selection allowed
if userChoice is false then
return "Canceled"
else
return item 1 of userChoice
end if
EOT
)"
if [ "$CHOICE" = "Canceled" ]; then
exit 0
fi
# Decide how to diff based on the user's choice
case "$CHOICE" in
"VSCode Diff")
# Assumes 'code' is on PATH. If not, use the full path, e.g.:
# /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code -d ...
code -d "$1" "$2"
;;
"FileMerge")
# Adjust path if needed; this is the usual location for Xcode’s FileMerge:
/usr/bin/opendiff "$1" "$2"
;;
"Terminal Diff")
# We'll do a unified diff and open the result in TextEdit
diff -u "$1" "$2" | open -f -a TextEdit
;;
*)
echo "Unknown choice: $CHOICE"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment