I’ve been asking a lot of questions, and getting a lot of help, recently in this forum. So I thought I’d try to give back a little.
Many of you may already know how to do this, but if you’re like me it may not be so obvious. I continue to struggle with the multiple forms of file/folder paths in AppleScript.
One great thing about ASObjC is almost always it use, or can use, a POSIX path as a parameter. Sometimes, you might like to operate on either a folder that is selected in the Finder, OR, if none are selected, use the target of the Finder window. That’s exactly what this script does.
AppleScript To Get Path to Finder Folder or Window Target
tell application "Finder" to set fItemList to (selection as alias list) if ((count of fItemList) = 0 or (kind of (info for (item 1 of fItemList)) ≠ "folder")) then tell application "Finder" to set fItemPath to POSIX path of (target of front window as text) else set fItemPath to POSIX path of (item 1 of fItemList) end if
BTW, yes I know that info for has been deprecated. But that was years ago, and it still seems to be alive and well. Until it dies, I will continue to use it.
If you prefer not to use it, please feel free to post a version using alternate commands.