MacOS – Getting Automator file selection from multiple windows

automatorfindermacos

I've created an automator action (service) which looks at two different files or folders and then performs an action based on their paths and filename. The problem is, each pair of files/folders is in a different location. I can open up two Finder windows and select the file or folder in each one, but when I then try to run the Automator action, it only passes the name of the file from the currently selected Finder window.

How can I make Automator run the action on files from different folders?

My automator has a single action, "Run Shell Script" with the following script:

for filepath in "$@"; do
  P4TH=`echo "$filepath" | rev | cut -d/ -f2- | rev`
  FILE=`echo "$filepath" | rev | cut -d/ -f1 | rev` 
  echo "P4TH=$P4TH, FILE=$FILE" >> /Users/michael/debug.txt
done

Best Answer

This is possible by selecting the second window to get the selection.

Insert the "Run AppleScript" action at the first position in the workflow, clear all text in the action, and put this script in the action:

on run {input, parameters}
    tell application "Finder"
        activate -- doesn't work without the activate
        open target of Finder window 2 ---  select the second Finder window
        set end of input to (item 1 of (get selection)) as alias -- append the selection (in the second window) to the input list
        open target of Finder window 2
    end tell
    return input
end run

Tested on Mavericks