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

Max*_*Max 3 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))

mar*_*nte 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
Run Code Online (Sandbox Code Playgroud)

或使用shell

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


set parentPath to do shell script "dirname " & quoted form of thePath
Run Code Online (Sandbox Code Playgroud)

结果: "/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
Run Code Online (Sandbox Code Playgroud)

结果: "Macintosh HD:Users:USERNAME:Documents:Test"


Cra*_*ith 7

使用Finder 的容器命令:

set aFile to choose file

tell application "Finder" to set posixDirectory to POSIX path of ((container of aFile) as text)
Run Code Online (Sandbox Code Playgroud)