Last active
July 20, 2025 12:33
-
-
Save hsingh23/1aba0eeaa72c430ba9910a2a7ed9409b to your computer and use it in GitHub Desktop.
select-files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# A concise Zsh utility to select files in macOS Finder. | |
# | |
# Usage: | |
# select-files [-f /path/to/folder] file1.txt file2.jpg... | |
# -f: (Optional) Specify a target folder. Defaults to the current directory. | |
# --- Helper Functions --- | |
# Resolves a path to its absolute form for AppleScript | |
resolve_path() { | |
if command -v realpath &>/dev/null; then | |
realpath "$1" | |
elif command -v greadlink &>/dev/null; then | |
greadlink -f "$1" | |
else | |
[[ "$1" = /* ]] && echo "$1" || echo "$PWD/${1#./}" | |
fi | |
} | |
# --- Main Logic --- | |
run_selection() { | |
local folder_path="$1" | |
shift | |
local files_to_check=("$@") | |
local abs_folder=$(resolve_path "$folder_path") | |
# 1. Check which files exist and which are missing | |
local found_filenames=() missing_files=() | |
for file in "${files_to_check[@]}"; do | |
if [[ -e "$abs_folder/$file" ]]; then | |
found_filenames+=("$file") | |
else | |
missing_files+=("$file") | |
fi | |
done | |
# 2. Get unique filenames and identify duplicates | |
# (u) flag gets unique array elements | |
local unique_filenames=(${(u)found_filenames}) | |
# Find duplicates by comparing sorted lists | |
local duplicate_files=($(printf "%s\n" "${found_filenames[@]}" | sort | uniq -d)) | |
# 3. Select the unique files in Finder | |
if [[ ${#unique_filenames[@]} -gt 0 ]]; then | |
# CORRECTED: Store AppleScript in a variable and call with -e | |
# This correctly passes arguments to the 'on run' handler. | |
local applescript_code=' | |
on run argv | |
if (count of argv) < 2 then return | |
set folderPath to item 1 of argv | |
set fileNames to rest of argv | |
set theItems to {} | |
tell application "Finder" | |
try | |
activate | |
set containerFolder to (POSIX file folderPath) as alias | |
repeat with aName in fileNames | |
try | |
set end of theItems to (file aName of containerFolder) | |
end try | |
end repeat | |
if (count of theItems) > 0 then | |
reveal theItems | |
end if | |
end try | |
end tell | |
return | |
end run | |
' | |
osascript -e "$applescript_code" "$abs_folder" "${unique_filenames[@]}" | |
else | |
osascript -e "tell application \"Finder\" to reveal POSIX file \"$abs_folder\"" | |
fi | |
# 4. Report any missing or duplicate files | |
if [[ ${#missing_files[@]} -gt 0 || ${#duplicate_files[@]} -gt 0 ]]; then | |
echo "---" | |
fi | |
if [[ ${#missing_files[@]} -gt 0 ]]; then | |
echo "⚠️ Missing files:" | |
printf " - %s\n" "${missing_files[@]}" | |
fi | |
if [[ ${#duplicate_files[@]} -gt 0 ]]; then | |
echo "ℹ️ Duplicate files ignored:" | |
printf " - %s\n" "${duplicate_files[@]}" | |
fi | |
} | |
# --- Script Entry Point --- | |
TARGET_FOLDER="." | |
if [[ "$1" == "-f" ]]; then | |
if [[ -z "$2" ]]; then | |
echo "Error: -f option requires a folder path." >&2; exit 1 | |
fi | |
TARGET_FOLDER="$2" | |
shift 2 | |
fi | |
FILES_TO_CHECK=("$@") | |
if [[ ${#FILES_TO_CHECK[@]} -eq 0 ]]; then | |
echo "Usage: $(basename "$0") [-f /path/to/folder] file1 [file2...]" | |
echo "No files specified. Opening target folder..." | |
run_selection "$TARGET_FOLDER" | |
exit 0 | |
fi | |
run_selection "$TARGET_FOLDER" "${FILES_TO_CHECK[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment