2

传统上,每个人只能获得一票。我想对投票做同样的事情。

我在工作时在我的 Mac OS X 上有一堆帐户。我们正在投票选举某人作为我们的新部门负责人(不,我不会说谁),看看他是否有资格胜任这份工作。我决定编写一个小脚本来为我们完成这项工作。但是,我似乎不能做一件事:确保用户只能投票一次。我的脚本如下(它显然不会编译):

if the current user has never voted then --pseudo-code
    set the rating_results to (choose from list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} with prompt "Please rate (anonymous) based on your observations. BE HONEST!")
    email_results(rating_results)
else
    display dialog "You already rated (anonymous)!"
end if

我知道你会问“到目前为止我尝试过什么?” 所以我会告诉你。我试过get the current user了,没有用。我也试过do shell script "echo $USER"了,效果很好,但我仍然知道如何验证用户是否投票。

我想问题标题应该是“我如何验证用户是否投票?”。

非常感谢,

账单

4

1 回答 1

2

如果我是你,我会在Database Events应用程序中创建一个名为“Voters”的数据库。运行脚本时,检查名称为当前用户名的记录。如果记录存在,则用户已投票。同样,如果记录不存在,则用户没有投票。这一段翻译成代码如下:

set user_has_voted to false --inital value

tell application "Database Events"
    try
        get database "Voters"
        open database POSIX path of (path to documents folder) & "/Databases/Voters.dbev"
    on error
        make new database with properties {name:"Voters"}
        save
    end try
end tell
set the current_user to the long user name of (system info)
tell application "Database Events"
    tell database "Voters"
        try
            get record current_user
            --No error, the user has voted
            set user_has_voted to true
        on error
            make new record with properties {name:current_user}
            set user_has_voted to false
        end try
    end tell
    close database "Voters" saving yes
end tell

if user_has_voted then
    ...
else
    vote()
end if

这比使用 a 更安全,property因为它可以确定同一用户是否在任何时间范围内投票。

更新:根据@regulus6633 的评论,我添加了一个子例程(您可以将其放在上一个脚本的底部)和另一个脚本来帮助您完成这项工作。子例程将投票存储在数据库中,子例程之后的脚本从数据库中检索信息。

on vote()
    set the rating_results to (choose from list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} with prompt "Please rate (somebody) based on your observations. BE HONEST!")
    if the rating_results is false then error number -128
    tell application "Database Events"
        try
            get database "Votes"
            open database POSIX path of (path to documents folder) & "/Databases/Votes.dbev"
        on error
            make new database with properties {name:"Votes"}
            save
        end try
        try
            get record "Vote Frequency" of database "Votes"
        on error
            tell database "Votes" to make new record with properties {name:"Vote Frequency"}
        end try
        tell database "Votes"
            tell record "Vote Frequency"
                if exists field rating_results then
                    set the value of field rating_results to the value of field rating_results & the rating_results
                else
                    tell (make new field with properties {name:rating_results, value:{}}) to set the end of the value of it to the rating_results
                end if
            end tell
        end tell
        close database "Votes" saving yes
    end tell
end vote

Votes以下是从数据库中检索投票信息的方法。

tell application "Database Events"
    try
        get database "Votes"
        open database POSIX path of (path to documents folder) & "/Databases/Votes"
    on error
        my nobody_voted_yet() --Database Events doesn't allow any user interaction, so we have to use a subroutine to display information to the user
    end try
    set the votes to {}
    set the vote_total to 0
    tell database "Votes"
        tell record "Vote Frequency"
            repeat with i from 1 to the count of fields
                repeat with this_vote in the value of field i
                    set the end of votes to this_vote
                    set the vote_total to the vote_total + this_vote
                end repeat
            end repeat
        end tell
    end tell
    close database "Votes" saving yes --It is always a good idea to save the database, even if you're just retrieving values. This reduces the risk of the values changing themselves.
    my average_votes(votes, vote_total)
end tell

on nobody_voted_yet()
    display dialog "Nobody voted yet! You can't retrieve information that doesn't exist!" buttons{"Cancel"} default button 1
end nobody_voted_yet

on average_votes(vote_length, vote_total)
    set the voting_result to (the vote_total / (length of the vote_length))
    display dialog "The average rating for (somebody) is: " & (round the voting_result as string)
end average_votes
于 2011-08-13T01:28:03.490 回答