2011-08-13 53 views
2

傳統上,每個人只能得到一票。我想通過投票進行同樣的事情。如何使用AppleScript進行投票投票

我在工作時在我的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",這很有效,但我仍然知道如何驗證用戶是否投票。

我想問題標題應該是「我如何驗證用戶投票?」。

非常感謝,

+5

輸入文本文檔中的所有名稱。打印與選民一樣多的副本,並給每個選民一份副本。讓他們在他們想要選舉的人的名字旁勾上一個複選標記,摺疊紙張,然後放入紙板箱中。當每個人都投票時,計票。選中標記最多的人選。 –

+0

杜爾高,絕對輝煌。 +1爲笑。 – BRampersad

回答

2

比爾如果我是你,我會做的所謂「選民」的Database Events應用程序的數據庫。腳本運行時,檢查名稱是當前用戶名的記錄。如果記錄存在,則用戶已投票。同樣,如果記錄不存在,用戶還沒有投票。翻譯成代碼本段讀取:

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 

這比使用一個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 
+1

我喜歡這個數據庫的想法。這很容易和有效。我會擴展它。爲什麼不把投票保存到數據庫呢?然後,您不必收到投票電子郵件。您可以使用第二個腳本從數據庫中檢索投票。第二個腳本會將它們相加,然後除以數據庫中的記錄總數以獲得平均評分。 – regulus6633

+0

@ regulus6633我更新了答案,以反映你說的話:) – fireshadow52

+0

非常好的fireshadow52,我喜歡它! – regulus6633