2015-06-13 26 views
2

我很困惑 - 我一直在谷歌搜索一个小时,并尝试了大概十种不同形式的set posixDirectory to POSIX path of (parent of (path to aFile) as string),但我似乎无法做到正确。获取输入文件目录(Applescript)

我做 set posixFilePath to POSIX path of aFile

现在,我怎么得到的只是目录的POSIX路径获取完整的POSIX路径(包括文件名)?我收到的各种错误取决于什么,我......不能让别名..不能得到别名的父...

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

回答

7

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

从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 

,或者使用壳

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"

+0

@Max如果这解决了你的答案,你应该接受它,如果答案不这样做,请解释原因,我们可以尝试和纠正它。 – markhunte

5

使用Finder中的容器命令:

set aFile to choose file 

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

尝试子程序parentFolderOf(localPath)localPath可以是alias,alias 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 
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