脚本使用"写"之前正在工作,现在不在Applescript中工作

时间:2015-04-20 01:30:58

标签: arrays macos file applescript

我正在做一个家庭作业提醒应用程序。我最近问了一个关于使用"写"的问题。正确地回答了我的问题。我的脚本运行正常,但现在它已停止向我的文件写入任何内容。我必须改变一些东西,但我无法弄清楚错误是什么。有什么建议? (请注意:这仍然是一项正在进行的工作,并非一切都是竞争;但使用"写"的部分不应该受影响)

代码:

set saveFile to (path to desktop as text) & "Script Log.txt"
set assignments to read file the saveFile from 1 to eof using delimiter "*"

repeat
display dialog "What would you like to do?" buttons {"New Assignment", "Delete/Edit Assignment", "Exit"}
set function to button returned of result
if function = "New Assignment" then

    display dialog "What subject is this assignment for?" default answer ""
    set subject to text returned of result

    display dialog "What do you need to bring home for this assignment?" default answer ""
    set materials to text returned of result

    display dialog "What is the assignment?" default answer ""
    set instructions to text returned of result

    (choose from list {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Long-Term"} with prompt "When is this assignment due?")
    set dueDate to result as text
    if dueDate = "Long-Term" then
        display dialog "When is this due?" default answer ""
        set dueDat to text returned of result
    end if
    set assignments to {assignments & (subject & ": You need to bring " & materials & " home. It is due on " & dueDate & ". For this assignment, you need to " & instructions & ".")}

else if function = "Delete/Edit Assignment" then

    (choose from list {item 1 of assignments} with prompt "Which assignmnet would you like to delete/edit?")

else if function = "Exit" then

    set fullText to ""

    repeat with i from 1 to number of items in assignments
        set thisItem to item i of assignments & "*"

        set fullText to fullText & thisItem
        if i is not number of items in assignments then set fullText to fullText & return

    end repeat

    my scriptLog(fullText)

    exit repeat
end if
end repeat

on scriptLog(thisText)
  try
    open for access file the saveFile with write permission
    write (thisText & return) to file the saveFile starting at eof
    close access file the saveFile
on error
    try
        close access file the saveFile
    end try
end try
end scriptLog

display dialog assignments

1 个答案:

答案 0 :(得分:0)

saveFile未在scriptLog处理程序中定义。

更改第一行:set saveFile to (path to desktop as text) & "Script Log.txt"

属性,因为它的值是静态的。一个属性对代码的其余部分自动是全局的,它是处理程序。

property saveFile : (path to desktop as text) & "Script Log.txt"

要记住的另一件事是尝试块可以在调试时隐藏任何错误,因此如果您正在调试,则应该记得将它们注释掉。