2015-06-13 25 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