0

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
  • Maybe sharing your workflow will help answering it – Ruskes Sep 20 '14 at 3:37
  • @Buscar ok sure, it's just a skeleton at the moment, but I'll update the question. – Michael Sep 20 '14 at 3:47
1

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

1

The only thing a can think of is a little bit of GUI Applescript.

Place this code into a 'Run Applescript' action above your 'Run Shell script' action.

The Applescript is commented to explain what it is doing. But be aware this code is to start you of.

It works in my tests.

NOTE THIS SCRIPT WORKS WHEN THE WINDOWS ARE IN LIST VIEW

But not if you select an item in a list that is actually under another folder .i.e folder discloser is pointing down and you have selected an item from within it. You will get an error.

    set thePaths to {} -- empty list

    (* NOTE THIS SCRIPT WORKS WHEN THE WINDOWS ARE IN LIST VIEW *)
    tell application "Finder"
        activate
        set theWindows to target of windows -- get the windows target paths
        repeat with i from 1 to number of items in theWindows -- repeat for each window
            set this_window to item i of theWindows -- get window #n
            set thisSelection to my getSelected(i) as string -- run sub routine and pass the item count as the arguments

            set thePath to POSIX path of (item thisSelection of this_window as alias) -- convert the path to unix style path
            copy thePath to end of thePaths -- add to list
        end repeat
    end tell
Return thePaths

    on getSelected(i)
        set theRowSelection to "" -- declare variable
        tell application "System Events"
            tell process "Finder"

                set theRowSelection to value of text field 1 of UI element 1 of ((rows of outline 1 of scroll area 1 of splitter group 1 of window i) whose value of attribute "AXSelected" is true) -- get the selected item by using the attributes of the window - WHICH HAS THE SIDE BAR SHOWING

                 if theRowSelection is {missing value} then -- THE SIDE BAR SHOWING WAS NOT SHOWING SO THE scroll area 1 NEEDS TO CHANGE TO scroll area 2

                    set theRowSelection to value of text field 1 of UI element 1 of ((rows of outline 1 of scroll area 2 of splitter group 1 of window i) whose value of attribute "AXSelected" is true)

                end if

            end tell
        end tell
        return theRowSelection -- return the selected item name
    end getSelected

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .