【问题标题】:Applescript - creating a folder if it doesn't existApplescript - 如果文件夹不存在则创建一个文件夹
【发布时间】:2013-09-18 18:27:23
【问题描述】:

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

【问题讨论】:

  • 脚本编辑器的日志中有消息吗?
  • 我在日志中没有看到任何消息。

标签: applescript directory exists automator


【解决方案1】:

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

set inputFolder to (container of first item of droppedItems)

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

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

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案3】:

      我得到了以下版本的工作。我离开了“说”命令。使用 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 Pathnames

      【讨论】:

      • 您将 hfs 路径与 posix 路径混合在一起。在您的脚本中,outPut 文件夹将等于 Mac OS X:Users:Keydell:Desktop:/converted/。
      • 如果转换后的文件夹不存在,您的版本可以工作 - 但如果它已经存在,脚本将失败。
      • 它失败了,因为他将 posix 路径与 hfs 路径混合在一起。
      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 2011-02-19
      • 2013-01-17
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多