Apple 脚本处理程序在 Tell 语句中不起作用

Apple 脚本处理程序在 Tell 语句中不起作用

嗨,我正在编写一个脚本,它将把所选文件/文件夹的格式从 xxxyearxxx 重命名为 xxxyear。我是 Apple 脚本的新手,做过很多 autoit/c++ 工作。

我无法让我的处理程序在 tell 语句中工作。我尝试了所有方法(“my”、“of me”),但处理程序出现错误。在 tell 语句之外调用时,处理程序工作正常:

使用“my”,脚本无法编译,并且出现以下错误:语法错误:命令名不能跟在“my”后面。

使用“of me”此脚本可以编译并运行,但我收到以下错误:“Finder 收到错误:splittext 缺少使用参数。”编号 -1701 来自 «class by »

任何帮助解决的问题都会非常有帮助。脚本如下:

`

set MaxYear to 2018
set MinYear to 1990
-- return splittext {"abc2015def", 2018, 1990}


tell application "Finder"
set allfiles to every item of (choose folder with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
-- set allfile to selections

repeat with x in allfiles
    if kind of x is "Folder" then
        -- if xtype is "Folder" then
        set cname to name of x
        set newname to splittext {cname, MaxYear, MinYear} of me
        if newname then
            set name of x to newname
        end if
    end if
end repeat
end tell


on splittext {theText, MaxYear, MinYear}
set dyear to MaxYear
repeat until dyear < MinYear
    set AppleScript's text item delimiters to dyear
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to ""
    if (count of theTextItems) > 1 then
        return the first item of theTextItems & dyear as string
    end if
    set dyear to dyear - 1
end repeat
return false
end splittext

答案1

该代码有效,但是......

您的处理程序:

splittext {theText, MaxYear, MinYear}

应该 :

splitText(theText, MaxYear, MinYear}

请注意,使用括号代替花括号(还有驼峰式大小写)。

您应该始终在处理程序中返回一个字符串:

return "" -- return false

在上面的重复循环中:

if newname is not "" then -- if newname then

还有另一个陷阱:x 的类型。它是一个本地字符串,即“Folder”在法语中将是“Dossier”。如果你不是来自英语国家,你也许应该做这样的事情:

log kind of x

因此,在我的计算机(10.12.6)上运行的整个代码:

set MaxYear to 2018
set MinYear to 1990
--return splitText("abc2015def", 2018, 1990)

tell application "Finder"
    set allfiles to every item of (choose folder with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
    -- set allfile to selections

    repeat with x in allfiles
        log kind of x
        if kind of x is "Folder" then
            -- if xtype is "Folder" then
            set cname to name of x
            set newname to my splitText(cname, MaxYear, MinYear)
            if newname is not "" then
                set name of x to newname
            end if
        end if
    end repeat
end tell


on splitText(theText, MaxYear, MinYear)
    set dyear to MaxYear
    repeat until dyear < MinYear
        set AppleScript's text item delimiters to dyear
        set theTextItems to every text item of theText
        set AppleScript's text item delimiters to ""
        if (count of theTextItems) > 1 then
            return the first item of theTextItems & dyear as string
        end if
        set dyear to dyear - 1
    end repeat
    return ""
end splitText

答案2

谢谢,在这种情况下,我最终发现错误 splittext 是某种内部 AppleScript 项目。我没有添加花括号,程序会自动将我的标准括号更改为花括号。

一旦我将名称改为 _splittext,一切都正常了。

相关内容