Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Getting the path of the parent folder of a file?

How can I get the parent folder/directory of a file? I cant't find any actions i Automator that will give me this.
Will I have to do this through AppleScript? If so, then how?

Anders

Original iBook, PM G3 266 mhz

Posted on May 23, 2008 6:10 AM

Reply
12 replies

May 23, 2008 6:58 AM in response to gumsie

Thank you guys for your suggestions but unfortunately they will not do the trick as I want to make a general applicable workflow. The parth can not be hardcoded in the workflow as different files and folders will be worked on each time the workflow is run.

The input to the workflow is a file or list of files. I want to be able to retrieve the path of the parent folder of these files at runtime.

May 23, 2008 1:54 PM in response to anilsen

Mine should work as a stand-alone applescript droplet (save it as an application) from Script Editor.
This code will work in an Automator "Run Applescript" Action after a "Get Selected Finder Items" action:
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

It returns an Applescript list of each path for all the selected finder items. I had to change (number of items of) to (count of) as it failed in the Automator workflow.

If your workflow will only process one item at a time, then this will work in the workflow:
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

Feb 2, 2011 9:02 AM in response to anilsen

I am not sure if this helps anyone, but I had some trouble finding this kind of script for getting folder names etc., so I made it myself.

I am a novice at this so it could probably be cleaned up quite a bit with using the posix path and / or quoted form of and whatnot, but I have not yet dropped it into an actual script yet.

Here it is:

tell application "Finder"

set FolderPath to (choose folder) -- sets file path to folder you select
set ParentFolder to container of FolderPath -- sets the parent folder of the folder you select
set Foldername to name of folder FolderPath -- sets the folder name as text

display dialog FolderPath as text
display dialog ParentFolder as text
display dialog Foldername as text


end tell

Hope this helps!

Getting the path of the parent folder of a file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.