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

时间:2015-06-13 21:50:27

标签: applescript

我感到很困惑 - 我已经谷歌搜索了一个小时,并尝试了十种不同形式的set posixDirectory to POSIX path of (parent of (path to aFile) as string),但我似乎无法做到这一点。

我通过这样做获得完整的POSIX路径(包括文件名) set posixFilePath to POSIX path of aFile

现在,如何获取目录的POSIX路径?我根据自己的行为得到了各种错误......无法制作别名......无法获得别名的父级......

我认为这应该有效,但不是......     set posixDirectory to POSIX path of ((parent of aFile))

4 个答案:

答案 0 :(得分:9)

如果您已经拥有初始路径,有几种方法可以做到这一点。

来自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"

答案 1 :(得分:6)

使用Finder的容器命令:

set aFile to choose file

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

答案 2 :(得分:2)

当你已经拥有文件的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

答案 3 :(得分:1)

尝试子程序parentFolderOf(localPath)localPath可以是aliasalias as textposix path。它会返回一个可以与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(如果项目丢失会抛出错误) 。此处的输入适用于alias(alias as text)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