Skip to content

Instantly share code, notes, and snippets.

@leoncowle
Last active December 3, 2024 12:18
Show Gist options
  • Save leoncowle/925433463188e6372772602e729be536 to your computer and use it in GitHub Desktop.
Save leoncowle/925433463188e6372772602e729be536 to your computer and use it in GitHub Desktop.
From Terminal, run AppleScript to select files in Finder
# Author: Leon Cowle <[email protected]>
# Version: 0.1 (9/21/2024)
# License: Free to use/copy/whatever :)
# Simple AppleScript script to call from Terminal. It opens a Finder window to the current working dir,
# and then selects all the files given as arguments to the AppleScript script.
# Suggestion: use an alias in your shell, e.g.:
# alias sel="osascript /path/to/this/finderselect.scpt"
# and then run it like this:
# $ sel image2015*.jpg
# to open a Finder window to the current directory, and have all the image2015*.jpg files selected
on run argv
tell application "Finder"
# Get current dir
set thePWD to do shell script "pwd"
# Open Finder window to that dir
open (thePWD as POSIX file)
activate
# Set thePWD to a POSIX path of current dir
set thePWD to POSIX path of ((folder of front window) as alias)
# Iterate over arguments, make them fully qualified, add to list
set theFiles to {}
repeat with i from 1 to count of argv
set theName to thePWD & item i of argv
set end of theFiles to POSIX file theName as alias
end repeat
# Select them
select theFiles
return
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment