2

有人可以指出为什么这部分 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
4

3 回答 3

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
于 2013-09-14T00:41:48.720 回答
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 操作中,那么它不应该有“打开的 dropItems”。在 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 路径名

于 2013-09-14T02:04:05.803 回答
1

不要将其转换inputFolder为 Unicode 文本。Finder 不知道如何处理后续语句中的文本。

set inputFolder to (container of first item of droppedItems)

然后,仅将 转换inputFolder为文本。

set outputFolder to (inputFolder as text) & "converted"
于 2013-09-14T00:13:07.927 回答