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

时间:2017-02-06 15:27:00

标签: applescript

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

作为我之前(已解决)问题的后续问题Can Applescript be used to tell whether or not a directory (path) exists?

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

3 个答案:

答案 0 :(得分:1)

最简单的方法是使用shell,mkdir -p仅在文件夹不存在时创建该文件夹。

do shell script "mkdir -p  ~/Desktop/TestFolder"

但是有一点需要注意:如果路径中有空格字符,则需要用两个反斜杠替换每个空格,因为通常的quoted of不会扩展波形符。

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

答案 1 :(得分:0)

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

tell application "Finder"
    set f to POSIX file "/Users/username/Documents/new.mp3"
    if exists f then delete f
end tell

system attribute "HOME"已替换为/Users/username

set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3")
tell application "Finder" to if exists f then delete f

或使用pre-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

布朗:AppleScript set directory path in Finder

答案 2 :(得分:0)

如果您的问题仍然存在:

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

为了管理我的文件夹,我在相关情况下使用以下行:

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

set mkdirFolder to "mkdir -p " & desktopPath & "a/b/c/d/e/"

do shell script mkdirFolder

如果不存在则创建一个文件夹“ 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"

您可以在这里找到更多信息(Learn more about creating folder with "mkdir"