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
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
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
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
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.
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
This doesn’t return «scripta», 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.