2011-08-13 6 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

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를 사용하는 것보다 더 안전합니다.

업데이트 : Per @ 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

Very nice fireshadow52, 나는 그것을 좋아한다! – regulus6633