Getting the path of the parent folder of …
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
13 replies
Sign in or join Beachball Help to reply.
- 0
Barney-15E ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
on open filefolderlist
repeat with itemNum from 1 to number of items in filefolderlist
tell application "System Events"
set thePath to POSIX path of (container of (item itemNum of filefolderlist))
end tell
display dialog thePath
end repeat
end open - 0
roam ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hello Anders,
I don't know if this helps but If you have the file showing in Finder, you can set Finder to show the entire path of the file, including the parent folder of course, by selecting under the Finder menu, View, select Show Path Bar.
- 0
AJ ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You can hold down the Apple Key and click on the folder name at the top of the window.
It will display the path to where you are.
From there, you can select any of the folders and move to that folder.
you can also change your current view (from icon or list) to Column.
- 0
gumsie ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Or.....
........... if you have a file/folder that you want the path of, keep a terminal window open and drag the item onto it.
A much easier way and it means you can then copy and paste the text for long path names that are buried many folders deep. - 0
anilsen ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
- 0
[icarus] ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
maybe this link will be of some help:
macosxhints.com - a simple way to copy the path of a file or folder
⌘ + ⌥ + space to open a spotlight window then drag and drop a file or folder onto the results window... now the path appears in the search field.
there are two comments on the hint concerning an automator-workflow and an applescript... good luck.
- 0
spazek ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
See this thread:
http://discussions.apple.com/message.jspa?messageID=7249042#7249042
- 0
Barney-15E ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
on open filefolderlist
repeat with itemNum from 1 to number of items in filefolderlist
tell application "System Events"
set thePath to POSIX path of (container of (item itemNum of filefolderlist))
end tell
display dialog thePath
end repeat
end open - 0
anilsen ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Thanks for your answer Barney-15E
The script runs but the no dialog is displayed though. Also I've inserted
return thePath
just before the end open handler but no result is returned.
Another guy posted a solution in my other post, but I would find it interesting to get your to work too.
Regards
Anders
- 0
Barney-15E ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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 runIt 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 - 0
anilsen ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Thanks for your help. Nice script. The problem is solved.
I used to do some applescripting years back (before that a lot of hyperScript/hyperCard) but have forgotten most about it. I think I might take it up again as it seem pretty neccesary because of limitations and awkward behaviour in Automator.
Regards
Anders
- 0
Koby ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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 textdisplay dialog FolderPath as text
display dialog ParentFolder as text
display dialog Foldername as textend tell
Hope this helps!
- 0
Mr. Quinn ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Try this bash script.
It takes each file path send to it and outputs the parent folder of each one.
for f in "$@"
do
echo "$(dirname "$f")"
done