Return name of parent folder

Hi All,

Anybody here knows how to make the script below return the name of parent folder and not the currently selected folder?

return expand("")

on expand(separator)
	
	tell application "Finder"
		set fileNames to {}
		set theItems to selection
		repeat with itemRef in theItems
			set end of fileNames to name of itemRef
		end repeat
	end tell
	
end expand

Thanks in advance!

Greetz,
vanWoods

If your selection is indeed some folder(s) as you sad then following should work:


return expand("")

on expand(separator)
	
	tell application "Finder"
		set fileNames to {}
		set theItems to selection
		repeat with itemRef in theItems
			set end of fileNames to name of itemRef
		end repeat
		return name of parent of (item 1 of theItems) -- ADDED
	end tell
	
end expand

If your selection is the elements of some folder you consider current, then you need one more extra level:


return expand("")

on expand(separator)
	
	tell application "Finder"
		set fileNames to {}
		set theItems to selection
		repeat with itemRef in theItems
			set end of fileNames to name of itemRef
		end repeat
		return name of parent of parent of (item 1 of theItems) -- ADDED
	end tell
	
end expand

Thanks KniazidisR for your quick reply!

Only this is returning the selected folder name and not the parent folder. Do you know what I could be missing?

So, you selected not the folder(s) but elements of some folder you consider current. It is 2nd script version in post #2.

Is this what you mean?


return expand("")

on expand(separator)
	set folderNames to {}
	tell application "Finder"
		set theItems to selection
		repeat with itemRef in theItems
			set end of folderNames to name of container of itemRef
		end repeat
	end tell
	return folderNames
end expand

FWIW, regarding the use of “parent” by KniazidisR and “container” by Ed–both of which seem to work–the following is from Nigel:

https://macscripter.net/viewtopic.php?id=42711

Apparently, parent also works with system events, and it’s not in that dictionary either:


tell application "System Events"
		set fileNames to {}
		repeat with itemRef in theitems
			set end of fileNames to name of parent of itemRef
		end repeat
		return fileNames
end tell

When I have a choice between a documented feature and an undocumented feature I prefer to use what’s documented. Undocumented features have a way of changing or going away without warning, and I hate having to go back and modify broken scripts.

From Script Editor’s event log, after running that last script:

tell application "Script Editor"
	choose file
		--> alias "Anacreon:Users:xxx:Desktop:QSA cleaner.rtf"
end tell
tell application "Finder"
	get file "Anacreon:Users:xxx:Desktop:QSA cleaner.rtf"
		--> document file "QSA cleaner.rtf" of folder "Desktop" of folder "xxx" of folder "Users" of startup disk
	get name of parent of document file "QSA cleaner.rtf" of folder "Desktop" of folder "xxx" of folder "Users" of startup disk
		--> "Desktop"
end tell
tell application "Script Editor"
	display dialog "Desktop"
		--> {button returned:"OK"}
end tell

So it is Finder getting the parent…

I guess, my explanation of the phenomenon is not entirely accurate. There are guys here who are more experienced than me. Indeed, some way interpretator redirects getting the parent to the Finder in the example 2.

I think if that were the case, then both of these would work outside Finder or System Events tell blocks:

parent of alias "myFile"

and

parent of file "myFile"

Both are object model objects with parents and children recognized by appleScript.

Thank you estockly!

This also works but it goes two levels up instead of one. Can’t set it to set the parent folder?

Thanks KniazidisR!

Good to know that I can also level up two folders. It can be useful in some cases as some of my colleagues sometimes put folder in folder.

OK, is this what you mean?


return expand("")

on expand(separator)
	set itemNames to {}
	tell application "Finder"
		set theItems to selection
		repeat with itemRef in theItems
			set end of itemNames to name of itemRef
		end repeat
	end tell
	return itemNames
end expand

Thanks but this version only sets the name of the selected item.

I used a previous script of you now and that works fine. Adding one or more containers to the script will level it up as far as needed.

return expand("")

on expand(separator)
	set folderNames to {}
	tell application "Finder"
		set theItems to selection
		repeat with itemRef in theItems
			set end of folderNames to name of container of container of itemRef
		end repeat
	end tell
	return folderNames
end expand

Thanks everybody for taking the time to help out, it works as needed now!

Have a good weekend!

This doesn’t return «script a», it returns «script», which is as expected. Although b is instantiated inside a, it inherits from the top-level script object, as they all do unless the parent property is set to specifically designate otherwise.

I deleted my wrong post so as not to mislead others. For the correct amendment and observation - thanks.