Get the directory of the input file (Applescript)
I'm confused. I've worked for an hour and tried maybe ten different forms set posixDirectory to POSIX path of (parent of (path to aFile) as string)
, but I can't figure out which is correct.
I am getting the full POSIX path (including filename) by doing
set posixFilePath to POSIX path of aFile
Now how do I get the POSIX path in a directory only? I am getting various errors depending on what I am doing ... cannot make an alias .. cannot get the parent alias ...
I would have thought that this should work, but it doesn't ... set posixDirectory to POSIX path of ((parent of aFile))
source to share
There are several ways to do this if you already have the original path.
From Posix path format
set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"
set textNumber1 to characters 1 thru -((offset of "/" in (reverse of items of thePath as string)) + 1) of thePath as string
or using a shell
set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"
set parentPath to do shell script "dirname " & quoted form of thePath
Result: "/Users/USERNAME/Documents/Test"
From the Posix file format
set thePath to "Macintosh HD:Users:USERNAME:Documents:Test:selectedTextColour.css"
set textNumber1 to characters 1 thru -((offset of ":" in (reverse of items of thePath as string)) + 1) of thePath as string
Result: "Macintosh HD:Users:USERNAME:Documents:Test"
source to share
This should be sufficient if you already have the posix file path.
to parentFolOfPxPath for pxPath
-- Assumes no superfluous slashes
set {tids, text item delimiters, i} to {text item delimiters, "/", ((pxPath ends with "/") as integer) + 1}
set {parFol, text item delimiters} to {text 1 thru text item -(i + 1) of pxPath, tids}
return parFol
end parentFolOfPxPath
source to share
Try a subroutine parentFolderOf(localPath)
. localPath
It may be alias
, alias as text
or posix path
. It returns a link that you can use with Finder
, for example:
set thisItem to parentFolderOf(path to fonts folder)
tell application "Finder" to reveal thisItem
Here's the script:
log (path to fonts folder)
log parentFolderOf(path to fonts folder)
log parentFolderOf(POSIX path of (path to fonts folder as text)) as text
log parentFolderOf("/System")
log parentFolderOf("/System") as text
# lets generate an error:
# parentFolderOf("ThisGeneratesAnError")
on parentFolderOf(localPath)
if (class of localPath is text) and (localPath contains ":") then
try
set localPath to localPath as alias
on error
error "File missing!"
end try
end if
# if its not an alias and not text containing ":" we assume its a posix path
if not (class of localPath is alias) then
try
set localPath to (localPath as POSIX file) as alias
on error
error "File missing!"
end try
end if
-- get the container:
set localPathContainerPath to ""
tell application "Finder"
try
set localPathContainerPath to (get container of localPath)
end try
end tell
return localPathContainerPath
end parentFolderOf
Addition The McUsr method seems like a good strategy because there is no need to use Finder
(which throws an error if the item is missing). Here's the version whose input works with alias
, (alias as text)
or posix path
:
log (parentFolder for (path to me))
log (parentFolder for "/Root/Sub1/Sub2/Sub3/testFile.txt")
log (parentFolder for "/Root/Sub1/Sub2/Sub3////")
log (parentFolder for "///Root/Sub1/Sub2/Sub3////")
log (posixPath for (path to me))
to parentFolder for aPath
set aPath to posixPath for aPath
set {tids, text item delimiters, i} to {text item delimiters, "/", ((aPath ends with "/") as integer) + 1}
set {pF, text item delimiters} to {text 1 thru text item -(i + 1) of aPath, tids}
return pF
end parentFolder
to posixPath for aPath
if class of aPath is not text then set aPath to aPath as text
if aPath contains ":" then set aPath to POSIX path of aPath
repeat while aPath starts with "//"
set aPath to (characters 2 thru -1 of aPath) as text
end repeat
repeat while aPath ends with "//"
set aPath to (characters 1 thru -2 of aPath) as text
end repeat
return aPath
end posixPath