【问题标题】:Applescript error: Can’t make {alias "G-DRIVE:video:", "2005:"} into type integerApplescript 错误:无法将 {alias "G-DRIVE:video:", "2005:"} 转换为整数类型
【发布时间】:2019-10-21 03:57:05
【问题描述】:

我正在编写一个 AppleScript 来处理一些 MTS 文件。该脚本递归地遍历顶级文件夹“Volumes:G-Drive:video”的子文件夹。子文件夹排列在年份文件夹中,从 2005 年的名为“2005”的文件夹开始。脚本尝试迭代的第一个文件夹“2005”发生运行时错误。我怀疑这与我处理别名和文本的方式有关,但在这一点上我很难解决如何解决它。运行时的错误是这样的: 无法将 {alias "G-DRIVE:video:", "2005:"} 转换为整数类型。

我不知道在我的代码中,整数类型在哪里发挥作用,从而导致此错误。脚本编辑器并没有将我指向代码中运行时错误的位置,所以我也不知道如何找出是哪一行导致了这种情况。

set folderName to "Volumes:G-Drive:video" as alias

my processFolder("", folderName)

on processFolder(root, folderNameToProcess)
    tell application "Finder"
        set theItems to every file of folder (root & folderNameToProcess)
        repeat with theFile in theItems
            set Nm to name of theFile as text
            set Ex to name extension of theFile
            if Nm does not contain "-c" and Ex is "MTS" then
                set NmMinusExt to my remove_extension(Nm)
                set logMsg to "Deleting " & Nm
                my logThis(logMsg)
                tell application "Finder" to delete theFile
            end if
        end repeat
        set theItems to every file of folder (root & folderNameToProcess)
        repeat with theFile in theItems
            set Nm to name of theFile as text
            set Ex to name extension of theFile
            --tell (info for theFile) to set {Nm, Ex} to {name, name extension}
            if Nm contains "-c" and Ex is "MTS" then
                set NmMinusExt to my remove_extension(Nm)
                set shortNm to my remove_lastTwoCharacters(NmMinusExt)
                set name of theFile to shortNm & ".MTS" as text
                set logMsg to "Renaming " & Nm
                my logThis(logMsg)
                --set lastTwoLetters to characters (((length of Nm) - 2) as number) thru (((length of Nm) - 0) as number) of (Nm as text)
                --if lastTwoLetters is "-c" then
                --display notification lastTwoLetters
                --end if
            end if
        end repeat
        set theFolders to name of folders of folder (root & folderNameToProcess)
        repeat with theFolder in theFolders
            copy theFolder as string to TheFolderName
            display notification "found folder named: " & TheFolderName
            set firstChar to (text 1 thru 1 of TheFolderName)
            if firstChar is not "." then
                --display dialog (folderNameToProcess & TheFolderName & ":")
                try
                    my processFolder(folderNameToProcess, TheFolderName & ":")
                on error errStr number errorNumber
                    display dialog errStr
                end try
            end if
        end repeat
    end tell
    return
end processFolder

on remove_extension(this_name)
    if this_name contains "." then
        set this_name to ¬
            (the reverse of every character of this_name) as string
        set x to the offset of "." in this_name
        set this_name to (text (x + 1) thru -1 of this_name)
        set this_name to (the reverse of every character of this_name) as string
    end if
    return this_name
end remove_extension

on remove_lastTwoCharacters(this_name)
    set this_name to ¬
        (the reverse of every character of this_name) as string
    set this_name to (text 3 thru -1 of this_name)
    set this_name to (the reverse of every character of this_name) as string
    return this_name
end remove_lastTwoCharacters

脚本编辑器的事件窗格产生以下跟踪:

tell application "Finder"
    get every file of folder "G-DRIVE:video:"
    get every file of folder "G-DRIVE:video:"
    get name of every folder of folder "G-DRIVE:video:"
    display notification "found folder named: 2005"
end tell
tell application "Script Editor"
    display notification "found folder named: 2005"
end tell
tell application "Finder"
    get every file of folder {alias "G-DRIVE:video:", "2005:"}
    display dialog "Finder got an error: Can’t make {alias \"G-DRIVE:video:\", \"2005:\"} into type integer."

【问题讨论】:

    标签: applescript


    【解决方案1】:

    folderName 是一个别名。连接别名和字符串的尝试会创建一个错误指示的列表。

    解决方案是仅使用 (HFS) 字符串路径。

    替换

    set folderName to "Volumes:G-Drive:video" as alias
    

    set folderName to "G-Drive:video:"
    

    尾随冒号对于能够添加路径组件至关重要

    【讨论】:

    • 当我这样做时,我得到一个不同的错误。它甚至无法读取顶级文件夹。这是“事件”选项卡中的跟踪:告诉应用程序“Finder”获取文件夹“Volumes:G-Drive:video:”的每个文件结果:错误“Finder 出现错误:无法获取文件夹 \”Volumes:G-Drive :视频:\”。”文件夹“Volumes:G-Drive:video:”中的编号 -1728
    • 我的错。删除前缀Volumes:HFS 字符串路径总是以磁盘名称开头。
    • 做到了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2021-02-14
    • 2020-08-11
    • 2019-02-26
    相关资源
    最近更新 更多