获取项目信息时出现问题(applescript)

发布于 2024-10-10 16:43:36 字数 1601 浏览 13 评论 0原文

我是一个非常好的苹果脚本编写者,并且需要一些帮助。我正在制作一个回收站应用程序,类似于 Windows XP 回收站。这当然是一滴水滴。当我将项目拖放到应用程序上时,应用程序会启动一个子例程,该子例程旨在检查是否超出了回收站(垃圾箱)的大小限制。但是,当我尝试获取垃圾箱中项目的信息时,出现的错误消息是:“Finder 出现错误。未找到文件项目 1。”我真的需要帮助:( 子程序如下:

on check() 
tell application "Finder"
    set the total_size to 0
    set the trash_items to get every item in trash
    if the trash_items is not {} then
        repeat with i from 1 to the count of items in trash
            set this_info to get info for item i of trash --ERROR ON THIS LINE
            set the total_size to the total_size + (size of this_info)
        end repeat
        try
            set the second_value to the free_space / (RBPFMS / 100)
            if the total_size is greater than the second_value then
                display alert "Size Limit Exceeded" message "The Recycle Bin cannot receive any more items because it can only use " & RBPFMS as string & " of your hard drive." buttons {"OK"} default button 1
                return false
            else
                return true
            end if
        on error
            set RBP to ((path to startup disk) as string) & "Recycle Bin Properties"
            display dialog "Error: You have modified the properties file for the Recycle Bin. Do not modify the properties file. It is there to store information that is used to determine the properties for the Recycle Bin." with title "Property File Modified" with icon 0 buttons {"OK"} default button 1
            set the name of RBP to "DELETE ME"
            error number -128
        end try
    end if
end tell
end check

I am a pretty good applescripter, and would like some help with this. I am making a recycle bin application, similar to that of the Windows XP Recycle Bin. It is of course a droplet. When I drop items onto the application, the application starts a subroutine designed to check whether the size limit of the Recycle Bin (Trash) was exceeded. However, when I try to get the info for the items in the Trash, the error message that comes up is: "Finder got an error. File item 1 wasn't found." I really need help :(
The subroutine is below:

on check() 
tell application "Finder"
    set the total_size to 0
    set the trash_items to get every item in trash
    if the trash_items is not {} then
        repeat with i from 1 to the count of items in trash
            set this_info to get info for item i of trash --ERROR ON THIS LINE
            set the total_size to the total_size + (size of this_info)
        end repeat
        try
            set the second_value to the free_space / (RBPFMS / 100)
            if the total_size is greater than the second_value then
                display alert "Size Limit Exceeded" message "The Recycle Bin cannot receive any more items because it can only use " & RBPFMS as string & " of your hard drive." buttons {"OK"} default button 1
                return false
            else
                return true
            end if
        on error
            set RBP to ((path to startup disk) as string) & "Recycle Bin Properties"
            display dialog "Error: You have modified the properties file for the Recycle Bin. Do not modify the properties file. It is there to store information that is used to determine the properties for the Recycle Bin." with title "Property File Modified" with icon 0 buttons {"OK"} default button 1
            set the name of RBP to "DELETE ME"
            error number -128
        end try
    end if
end tell
end check

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糯酥胸 2024-10-17 16:43:36

该错误是由表达式 info for item i ofrasht 引起的。子表达式 item i oftrash 返回一个 (Finder) 项目对象。但是,info for 命令需要文件的别名或文件引用(请参阅AppleScript 语言指南)。

有两种方法可以修复表达式。显式地将项目转换为别名,即:

repeat with i from 1 to the (count of items) in trash
  set this_info to get info for (item i of trash as alias)
  set the total_size to the total_size + (size of this_info)
end repeat

或者不使用 info for 命令,只需使用 Finder 项目的 size 属性:

repeat with i from 1 to the (count of items) in trash
  set the total_size to the total_size + (size of item i)
end repeat

确保同时具有 RBPFMSfree_spacecheck 函数中声明为全局变量:

on check()
    global RBPFMS
    global free_space
    ...
end

另一个错误:在 中将括号作为字符串 放在 RBPFMS 周围显示警报语句:

display alert "Size Limit Exceeded" message "The Recycle Bin cannot receive any more items because it can only use " & (RBPFMS as string) & " of your hard drive." buttons {"OK"} default button 1

The error is caused by the expression info for item i of trash. The subexpression item i of trash returns a (Finder) item object. The info for command however requires an alias or file reference to the file (see AppleScript language guide).

There are two ways to fix the expression. Explicitly cast the item to an alias, i.e.:

repeat with i from 1 to the (count of items) in trash
  set this_info to get info for (item i of trash as alias)
  set the total_size to the total_size + (size of this_info)
end repeat

Or instead of using the info for command, simply use the Finder item's size property:

repeat with i from 1 to the (count of items) in trash
  set the total_size to the total_size + (size of item i)
end repeat

Be sure to have both RBPFMS and free_space declared as global variables in the check function:

on check()
    global RBPFMS
    global free_space
    ...
end

Another bug: put parentheses around RBPFMS as string in the display alert statement:

display alert "Size Limit Exceeded" message "The Recycle Bin cannot receive any more items because it can only use " & (RBPFMS as string) & " of your hard drive." buttons {"OK"} default button 1
谢绝鈎搭 2024-10-17 16:43:36

当我尝试调用子例程时,出现错误“无法继续检查。”

该调用需要在“check()”前面加上“my”。

告诉 Finder 调用它的“检查”子例程是行不通的。 Finder 会告诉您它不知道如何“检查”。它将使用的词语是“Finder 无法继续检查”。但您可以告诉 Finder 调用您本地定义的“检查”子例程。当您调用子例程时,您只需在“check”前添加 my 前缀即可。每次从tell 块内部调用自己的子例程时都需要执行此操作。

when I try to call the subroutine, it errors "Can't continue check."

The call needs a "my" in front of "check()".

Telling the Finder to call its "check" subroutine won't work. The Finder will tell you that it doesn't know how to "check". The words it will use are "Finder can't continue check." But you can tell the Finder to call your locally-defined "check" subroutine. You simply prefix my to "check" when you call the subroutine. You need to do this every time you call your own subroutine from inside a tell block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文