Applescript:如果不存在,则创建新的文件夹结构

时间:2012-02-24 19:32:38

标签: macos applescript

我一直在搜索留言板,试图弄清楚如何执行这个脚本。本质上,目标是能够在文件夹内运行此脚本,如果某些文件夹不存在,则会创建这些文件夹。如果它们确实存在,则没有任何反应。以下是我到目前为止拼凑的内容:

property archivesFolder : "Archives"

property imagesFolder : "Images"

property proofreadFolder : "Proofreading"

property proofFolder : "Proofs"

property sourceFolder : "Source"

try

tell application "Finder" to set theLocation to (folder of the front window as alias)

end try

tell application "Finder"

    if (exists folder archivesFolder) then
        (* do nothing *)
    else
        make new folder at theLocation with properties {name:archivesFolder}
    end if

    if (exists folder imagesFolder) then
        (* do nothing *)
    else
        make new folder at theLocation with properties {name:imagesFolder}
    end if

    if (exists folder proofreadFolder) then
        (* do nothing *)
    else
        make new folder at theLocation with properties {name:proofreadFolder}
    end if

    if (exists folder proofFolder) then
        (* do nothing *)
    else
        make new folder at theLocation with properties {name:proofFolder}
    end if

    if (exists folder sourceFolder) then
        (* do nothing *)
    else
        make new folder at theLocation with properties {name:sourceFolder}
    end if

end tell

我做错了什么? (原谅我的n00b代码格式化,在工作中无法弄清楚如何创建代码块)另外,这可能不仅仅在前窗上,而是在刚刚选择的文件夹上?给予的任何帮助都很棒。

4 个答案:

答案 0 :(得分:4)

您还可以将shell脚本与mkdir一起使用,因为如果文件夹已存在,创建中间文件夹的选项不会出错。

#  define a list of folders - items will need to be quoted if they contain spaces, etc.
property theFolders : {"Archives", "Images", "ProofReading", "Proofs", "Source"} -- can also nest, e.g. "Inside/One"

try
    tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
    set targetFolder to (choose folder)
end try

# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}

do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders

答案 1 :(得分:3)

我建议两个选项(运行脚本):

选项1 :获取该代码(假设它符合您的计划),并将其另存为应用程序(使用脚本编辑器)。

然后,只需将该应用程序拖动到窗口工具栏(您需要让工具栏可见)。为此,请在拖动时按住命令键。

选项2 :使用管家:http://manytricks.com/butler/ (有一个免费版本,我不知道你的OSX版本。)

它允许您为applescript脚本定义系统范围的快捷键。

创建智能项目(applescript);将代码粘贴到那里,并在脚本的名称中添加快捷键:示例:create folder here ⇧⌥⌘N

修改

根据您的评论,我了解您的问题,我可以告诉您,您错过了路径(当前文件夹,在您的情况下theLocation

因此,在if (exists folder archivesFolder) then的每个案例中,您都需要像of theLocation这样添加if not (exists folder archivesFolder of theLocation) thenproperty archivesFolder : "Archives" property imagesFolder : "Images" property proofreadFolder : "Proofreading" property proofFolder : "Proofs" property sourceFolder : "Source" try tell application "Finder" to set theLocation to (folder of the front window as alias) end try tell application "Finder" if not (exists folder archivesFolder of theLocation) then make new folder at theLocation with properties {name:archivesFolder} end if if not (exists folder imagesFolder of theLocation) then make new folder at theLocation with properties {name:imagesFolder} end if if not (exists folder proofreadFolder of theLocation) then make new folder at theLocation with properties {name:proofreadFolder} end if if not (exists folder proofFolder of theLocation) then make new folder at theLocation with properties {name:proofFolder} end if if not (exists folder sourceFolder of theLocation) then make new folder at theLocation with properties {name:sourceFolder} end if end tell

最后,如果文件夹存在,知道你不会做任何事情,只需测试错误的情况。

我测试了这段代码,并在此发布:

{{1}}

答案 2 :(得分:0)

试试这个:

tell application "Finder"
set theLocation to (target of front window) as string
set folderNames to {"archivesFolder", "imagesFolder", "proofreadFolder", "proofFolder", "sourceFolder"}
repeat with theFolder in folderNames
    try
        make new folder at theLocation with properties {name:"" & theFolder & ""}
    end try
    end repeat
end tell

答案 3 :(得分:0)

这是我根据服务日期将一堆医疗文件分类到子文件夹中的脚本(解释脚本中的“文本5到14”;文件根据模式命名,以便脚本可以提取文件名中的服务日期)。我只是将make文件夹指令放在try块中,而不是检查文件夹是否已经存在;如果该文件夹已存在,则“尝试”失败,但在假设该文件夹已存在的情况下脚本继续运行。使用'text'项而不是'character'将提取的字符串作为单个字符串返回,而不是作为必须转换回字符串的字符数组。

tell application "Finder"
set theDirectory to "Internal Disk:Users:steven:Documents:Vondalee:Medical"
set theFiles to every file in folder theDirectory whose name extension is "pdf"
repeat with eachFile in theFiles
    set theFileName to the name of eachFile
    set theFolder to text 5 thru 14 of theFileName
    try
        make new folder at theDirectory with properties {name:theFolder}
    end try
    move eachFile to folder theFolder of folder theDirectory
end repeat
end tell