2

I cannot find any information on this, so I hope somebody with experience can fill in the gap.

I am writing a simple Automator Service, and I can’t get past the first step.

  • The script is a Service
  • Service receives selected files or folders in Finder.app
  • Run AppleScript

The script is

on run {input,parameters}
    set test to container of input -- Can’t make container into type reference.
    display dialog test as string
    return input
end run

The input parameter has the selected file or folder, and all I want to do at this stage is to get the parent folder of the selected item. Everything I try, which is from countless near solutions on the web fail in this, telling me that I can’t make the container into a reference.

I have no idea what I should be doing, and I can’t find any information on this.

How can I get the parent folder using Automator?

1

The curly-brace portion of on run {input, parameters} creates a list and as such you need to address input as a list.

The following example assumes only one file or folder gets passed to the Service:

on run {input, parameters}
    tell application "System Events"
        set thePath to POSIX path of (container of (item 1 of input))
    end tell
    return thePath
end run

Can your Service receive more then one file or folder at a time? ... If yes, then you'll need to incorporate the code below.

The following example assumes multiple files or folders gets passed to the Service:

on run {input, parameters}
    set pathList to {}
    repeat with itemNum from 1 to count of input
        tell application "System Events"
            copy POSIX path of (container of (item itemNum of input)) to end of pathList
        end tell
    end repeat
    return pathList
end run

Reference: Getting the path of the parent folder of a file?

2
  • I didn’t realise that input was a list. Thanks for your solutions. I have turned the first one to a service to get the parent path, and modified the second to give me a list of full path names. Thanks. – Manngo Jan 20 '17 at 21:54
  • This doesn't work. Returns "Can't get container of "[selected file name]. – John Smith Aug 2 '17 at 7:07

You must log in to answer this question.

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