Adding user Authentication using Applescript - applescript

I want to add user authentication in my applescript to perform some task. If the username and password is correct then only the next task should happen. How to do this using applescript?

You haven't really said how much security you want.
The most basic of solutions:
display dialog "Enter username:" default answer ""
set enteredUsername to text returned of result
display dialog "Enter password:" default answer ""
set enteredPassword to text returned of result
if enteredUsername is "CORRECTUSERNAME"
if enteredPassword is "CORRECTPASSWORD"
-- do something
end if
end if
Save it as a read-only script and it'll prevent casual users from looking at the contents of the script, but this is not really secure at all if someone really wants to break through your protection.

If your Mac is running Leopard (Mac OS X 10.5+), then you could use the long user name property of the system info command, which returns the full name of the current user. Having said that, you could do this...
set the current_user to (get the long user name of (system info)) as string
set the input_password to the text returned of (display dialog "Please enter the password for " & the current_user & ":" default answer "" buttons{"OK"} default button 1 with hidden answer) --The user cannot bypass this dialog.
--The 'hidden answer' property, when true, changes every character you type in the dialog box into ◘'s.
try
--This next line will verify that the password entered matches the current user's password.
do shell script "history -c" user name the current_user password the input_password with administrator privileges
on error --incorrect password
display dialog "Sorry. You entered the wrong password." buttons{"Cancel"}
end try
--Put whatever you want to do after authentication here.
If you have any questions, just ask me :)

If you are wanting a username and password input then use this:
set a to the text returned of (display dialog "Please enter your username" default answer "")
set b to the text returned of (display dialog "Please enter your password" default answer "")
if a="username" then --set the username to your username
if b="password" then --set the password to your password
--what ever is after your password protection
end if
end if

Related

Applescript - remember state between executions?

Some Applescripts I have used have remembered state between executions, e.g., location for Open/Save dialogues.
Now I have written an AS that takes a string as input from the user (via a display dialog). I would like the script to remember that string between executions. Possible? How?
If you make the string a property, it will remember that string between executions, until the next time you open up and re-save the script.
Here’s an example:
property myName : ""
if myName is "" then
display dialog "What is your name?" default answer ""
set myName to the text returned of the result
end if
display notification "Hello, " & myName
Save the script as an “Application”, “Script”, or “Script bundle”. If saved as “Text” (or if run from Script Editor) it will not maintain properties between runs.
The first time you run it, it will notice that myName is the empty string and request your name.
Subsequent times you run it, it will already have your name in the property myName.
If you open the script again, and then resave the script, this resets all properties. If you need the script to retain data through edits, you’ll need to save it in a database, which could be as simple as a text file in a central location.
set nameFile to POSIX file "/Users/USERNAME/name.txt"
tell application "Finder"
if exists nameFile then
set nameFileHandle to open for access nameFile
set myName to read nameFileHandle
close access nameFileHandle
else
display dialog "What is your name?" default answer ""
set myName to the text returned of the result
set nameFileHandle to open for access nameFile with write permission
write myName to nameFileHandle
close access nameFileHandle
end if
end tell
display notification "Hello, " & myName
In this case the variable nameFile doesn’t need to be a property since it never changes. Because it doesn’t need to be a property, this version also does not need to be saved as non-Text, and can even be run from Script Editor.
It is also very simple; if you need to store and retrieve multiple items, you would need to follow up on the “File Read/Write” section of “StandardAdditions”. You can get there from Script Editor under “File:Open Dictionary…”.
For more complex memory, you may find the “Property List Suite” in the “System Events” dictionary useful.

is that possible to create vbscript file can using the send key and run the program at the same time to execute?

I'm pretty new on the vbscript,not sure if I can explain clearly, Please let me know if you need more info to help me out on this , Big Thanks!
I need to execute a adding user program, and that program has to run it under the cmd run as admin privilege, at the same time, have to enter the user name at the end in order to make this function normal.
looks like this :
c:\windows\ccm\postdeployment\lin\XX.exe "username" Then enter
(was thinking possible to use the vbscript to use input box to enter the useranem and execute the command, But i'm not sure how to do that)
Try this
username = InputBox("Enter user name below")
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\windows\ccm\postdeployment\lin\XX.exe " & username
You should lookup for a online tutorial on WSH

Create Apple Script to Map SMB Shares with custom servers

I want to make a applescript that will let the user type in the server name and name of the shared folder and map it via applescript.
I know how to map a set in stone smb share but I want this to be user friendly so they can they type in the share name.
Example
smb://share3/installs
The script would ask what server is it on: share3
Script would ask what is the folder name: installs
Then the script would popup the default connect to server logon info that the user needs to type.
Well, what you're asking for is display dialog and a bit of do shell script, which is detailed in the AppleScript Language Guide (PDF).
So, to answer your question directly, you probably want something like the following:
set server to text returned of (display dialog "Type the name of the server" with title "Open Share" default answer "10.0.10.50")
set share to text returned of (display dialog "Type the name of the share" with title "Open Share" default answer "installs")
do shell script "open smb://" & server & "/" & share
However, if your users are "simple" as you say, then you might not want them typing in server names and share names. Instead, you could provide them with pick lists.
set serverShares to {{"share2", {"installA", "installB"}}, {"share3", {"install1", "install2"}}}
set serverList to {}
repeat with servers in serverShares
set the end of serverList to item 1 of servers
end repeat
set serverChoice to item 1 of (choose from list serverList with title "Open Shared Volume")
set shareList to {}
repeat with i from 1 to length of serverShares
set server to item 1 of item i of serverShares
if server is equal to serverChoice then
set shareList to item 2 of item i of serverShares
exit repeat
end if
end repeat
set shareChoice to item 1 of (choose from list shareList with title "Open Shared Volume")
do shell script "open smb://" & serverChoice & "/" & shareChoice
Just customize the server/share name nested list at the top to customize which shares are available on which servers. Using the shell script open smb://server/share will prompt the user for login credentials if they don't already have them saved in their keychain.

How to make a voting poll with AppleScript

Traditionally, each person gets only one vote. I'd like to do the same thing with a voting poll.
I have a bunch of accounts on my Mac OS X at work. We are voting to elect someone as our new department head (no I won't say who) to see if he qualifies for the job. I decided to write a little script that does the job for us. However, I can't seem to do one thing: make sure a user can only vote one time. My script is below (it obviously won't compile):
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
I know you will ask "What have I tried so far?" so I will tell you. I tried get the current user, to no avail. I've tried do shell script "echo $USER" as well, which worked, but I still have know idea how to verify whether or not the user voted.
I guess the question title should be "How do I verify whether or not a user voted?".
Thanks so much,
Bill
If I were you, I would make a database in the Database Events application called "Voters". When the script is run, check for a record whose name is the current user's name. If the record exists, the user has voted. Likewise, if the record doesn't exist, the user hasn't voted. Translated into code this paragraph reads:
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
This is more secure than using a property because it can determine if the same user has voted within any time frame.
UPDATE: Per #regulus6633's comment, I have added a subroutine (that you can put at the bottom of the previous script) and another script to help you with the job. The subroutine stores the votes in a database, and the script following the subroutine retrieves the info from the database.
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
Here is how you would retrieve the voting information from the Votes database.
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

How do I diagnose a compiled Applescript that just quits at startup?

I wrote two applescripts so that my wife could launch mt-daapd and shut it down easily. They work fine in the Script Editor app but when I compile them into stand-alone apps, the apps work the first time I test them. Then they embarrass me as I proudly show them off to my wife. I see the "open" animation and then they just sit there. I've created other stand-alone apps before and this hasn't happened.
I tried changing the app type to a bundle (same problem). I even tried attaching to the executable via gdb to see if I could break on something magic to tell me what was going on. I looked in the Console for some information. Nothing was ther The scripts laughed in my face.
How do I fix this problem?
I've included one of the scripts below; the second is pretty much the same. I'm running 10.5.8.
property userpassword : ""
if userpassword is "" then
display dialog "Please enter your user password:" default answer "" with hidden answer
set userpassword to text returned of result
set the_password to "Undefined"
repeat until the_password is "Correct"
try
do shell script "/opt/local/sbin/mt-daapd -c /etc/mt-daapd.conf" password userpassword with administrator privileges
set the_password to "Correct"
on error
display dialog "Sorry, the password entered was not correct. Please try again:" default answer "" with hidden answer
set userpassword to text returned of result
end try
end repeat
if the_password is "Correct" then
display dialog "Your music is being shared!" buttons {"Done"} default button "Done"
end if
end if
I'm not sure how this is happening but the script is saving the value of userpassword between calls so once it has been set to whatever value, it retains that value and just exits the program. I discovered this after looking at how I created my other stand alone apps.
properties in applescripts are not fixed, they're like the properties of any other object. You can change them at run time, or evn from another script. so if your script1 was
property potato: "potayto"
say potato
and you ran another script
set potato of script1 to "potahto"
then running script1 again would make your computer say "potahto".
Properties can be useful ways of storing preferences in scripts.
Just delete the first if statement, it's redundant anyway. Check if the password is correct, rather than if it is empty.
thus:
property userpassword :""
set the_password to "Undefined"
repeat until the_password is "Correct"
try
do shell script "/opt/local/sbin/mt-daapd -c /etc/mt-daapd.conf" password userpassword with administrator privileges
set the_password to "Correct"
on error
display dialog "Sorry, the password was not correct. Please try again:" default answer "" with hidden answer
set userpassword to text returned of result
end try
end repeat
if the_password is "Correct" then
display dialog "Your music is being shared!" buttons {"Done"} default button "Done"
end if

Resources