2011-08-13 10 views
2

伝統的に、1人の投票で1人の投票しか得られません。私は投票投票で同じことをしたいと思います。AppleScriptで投票投票を行う方法

私は仕事中のMac OS Xにたくさんのアカウントを持っています。私たちは新しい部署長として誰かを選ぶことに投票しています(私は誰も言わないでしょう)。私は仕事をする小さなスクリプトを書くことに決めました。しかし、私は1つのことをするように見えることはできません:ユーザが1度だけ投票できることを確認してください。私のスクリプトは、(それは明らかにコンパイルされません)以下である:

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

Dour High、絶対に華麗です。 +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を使用するよりも安全です。

UPDATE:@の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

私はデータベースのこの考えが好きです。それは簡単で効果的です。私はそれを拡張するだろう。なぜ投票をデータベースにも保存しないのですか?その後、投票の電子メールを受け取る必要はありません。 2番目のスクリプトを使用して、データベースから投票を取得することができます。 2番目のスクリプトはそれらを合計し、平均レーティングを得るためにデータベース内のレコードの総数で割ったものです。 – regulus6633

+0

@ regulus6633あなたの言ったことを反映する答えを更新しました:) – fireshadow52

+0

非常に素晴らしいfireshadow52、私はそれが好きです! – regulus6633