Automator steps to create relative path to parent folder

Directory structure....

--G
 - Action.app (this Automator app)
 - Random.app

 - Folder 1
  - File A.a
  - File B.t
  - File C.q

 - Folder 2
  - File D.a
  - File B.t
  - File C.q

I click-drag Folder 1 and Folder 2 and drop them on Action.app. (The names of these folders are always different).

The Action.app moves all files in the folders to the parent directory then deletes the folder. Duplicate files are overwritten during move. So I end up with:

--G
 - Action.app (Automator app)
 - Random.app
  - File A.a
  - File D.a
  - File B.t
  - File C.q

This works....

enter image description here


What I'm trying to accomplish....

The above automator action has the parent folder hard-coded in the steps - --G – that's the parent folder. I want to remove that hard coded folder and make it relative to the items I've dragged (or location of the action.app). That way I can place this action in any directory and run it.

I have searched quite a bit and tried, I think, every question here regarding "getting parent folder" and none of them seem to work, using Applescript, shell scripts, etc. for this basic instance. In fact, one of the shell solutions started moving files and deleting directories on the startup volume/desktop (ooof!).

I've tried the following...

enter image description here

This does set the parentFolder variable to the parent directory's name (verified in results).

But the variable parentFolder is not an option for the Move Finder Items step.

If I don't "ignore input" after setting the variable, then everything in the parent folder is processed (breaking the apps, because "Contents" is extracted from them) - the drag/drop doesn't target specific folders.

I realize I'm probably overlooking something simple. Any help?

In case: Mojave 10.14.6 / Automator 2.9


Solution 1:

A couple things stand out for me. I'm using Sierra so YMMV.

First, instead of using a generic variable, try using Path. It is intended for file locations and it might make a difference. Second, you might re-order your actions slightly. See below.

Update 1: Now an application; Should move only selected subfolders;

Since you intend to drop folders on it, I removed the initial 'get selected…'. Save as an application and it should default to receiving files and folders as input.

  • Run Applescript -- Use yours, as is. Result should be an alias referring to the parent (and destination)

  • Set Value of Variable -- Use the 'Path' variable. The result should be the alias from the applescript but as a single-item list

  • Get Selected Finder Items -- Check 'ignore…'; result should be the selected sub-folders, but not the parent

  • Get Folder Contents -- Check 'repeat for…', should result in selected sub-folders and their contents

  • Filter Finder Items -- All > Kind is not folder; will result in all files but no sub-folders

  • Move Finder Items -- Drag 'Path' onto the 'To:' dropdown; Check 'replacing…'; should result in same files as previous action but moved to the parent folder

  • Get Selected Finder Items -- Check 'ignore…'; All files from previous action will be ignored and results should be the now-empty sub-folders

  • Move Finder Items to Trash -- Sub-folders from previous action should now be in trash but the action will not list any results.

Something you can try in a workflow. There is a View Results action. Insert one after 'run applescript' and remove everything after. When you run the script, it will show you the results. You can then add the 'set variable' action and move the 'view results' below it and run the script again. You should see that both actions have the same results… i.e. the parent folder of item 1 of your selection. I find variables to be rather opaque and this can help clarify what is going on.

Update 2:

To aid with visualization, here is a capture of a functional workflow. The key is to use the Path variable. Credit to @Scott for the cap and annotations.

Workflow visualization, annotated1