Applescript - 如果文件夹不存在,则创建一个文件夹

时间:2013-09-14 00:03:21

标签: applescript directory exists automator

有人可以指出为什么这一点Applescript不起作用?谢谢!

on open droppedItems
    tell application "Finder"
        set inputFolder to (container of first item of droppedItems) as Unicode text
        if (exists folder ("converted") of inputFolder) then
            set outputFolder to (inputFolder & "/converted/") as text
        else
            make new folder at inputFolder with properties {name:"converted"}
            set outputFolder to the result as text
        end if
    end tell
end

3 个答案:

答案 0 :(得分:2)

这是另一种方法: mkdir -p将创建一个新文件夹(如果不存在)。从手册页:

-p根据需要创建中间目录。如果未指定此选项,则每个操作数的完整路径前缀必须已经存在              存在。另一方面,如果指定了此选项,则如果作为操作数的目录已存在,则不会报告错误。

on open droppedItems
    repeat with anItem in droppedItems
        set outputFolder to POSIX path of ((anItem as text) & "::") & "converted"
        do shell script "mkdir -p " & quoted form of outputFolder
    end repeat
end open

修改

on open droppedItems
    repeat with anItem in droppedItems
        set outputFolder to POSIX path of ((anItem as text) & "::") & "converted"
        do shell script "mkdir -p " & quoted form of outputFolder

        --If you need to reference the folder by alias instead of posix path
        set outputFolderAlias to (POSIX file outputFolder) as alias
    end repeat
end open

答案 1 :(得分:2)

我得到了以下版本。我离开了“说”命令。使用say命令是一种很好的调试技术。

on open droppedItems
    say "on open"
    tell application "Finder"
        set inputFolder to (container of first item of droppedItems) as Unicode text
        set convertedFolderPath to inputFolder & "converted:"
        if (exists (folder convertedFolderPath)) then
            say "converted folder exists"
            set outputFolder to (inputFolder & "/converted/") as text
        else
            say "converted folder does not exist"
            make new folder at inputFolder with properties {name:"converted"}
            set outputFolder to the result as text
        end if
    end tell
    say "end open"
end open

--- ---编辑

哦,这是用“Automator”标签标记的。如果您的代码在“运行AppleScript”的Automator操作中,则它不应该具有“on open droppedItems”。在Automator中,脚本应如下所示:

on run {input, parameters}
    -- Enter your scripting here (without the "on open droppedItems" part)
    return input
end run

---编辑2 ---

行。我知道路径是部分HFS和部分POSIX。有趣的是,它在我的计算机上工作,既可以创建新文件夹,也可以检测文件夹已经存在,但这里的代码是固定的HFS路径,没有任何部分是POSIX pah:

on open droppedItems
    say "on open"
    tell application "Finder"
        set inputFolder to (container of first item of droppedItems) as Unicode text
        set convertedFolderPath to inputFolder & "converted:" ---- changed this ----
        if (exists (folder convertedFolderPath)) then
            say "converted folder exists"
            set outputFolder to convertedFolderPath
        else
            say "converted folder does not exist"
            make new folder at inputFolder with properties {name:"converted"}
            set outputFolder to the result as text
            say "created folder"
        end if
    end tell
    say "end open"
end open

Mac Pathnames

答案 2 :(得分:1)

不要将inputFolder转换为Unicode文本。 Finder不知道在后续陈述中如何处理文本。

set inputFolder to (container of first item of droppedItems)

之后,只需将inputFolder转换为文字。

set outputFolder to (inputFolder as text) & "converted"