Created
December 17, 2024 12:44
-
-
Save juanfal/e15e53b2734360e2782d334557529b1f to your computer and use it in GitHub Desktop.
select in Finder front Window from terminal, can add more to the already selected.
This file contains 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
#!/usr/bin/env ruby | |
# | |
# 2024-12-17 | |
# To gist | |
# 2016-07-25 | |
# 2023-07-29 | |
# Fixed many mistakes if not all. It simply adds the new items to the | |
# already selected on Finder | |
# inspired on | |
# http://www.red-sweater.com/AppleScript/ | |
# | |
# 2024-01-06 | |
# Added strip to the args… yes I know, there can be spaces sometimes | |
# it helps in terminal after blibros triple click selecting lines | |
# | |
# Use: | |
# On Terminal do: | |
# selectinfinder.rb *pattern* | |
# so a list of glob expanded item names in the directory is produced | |
# This files are added to the selected ones in the top most window only when both | |
# coincide (the window for the first arg and the window with the previously selected items) | |
# | |
# selectinfinder.rb - *pattern* | |
# | |
# does not add | |
# | |
# Limitations: | |
# It only only selects items in one directory/window, the path for the first one | |
# Because of the way finder selection is handled (I thing) no directories can | |
# be added | |
$añade = true | |
$progn=File.basename($0) | |
theFinderPath=%x{osascript -e ' | |
tell application "Finder" | |
try | |
get the POSIX path of (folder of the front window as string) | |
on error | |
get the POSIX path of (path to desktop folder as string) | |
end try | |
end tell' | |
}.chomp | |
Dir.chdir theFinderPath | |
puts "Front window: #{theFinderPath}" | |
pattern = [] | |
if ARGV.length>0 then | |
ARGV.each do |it0| | |
it = it0.strip | |
if it == '-h' | |
puts <<~END | |
#$progn [-] '*.cpp' 'adir/*.py' | |
with - it doesn't add but directly selects files | |
END | |
return 0 | |
elsif it == '-' then | |
$añade = false | |
else | |
puts "Globbing: #{Dir.pwd}/#{it}" | |
pattern += Dir.glob(it) | |
end | |
end | |
end | |
puts "Files to select: #{pattern}" | |
return if pattern.length == 0 | |
pattern.map! {|d| (d =~ /^\//)? d : File.join(theFinderPath,d.sub(/^\.\//, ""))} | |
if $añade then | |
alreadySelectedItems = %x{osascript <<-END | |
set AppleScript's text item delimiters to ":" | |
set selectedItems to {} | |
tell application "Finder" | |
set selectedItems to selection | |
end tell | |
set allPaths to {} | |
repeat with bob in selectedItems | |
set end of allPaths to "/" & POSIX path of (bob as alias) | |
end repeat | |
allPaths | |
END} | |
puts "There was: #{alreadySelectedItems}" | |
allPaths = alreadySelectedItems.chomp.split(/, \//) + pattern | |
else | |
allPaths = pattern | |
end | |
puts "There will be: #{allPaths.join ', '}" | |
allPaths.map! {|x| %Q{posix file "#{x}"} } | |
allPathString = allPaths.join ", " | |
puts | |
puts "allPathString: #{allPathString}" | |
puts | |
%x{osascript -e 'tell application "Finder" to activate'} | |
%x{osascript <<-END | |
tell application "Finder" to select { #{allPathString} } | |
END} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment