正在获取输入文件的目录(Applescript)

正在获取输入文件的目录(Applescript),applescript,Applescript,我很困惑——我已经在谷歌上搜索了一个小时,并尝试了大概十种不同形式的将posixDirectory设置为POSIX路径(作为字符串的(指向文件的路径)的父项),但我似乎没能找到正确的方法 我通过执行以下操作获得完整的POSIX路径(包括文件名) 将posixFilePath设置为文件的POSIX路径 现在,如何获得目录的POSIX路径??根据我的工作,我会遇到各种各样的错误。。。无法生成别名。。无法获取别名的父级 我想这应该行得通,但不是。。。 将posixDirectory设置为((文件的父级

我很困惑——我已经在谷歌上搜索了一个小时,并尝试了大概十种不同形式的
将posixDirectory设置为POSIX路径(作为字符串的(指向文件的路径)的父项)
,但我似乎没能找到正确的方法

我通过执行以下操作获得完整的POSIX路径(包括文件名)
将posixFilePath设置为文件的POSIX路径

现在,如何获得目录的POSIX路径??根据我的工作,我会遇到各种各样的错误。。。无法生成别名。。无法获取别名的父级

我想这应该行得通,但不是。。。
将posixDirectory设置为((文件的父级))的POSIX路径

使用Finder的容器命令:

set aFile to choose file

tell application "Finder" to set posixDirectory to POSIX path of ((container of aFile) as text)

尝试子例程
parentFolderOf(localPath)
<代码>本地路径可以是
别名
文本别名
posix路径
。它返回一个可与
Finder
一起使用的引用,如下所示:

set thisItem to parentFolderOf(path to fonts folder)
tell application "Finder" to reveal thisItem
下面是脚本:

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


此外,McUsr的方法似乎是一个很好的策略,因为不需要使用
Finder
(如果项目丢失,它将抛出一个错误)。这里是一个输入与
别名
(别名为文本)
posix路径
一起工作的版本:

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

当您已经拥有文件的posix路径时,这就足够了

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

如果您已经有了初始路径,那么有两种方法可以做到这一点

从Posix路径格式

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

或者使用shell

set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"


set parentPath to do shell script "dirname " & quoted form of thePath
结果:
“/Users/USERNAME/Documents/Test”


从Posix文件格式

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

结果:
“Macintosh HD:Users:USERNAME:Documents:Test”

@Max如果这解决了您的答案,您应该接受它,或者如果没有答案,请解释原因,我们可以尝试补救。