【问题标题】:AppleScript Move File(s) failing. What did I do wrong?AppleScript 移动文件失败。我做错了什么?
【发布时间】:2014-01-08 01:26:02
【问题描述】:

我正在编写我的第一个 AppleScript,但在最后一个命令中遇到了错误。基本上,脚本应该遍历给定文件夹(“/Volumes/Public2/Pictures/LINP/temp/”)中的所有 jpg,并根据文件名中的日期将它们全部移动到子文件夹中:

例如。 000DC5D1A54E(天井)_1_20130604084734_139988.jpg

其中 20130604 是日期 (2013-06-04)

我的脚本部分获取每个文件名并循环它们,创建必要的子文件夹效果很好。完全没有问题。当我尝试实际的“移动”时,我得到了系统事件错误。因此,首先,这是完整的脚本:

set workFolder to "/Volumes/Public2/Pictures/LINP/temp/"
log workFolder
set workFolder to (POSIX file workFolder) as alias
log workFolder


tell application "System Events"

    --get files to work on
    set filesToProcess to files of workFolder
    --log filesToProcess

    repeat with thisFile in filesToProcess

        log thisFile

        set {fileName, fileExt} to {name, name extension} of thisFile
        log fileName
        log fileExt

        if (fileExt is in "(*jpg*)" and ((length of fileName) is greater than 10)) then
            --get name of file without extension
            set rootName to text 1 thru -((length of fileExt) + 2) of fileName
            log rootName

            --break apart the file name to get to the imortant stuff
            set AppleScript's text item delimiters to "_"

            --store the camera name
            set cameraName to text item 1 of rootName
            log cameraName

            --store the full date
            set photoDate to text item 3 of rootName
            log photoDate

            --cut out the time information
            set photoDate to text 1 thru 8 of photoDate
            log photoDate

            --break out the year
            set photoYear to text 1 thru 4 of photoDate
            log photoYear

            --break out the month
            set photoMonth to text 5 thru 6 of photoDate
            log photoMonth

            --break out the day
            set photoDay to text 7 thru 8 of photoDate
            log photoDay

            --combine parts into a new name of the form "YYYY-MM-DD"
            set photoFolder to photoYear & "-" & photoMonth & "-" & photoDay
            log photoFolder

            --make sure a correctly named folder exists
            set targetFolder to my checkForFolder({parentFolder:(workFolder as text), folderName:photoFolder}) as text
            log "targetFolder: " & targetFolder
            log thisFile

            log "targetFolder Class: " & class of targetFolder
            log "thisFile Class: " & class of thisFile


            set finalTarget1 to (POSIX file targetFolder) as alias as string
            log "finalTarget1: " & finalTarget1
            log "finalTarget1 Class: " & class of finalTarget1

            --Here's where the error pops up. I've tried both HFS and POSIX formats for the target folder, but the issue seems to be with the file
            --Yes, the files definitely exist (I've confirmed this). I show exactly what the values are and error messages I'm getting at the end of this post
            move thisFile to the folder finalTarget1
            move thisFile to the folder targetFolder

        end if
    end repeat
end tell

to checkForFolder({parentFolder:fParent, folderName:fName})
    --find or create a folder
    tell application "System Events"
        --set fName to POSIX file of fName as alias
        --set fParent to POSIX file of fParent as alias

        if not (exists POSIX path of (folder fName of folder fParent)) then
            set output to POSIX path of (make new folder at end of folder fParent with properties {name:fName})
        else
            set output to (POSIX path of (folder fName of folder fParent))
        end if
    end tell

    --returns a POSIX path
    return output
end checkForFolder

在对 Apple 论坛、Stack Overflow 和 Google 预言机进行了数小时的研究后,我意识到上下文很重要,因此我试图提供尽可能多的信息。在这里,我打印(记录)了 thisFile、targetFolder 和 finalTarget1 的内容和 Class 类型:

目标文件夹:/Volumes/Public2/Pictures/LINP/temp/2013-06-04

(targetFolder 类:文本)

这个文件

(*file Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg*)

(thisFile 类:文件)

(finalTarget1: Public2:Pictures:LINP:temp:2013-06-04:)

(finalTarget1 类:文本)

以下是两次移动尝试中的每一个都显示的错误:

将此文件移动到文件夹 finalTarget1

= 将文件“Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg”移动到文件夹“Public2:Pictures:LINP:temp:2013-06-04:”

错误“系统事件出错:无法获取文件\”Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg\”。”编号-1728来自文件“Public2:Pictures:LINP:temp :000DC5D1A54E(天井)_1_20130604084734_139988.jpg"

将此文件移动到文件夹 targetFolder

= 将文件“Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg”移动到文件夹“/Volumes/Public2/Pictures/LINP/temp/2013-06-04”

错误“系统事件出错:无法获取文件\”Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg\”。”编号-1728来自文件“Public2:Pictures:LINP:temp :000DC5D1A54E(天井)_1_20130604084734_139988.jpg"

最后,是的,Public2 已安装,我知道它是可访问的,因为我正在取回完整的文件夹内容并适当地创建每个子文件夹。只是最后一次“移动”操作失败了,我敢肯定这是我无知的产物。

谁能帮助确定原因以及我该如何解决?

【问题讨论】:

    标签: file applescript move


    【解决方案1】:

    我拿了你的脚本并快速制作了一个本地版本来测试,临时文件夹中有两个 jpeg。到目前为止,这就是我遇到的。 修复 #1:

    move thisFile to the folder finalTarget1
    

    需要改成

    move thisFile to finalTarget1
    

    但是有一个错误

    move thisFile to the folder targetFolder
    

    因为脚本试图移动它已经移动的文件。不确定该行是否用于测试目的。也许你可以澄清一下。如果我注释掉第二行,它似乎可以工作。

    【讨论】:

    • CRGreen:感谢您的建议。不幸的是,当我将该行更改为“将 thisFile 移动到 finalTarget1”时,它会出错:将文件“Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg”移动到“Public2:Pictures:LINP:temp:2013- 06-04:"--> 错误号 -1700 从“Public2:Pictures:LINP:temp:2013-06-04:”到位置说明符结果:错误“系统事件出错:无法制作 \"Public2:图片:LINP:temp:2013-06-04:\" 转换为类型位置说明符。"编号 -1700 从“Public2:Pictures:LINP:temp:2013-06-04:”到位置说明符
    • 至于这两个动作,我尝试了两个稍微不同的动作,看看一个是否可行。我在帖子中提供了每次尝试的输出,希望由此产生的错误消息有助于确定问题。
    • 嗨@user3171341。我没有看到那个错误。当我运行脚本并将第一行修改为“将 thisFile 移动到 finalTarget1”时,它可以工作。您使用的是什么版本的操作系统?将第二行注释掉,但看看是否适用于第一行:move thisFile to (my finalTarget1 as alias) 如果这行得通,我将编辑我的答案。
    • 另外(我知道这看起来很奇怪),试试这个:move alias (thisFile as alias as text) to (finalTarget1 as alias)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    相关资源
    最近更新 更多