【问题标题】:Problem with Getting info for item (applescript)获取项目信息的问题(applescript)
【发布时间】:2014-01-01 09:56:48
【问题描述】:

我是一个非常优秀的applescripter,并且希望得到一些帮助。我正在制作一个类似于 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

【问题讨论】:

    标签: applescript subroutine


    【解决方案1】:

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

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

    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
    

    确保在check 函数中将RBPFMSfree_space 声明为全局变量:

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

    另一个错误:在display alert 语句中为RBPFMS as string 加上括号:

    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
    

    【讨论】:

    • 非常感谢!现在这个问题已经解决了。但是,当我尝试调用子例程时,它会出错“无法继续检查”。你能检查一下子程序中的所有语句吗?那很好啊。 :)
    • 变量 free_space 和 RBPFMS 没有在 check 函数中定义。
    • 我知道。我将它们设置为全局变量,以便它们可以在任何地方使用。
    • ...再想一想,我可能没有。我会检查的。
    • 我检查以确保它们是全球性的,而且确实如此。我不知道是什么导致了错误。 :(
    【解决方案2】:

    当我尝试调用子程序时,它会报错“无法继续检查。”

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

    告诉 Finder 调用它的“检查”子例程是行不通的。 Finder 会告诉您它不知道如何“检查”。它将使用的词是“Finder 无法继续检查”。但是您可以告诉 Finder 调用您本地定义的“检查”子例程。当您调用子例程时,您只需在 my 前面加上“检查”。每次从 tell 块中调用自己的子例程时都需要这样做。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 2014-11-15
      • 1970-01-01
      相关资源
      最近更新 更多