Searching in Finder from Selection

梦想的初衷 提交于 2019-12-13 18:06:48

问题


Usually I get an excel spreadsheet with dozens of filenames, for which I then need to go and search individually.

Spreadhseet

Is there a way that I could simply:

  1. Select All filenames in e.g. row A of Excel,
  2. then Search for all these files on "This Mac"
  3. then Copy all found files into the New Folder on the Desktop

So far I've tried the first part of searching and this is what i get :a)

Automator with Variable. But the problem is, it only searches for 1 file from selection

b)

Automator with Shell Script (Copy to Clipboard > Open Finder > CMD+F (to highlight Search dialog) > CMD+V). It opens a new Finder window, but it doesn't paste the clipboard into search dialog

c) /usr/bin/pbcopy

on run {input, parameters}
    tell application "System Events"
        keystroke "f" using {command down}
        keystroke "v" using {command down}
    end tell    
    return input
end run`

End result, is same as option b). I was planning to run this in Automator as a 'Service', which I could later assign to Keyboard Shortcut.

I am pretty sure there should be a simple shell option for this - any advice would be much appreciated.


回答1:


I made a bash script that does what you want. You would basically select a bunch of filenames in Excel, or any other app, and copy them to the clipboard with C. After that you need to run the script and it will take items from the clipboard and search for TIFF or JPEG images that match that name and copy them to a directory on your Desktop called Selected Files:

#!/bin/bash

# Get contents of clipboard into bash array
files=( $(pbpaste) )

# Create output directory - no checks for already existing or already containing files
OUTDIR="$HOME/Desktop/Selected Files"
mkdir -p "$OUTDIR"

# Iterate through fetching files
for ((i=0;i<${#files[@]};i++)) ; do
   name=${files[i]}
   result=$( mdfind "kMDItemDisplayName == \"${name}.*\" && (kMDItemKind==\"TIFF image\" || kMDItemKind==\"JPEG image\")" )
   if [ -f "$result" ]; then
      echo $name: $result
      cp "$result" "$OUTDIR"
   else
      echo ERROR: Searched for: $name, found $result
   fi
done

I am not sure of your level of familiarity with bash, so you may be able to ignore the following...

Make a new directory for your own scripts:

mkdir -p $HOME/scripts

Save the above script in that directory with filename:

$HOME/scripts/gather

Make the script executable by typing this into Terminal:

chmod +x $HOME/scripts/gather

Edit your login profile ($HOME/.profile) and add your $HOME/scripts directory to your PATH:

export PATH="$PATH":$HOME/scripts

Then start a new Terminal and you can use any script that you have saved in $HOME/scripts without needing to specify the full path to it, e.g.:

gather

Following information kindly contributed by @user3439894 in comments section, as I am out of my depth on this aspect...

To use a keyboard shortcut, you'd have to create an Automator "Service workflow" with a "Run Shell Script" action, which you can assign a keyboard shortcut to under: System Preferences > Keyboard > Shortcuts > Services



来源:https://stackoverflow.com/questions/52065902/searching-in-finder-from-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!