所谓的重复问题解释了如何删除文件,但我需要创建一个(或多个(不存在的目录一个完全不同的任务!
作为我之前(已解决的(问题的后续 Applescript 可以用来判断目录(路径(是否存在吗?
我现在需要知道如何在路径中创建尚不存在的任何目录?
最简单的方法是使用 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
在获取文件对象的路径之前添加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
或者使用 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:AppleScript 在 Finder 中设置目录路径
如果您的问题仍然是:
"创建一个(或多个(不存在的目录 一个完全不同的任务?">
为了管理我的文件夹,我在相关情况下使用这些行:
创建从"a"到"e"的所有文件夹。如果文件夹"a"已经存在,则从"b"到"e"。等。。。
set mkdirFolder to "mkdir -p " & desktopPath & "a/b/c/d/e/"
do shell script mkdirFolder
创建一个文件夹">a"(如果不存在(,并在其顶层创建文件夹"b to 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"
您可以在此处找到更多信息(了解有关使用"mkdir"创建文件夹的更多信息(