2011-01-23 26 views
6

我是一個相當出色的applescripter,並已編寫很長一段時間的腳本。我目前正在創建的應用程序涉及使用「數據庫事件」應用程序。我試圖通過使用子例程來設置字段的值。顯然,我「不能繼續set_duration」,我不知道什麼是錯的。當前源代碼如下。問題與Applescript子程序

property first_run : true 
on run 
if first_run then 
display dialog "THIS APPLICATION IS DESIGNED TO CLEAN UP THE FOLDER CONTAINING IT." & return & return & "After a certain number of days that you set, every item in that folder that has not been used for that duration will automatically be moved to a folder named \"Unused Items\"." with icon 1 buttons {"Cancel", "OK"} default button 2 
set first_run to false 
end if 
tell application "Database Events" 
set quit delay to 0 
try 
get database "Folder Cleanup Wizard" 
on error 
make new database with properties {name:"Folder Cleanup Wizard"} 
tell database "Folder Cleanup Wizard" 
make new record with properties {name:"Duration"} 
tell record "Duration" to make new field with properties {name:"Days", value:set_duration()} --> UNKNOWN ERROR 
end tell 
end try 
end tell 
end run 

on set_duration() 
try 
set duration to the text returned of (display dialog "Enter the minimum duration period (in days) that files and folders can remain inactive before they are moved to the \"Unused Items\" folder." default answer "" with title "Set Duration") as integer 
if the duration is less than 0 then 
display alert "Invalid Duration" message "Error: The duration cannot be a negative number." buttons {"OK"} default button 1 
set_duration() 
end if 
on error 
display alert "Invalid Duration" message "Error: \"" & (duration as string) & "\" is not a valid duration time." buttons {"OK"} default button 1 
set_duration() 
end try 
end set_duration 

回答

10

的問題是,AppleScript的正在處理set_duration作爲database "Folder Cleanup Wizard"application "Database Events"的條款,這要感謝tell b鎖。 (在tell塊之外,只有普通的set_duration()可以工作。)爲了解決這個問題,您需要使用my set_duration(),它告訴AppleScript在該函數的當前腳本中查找。所以,在這種情況下,你會有

... 
tell record "Duration" to ¬ 
    make new field with properties {name:"Days", value:my set_duration()} 
... 
2

我想這是因爲AppleScript的是越來越糊塗了其中set_duration住

做這樣的事情

tell me to set value_to_set to set_duration() 
tell record "Duration" to make new field with properties {name:"Days", value:value_to_set} 

我認爲這會爲你工作