【问题标题】:Accessing folders in AppleScript在 AppleScript 中访问文件夹
【发布时间】:2013-09-20 00:17:51
【问题描述】:

抱歉,这里是 AppleScripting 的初学者。

我正在尝试做一件非常简单的事情,将文件从一个文件夹移动到另一个文件夹。

tell application "Finder"

    set this_folder to "Users:chris.nicol:Movies:SmartConverter:"

    set this_list to every file of this_folder
    repeat with i in this_list
        --if i does not start with "x" then
        move i to "users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
        --end if
    end repeat
end tell

但是我不断收到错误消息:

我在文件夹上尝试了不同的命令(count、parentContainer 等),但我得到了相同类型的错误。我尝试了不同的文件夹格式...

  • Users/chris.nicol/Movies/SmartConverter/
  • Macintosh HD:Users:chris.nicol:Movies:SmartConverter
  • 等

关于我做错了什么有什么想法吗?

提前致谢, 克里斯

【问题讨论】:

    标签: macos applescript directory


    【解决方案1】:

    简单提示...如果您想找到正确的路径,请尝试使用此方法并在结果字段中查找正确的路径。您会看到硬盘驱动器的名称 Macintosh HD 是必需的。请注意,如果您想要文件的路径,也可以使用“选择文件”。

    (choose folder) as text
    

    接下来,您将看到的路径是一个字符串。 Applescript 将其视为字符串而不是文件或文件夹的路径。因此,当您想将字符串用作路径时,您必须在其前面适当放置“文件”或“文件夹”一词,以使 AppleScript 正确使用它。因此,您的脚本应如下所示。请注意,移动命令可以处理文件列表,因此不需要重复循环。

    set this_folder to "Macintosh HD:Users:chris.nicol:Movies:SmartConverter:"
    tell application "Finder"
        set this_list to every file of folder this_folder
        move this_list to folder "Macintosh HD:Users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
    end tell
    

    【讨论】:

    • 创建 this_list 后,您无需在移动命令中包含文件夹。您可以简单地将 this_list 移动到“Macintosh HD:Users:chris.nicol:music:itunes:itunes media:automatically add to itunes:”,就像我在回复中所做的那样。
    • 是的。然而,Finder 字典中定义的正确技术是在 move 命令中使用“位置说明符”。您可以使用“文件”或“文件夹”来创建说明符。此外,说明符将在字符串仅在有限情况下起作用的任何地方起作用。所以对某人来说最好的习惯就是按照我展示的方式去做,不要担心其他东西也能起作用的特殊情况。 posix 路径也是如此。他们在某些地方工作,但为什么要记住他们在哪里工作和不工作。只需使用文件/文件夹说明符,您将永远是正确的。
    【解决方案2】:

    试试:

    set thisFolder to (path to movies folder as text) & "SmartConverter"
    set thatFolder to (path to music folder as text) & "itunes:itunes media:automatically add to itunes"
    
    tell application "Finder" to move files of folder thisFolder to thatFolder
    

    【讨论】:

      【解决方案3】:

      您必须在其中一个文件夹中有一些不可见或不可复制的文件。要么在 set this_folder 行的末尾添加类似 without invisibles 的内容,要么完全摆脱循环并简单地调用

      move files of entire contents of this_folder to (the Add Automatically folder)
      

      【讨论】:

        猜你喜欢
        • 2013-08-17
        • 1970-01-01
        • 2012-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        • 2012-09-23
        相关资源
        最近更新 更多