如何在Applescript中创建目录路径?

如何在Applescript中创建目录路径?,applescript,Applescript,假设的重复问题解释了如何删除文件,但我需要创建一个(或多个)不存在的目录一个完全不同的任务 作为我先前(已解决)问题的后续 我现在需要知道如何在路径中创建任何不存在的目录?在获取文件对象的路径之前添加POSIX file: tell application "Finder" set f to POSIX file "/Users/username/Documents/new.mp3" if exists f then delete f end tell 系统属性“HOME”替换

假设的重复问题解释了如何删除文件,但我需要创建一个(或多个)不存在的目录一个完全不同的任务

作为我先前(已解决)问题的后续


我现在需要知道如何在路径中创建任何不存在的目录?

在获取文件对象的路径之前添加
POSIX file

tell application "Finder"
    set f to POSIX file "/Users/username/Documents/new.mp3"
    if exists f then delete f
end tell
系统属性“HOME”
替换为
/Users/username

set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3")
tell application "Finder" to if exists f then delete f
或者使用OS X之前的路径格式:

tell application "Finder"
    set f to "Macintosh HD:Users:username:Documents:new.mp3"
    -- set f to (path to documents folder as text) & "new.mp3"
    if exists f then delete f
end tell

Bron:

最简单的方法是使用shell,
mkdir-p
仅在不存在的情况下创建文件夹

do shell script "mkdir -p  ~/Desktop/TestFolder"
但是有一个警告:如果路径中有空格字符,则需要用两个反斜杠替换每个空格,因为通常引用的不会展开平铺

do shell script "mkdir -p  ~/Desktop/Test\\ Folder"
或者

set thePath to "~/Desktop/Test Folder ABC"
if thePath starts with "~" then
    set quotedPath to text 1 thru 2 of thePath & quoted form of (text 3 thru -1 of thePath)
else
    set quotedPath to quoted form of thePath
end if

do shell script "mkdir -p  " & quotedPath

如果您的问题仍然是:

“创建一个(或多个)不存在的目录是完全不同的任务吗?”

为了管理我的文件夹,我在相关案例中使用这些行:

创建从“a”到“e”的所有文件夹。如果文件夹“a”已经存在,则从“b”到“e”。 等等

如果不存在,则创建文件夹“a”,并在其顶层创建文件夹“b到e”

set mkdirFolder to "mkdir -p " & desktopPath & "a/{b,c,d,e}/"
        do shell script mkdirFolder
使用名称的一部分创建文件夹

-- (Note the single quotes round the space to mark it as part of the name.)
    set mkdirFolder to "mkdir -p " & desktopPath & "a/Chapter' '{1,2,3,4}/"

do shell script mkdirFolder

result--> Folders "Chapter 1", "Chapter 2", "Chapter 3", and "Chapter 4" are created in folder "a"

您可以在此处找到更多信息()

此处可能重复的可能重复您可以将desktopPath视为变量,但您可以使用斜杠放置任何其他路径。如果我错了,请纠正我。
-- (Note the single quotes round the space to mark it as part of the name.)
    set mkdirFolder to "mkdir -p " & desktopPath & "a/Chapter' '{1,2,3,4}/"

do shell script mkdirFolder

result--> Folders "Chapter 1", "Chapter 2", "Chapter 3", and "Chapter 4" are created in folder "a"