获取输入文件的目录(Applescript)
Getting directory of input file (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))
使用Finder的容器命令:
set aFile to choose file
tell application "Finder" to set posixDirectory to POSIX path of ((container of aFile) as text)
尝试子例程 parentFolderOf(localPath)
。 localPath
可以是 alias
、alias as text
或 posix path
。它 returns 可以与 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
Addition McUsr 的方法似乎是一个很好的 strategy 因为不需要使用 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
当您已经有了文件的 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"
我很困惑 - 我在谷歌上搜索了一个小时,尝试了大概十种不同形式的 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))
使用Finder的容器命令:
set aFile to choose file
tell application "Finder" to set posixDirectory to POSIX path of ((container of aFile) as text)
尝试子例程 parentFolderOf(localPath)
。 localPath
可以是 alias
、alias as text
或 posix path
。它 returns 可以与 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
Addition McUsr 的方法似乎是一个很好的 strategy 因为不需要使用 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
当您已经有了文件的 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"