keychain

As alot of you probaly know, im making a alarm that is set when the laptop is moved (http://semaja2.ewales.info), but at the time of posting the server is down.

Anywho onto a serious note, i was wondering how you would use the user key in a applescript, like so when they goto disarm the alarm is uses the account that it was locked in to disarm it.

I ask this because my current solution just wont work no matter was it gives me errors if the key doesnt exist which is whats really screwed, and other problems.

Anyideas?

-- alarm.applescript
-- alarm

--  Created by Steven Halford on 08/04/2006.
-- Modified by semaja2 on 13/04/2006
--  Copyright 2006 Steven Halford. All rights reserved.

property appName : "MultiAlarm"
--machType is either powerbook or ibook
property machType : "powerbook"
property alarmActivated : "false"
property thisHere : ""
property theUpperX : ""
property theLowerX : ""
property theUpperY : ""
property theLowerY : ""
property get_password : "0"
property the_password : "Hello"
property new_password : ""
property theOldVolume : ""
property alarmVolumeAlert : "100"
property alarmVoice : false
property alarmGrowl : 0

--Sounds
property alert_snd : ""


to getPW(KeyName)
	tell application "Keychain Scripting"
		launch
		tell current keychain to tell (some generic key whose name is KeyName) to set PWD to password
	end tell
end getPW

on launched theObject
	set alert_snd to load sound "caralarm.aiff"
	--load sound "caralarm.aiff"
	
	set theMachine to do shell script "/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/grep 'Machine Name' | /usr/bin/colrm 1 20"
	if theMachine is not "" then
		set {theModel} to getSystem(theMachine)
	end if
	
	if theModel contains "powerbook" then
		--beep 1
		set visible of window "about" to true
	else if theModel contains "ibook" then
		if machType contains "hirespb" then
			set visible of window "nosupport" to true
			--beep 10
		else
			--beep 1
			set visible of window "about" to true
		end if
	else
		set visible of window "nosupport" to true
		--beep 10
	end if
	
	
end launched

--on awake from nib theObject

--end awake from nib

--on will quit theObject
-- not sure if loaded sounds are saved on quit
-- no harm
--	delete alert_snd
--end will quit

on idle theObject
	if alarmActivated is false then
		
	end if
	if alarmActivated is true then
		
		
		repeat while alarmActivated is true
			
			
			--runs motion cmd to find x and y axis
			do shell script thisHere & "Contents/Resources/AMSTracker"
			set theNewCoords to last paragraph of result
			if theNewCoords is not "" then
				set {theNewX, theNewY} to getXY(theNewCoords)
				set theNewX to theNewX + 1000
				set theNewY to theNewY + 1000
				--Checks the x and y axis for change if change is found it will start the process
				if theNewX is greater than theUpperX or theNewX is less than theLowerX or theNewY is greater than theUpperY or theNewY is less than theLowerY then
					alarmAlert(1)
				else
					--stops sound and reactivates systems
					--stop (load sound "caralarm.aiff")
					set alarmGrowl to 0
					
				end if
			end if
		end repeat
	end if
end idle


---------------Interface Functions---------------



--Menu Items

on choose menu item theObject
	set n to name of theObject
	if n is "Arm" then
		alarmActivate(1)
	else if n is "Disarm" then
		if alarmActivated is true then
			set visible of window "main" to true
		end if
	end if
end choose menu item

--Text Fields

on end editing theObject
	set n to name of theObject
	if n is "secureDisarm" then
		set c to content of text field "secureDisarm" of window "main"
		set the_password to c
		set the content of text field "secureDisarm" of window "main" to ""
		if the_password = getPW("MultiAlarm") then
			set visible of window "main" to false
			alarmDeactivate(1)
		else
			--display dialog "Incorrect Password"
			beep 2
		end if
		--beep 2
	else if n is "secureExit" then
		set c to content of text field "secureExit" of window "quitCheck"
		set the_password to c
		set the content of text field "secureExit" of window "quitCheck" to ""
		if the_password = getPW("MultiAlarm") then
			quit
		else
			--display dialog "Incorrect Password"
			beep 2
		end if
		--beep 2
	else if n is "secureOld" then
		set c to content of text field "secureOld" of window "setCheck"
		set the_password to c
		set the content of text field "secureOld" of window "setCheck" to ""
		if the_password = getPW("MultiAlarm") then
			set visible of window "setpass" to true
			set visible of window "setCheck" to false
		else
			--display dialog "Incorrect Password"
			beep 2
		end if
		--beep 2
	else if n is "secureSet" then
		set the_password to content of text field "secureSet" of window "setpass"
		set visible of window "setpass" to false
		keychain(the_password)
		--beep 1
	end if
end end editing

--Clicked objects

on clicked theObject
	set n to name of theObject
	if n is "Disarm" then
		
		set c to content of text field "secureDisarm" of window "main"
		set the_password to c
		set the content of text field "secureDisarm" of window "main" to ""
		if the_password = getPW("MultiAlarm") then
			set visible of window "main" to false
			alarmDeactivate(1)
		else
			--display dialog "Incorrect Password"
			beep 2
		end if
		--beep 2
		
	else if n is "Confirm" then
		
		set c to content of text field "secureOld" of window "setCheck"
		set the_password to c
		set the content of text field "secureOld" of window "setCheck" to ""
		if the_password = getPW("MultiAlarm") then
			set visible of window "setpass" to true
			set visible of window "setCheck" to false
		else
			--display dialog "Incorrect Password"
			beep 2
		end if
		--Exit System
	else if n is "exit" then
		
		set c to content of text field "secureExit" of window "quitCheck"
		set the_password to c
		set the content of text field "secureExit" of window "quitCheck" to ""
		if the_password = getPW("MultiAlarm") then
			quit
		else
			--display dialog "Incorrect Password"
			beep 2
		end if
		--beep 2
		
	else if n is "Enable Alarm" then
		alarmActivate(1)
	else if n is "Disarm Alarm" then
		set x to count (every sound)
		say x
		if alarmActivated is true then
			set visible of window "main" to true
		end if
	else if n is "Set" then
		set newPWD to content of text field "secureSet" of window "setpass"
		keychain(newPWD)
		set visible of window "setpass" to false
		--beep 1
	else if n is "voice" then
		if alarmVoice is true then
			set alarmVoice to false
		else if alarmVoice is false then
			set alarmVoice to true
		end if
	end if
	
end clicked

----------------Functions------------

--Gets and organises coordinates
on getXY(theCoords)
	set oldTID to AppleScript's text item delimiters
	set text item delimiters to {"   "}
	set theX to second text item of theCoords
	set theY to third text item of theCoords
	set text item delimiters to oldTID
	return {theX, theY}
end getXY

--gets machine name
on getSystem(theMachine)
	set oldTID to AppleScript's text item delimiters
	set text item delimiters to {" "}
	set theName to first text item of theMachine
	set text item delimiters to oldTID
	return {theName}
end getSystem

--Checks and controls growl
on GrowlNotify(myName, myTitle, myText)
	set myApp to ""
	
	tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
	
	if GrowlRunning ≥ 1 then
		try
			set myApp to "GrowlHelperApp"
			
			set notifs to "{\"Alarm Activated\", \"Alarm Deactivated\",\"Theft in progress\"}"
			
			tell application myApp to run script "register as application \"" & appName & "\" all notifications " & notifs & " default notifications " & notifs & " icon of application \"" & appName & "\""
			
			tell application myApp to run script "notify with name \"" & myName & "\" title \"" & myTitle & "\" application name \"" & appName & "\" description \"" & myText & "\" icon of application \"" & appName & "\""
		end try
	end if
end GrowlNotify

on keychain(newPWD)
	tell application "Keychain Scripting"
		tell keychain 1
			unlock -- gotta unlock it.
			--set theKey to (first key whose name is "Some Key") -- then find it
			set password of (first generic key whose name is "MultiAlarm") to newPWD
		end tell
	end tell
end keychain

---------------------Alarm Functions---------------------

--Alarm Activation Script
on alarmActivate(do)
	set theOldVolume to get output volume of (get volume settings)
	set alarmActivated to true
	---------------Sets paths---------------------
	set thisHere to path to me
	set thisHere to POSIX path of thisHere
	--------------Sound Configs-----------------
	--Sets the sound for alarm activation
	set theArming to load sound "armx2.wav"
	--------------------------------------------------
	--set alarmVolumeOrignal to volume
	--plays the arming sound
	--beep
	play theArming
	
	--runs motion cmd to find x and y axis
	do shell script thisHere & "Contents/Resources/AMSTracker"
	set theCoords to last paragraph of result
	if theCoords is not "" then
		set {theX, theY} to getXY(theCoords)
		set theUpperX to theX + 1002
		set theLowerX to theX + 998
		set theUpperY to theY + 1002
		set theLowerY to theY + 998
	end if
	
	GrowlNotify("Alarm Activated", "Alarm", "Warning : Alarm Primed")
	
	
end alarmActivate

--Alarm Deactivation Script
on alarmDeactivate(do)
	--Deactivates the check
	set alarmActivated to false
	--plays the arming sound
	--beep 2
	play (load sound "arm.wav")
	--Displays growl notification
	GrowlNotify("Alarm Deactivated", "Alarm", "Warning : Alarm Disarmed")
	set volume output volume theOldVolume
end alarmDeactivate



on alarmAlert(do)
	--Play alarm sound
	play alert_snd
	--Say
	if alarmVoice is true then
		say "Help! I am being stolen."
	end if
	--beep 1
	--Set the volume to MAX
	set volume alarmVolumeAlert
	--Displays Growl Notification
	if alarmGrowl is equal to 0 then
		set alarmGrowl to 1
		GrowlNotify("Theft in progress", "Alarm", "Warning : Theft in progress")
	end if
	return
	
	
end alarmAlert

Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)

Are you planning to have a lot of keys?

no just the one key to arm it and disarm the alarm

ok ive been trying to make this work for a long time now, and its annoying cuz its the only thing left for this version.

Ive managed to get it to make a key if the key doesnt exist but it cant set the password

on setPassword(thePassword)
	tell application "Keychain Scripting"
		try
			set pw_keychain to "login"
		end try
		
		--set current keychain to pw_keychain
		set pw_keychain to current keychain
		unlock
		try
			set the_key to first key of pw_keychain whose name is "MultiAlarm"
		on error
			set the_key to (make new generic key with properties {name:"MultiAlarm", password:"MultiAlarm"})
		end try
		set password of the_key to thePassword
	end tell
end setPassword

Hi semaja2,

I was looking in my old keychain test script and used the ‘service’ property. Here’s the test script:

–tell application “Keychain Access” to activate
tell application “Keychain Scripting”
set cur_kch to current keychain
try
set kch to keychain “Passwords”
on error
set kch to (make new keychain with properties {name:“Passwords”})
end try
(*
tell application “Finder”
activate
display dialog “Enter password for Passwords:” default answer “”
end tell
set p to text returned of result
)
unlock kch with password “passwords”
(

set this_key to first key of kch whose name is “Ann”
password of this_key
*)
set current keychain to kch
set new_key to ¬
(make new generic key with properties ¬
{service:“Joe”, account:“Test”, password:“somepassword”})
lock kch
try
set current keychain to cur_kch
end try
end tell

Hope this works for you.

gl,

i tried what you said but i still get a error

on setPassword(thePassword)
	(*tell application "Keychain Scripting"
		try
			set pw_keychain to "login"
		end try
		
		--set current keychain to pw_keychain
		set pw_keychain to current keychain
		unlock current keychain
		try
			set the_key to first key of pw_keychain whose name is "MultiAlarm"
			set password of the_key to thePassword
		on error
			set the_key to (make new generic key with properties {service:"MultiAlarm", account:"MultiAlarm", password:thePassword})
		end try
		
	end tell*)
	tell application "Keychain Scripting"
		set cur_kch to current keychain
		try
			set kch to keychain "Passwords"
		on error
			set kch to (make new keychain with properties {name:"Passwords"})
		end try
		
		unlock kch with password "passwords"
		
		set current keychain to kch
		set new_key to ¬
			(make new generic key with properties ¬
				{service:"MultiAlarm", account:"MultiAlarm", password:thePassword})
		new_key
		lock kch
		try
			set current keychain to cur_kch
		end try
	end tell
	
end setPassword

Try opening up Keychain Access and manually delete any keys you made if any.

done that and it didnt work :S, could it just be my ibook?

I don’t know why a lot of the stuff isn’t working, but another thing I mentioned is to encrypt your password. You could also use the password to encrypt some text. Here’s an article from code exchange:

http://bbs.applescript.net/viewtopic.php?id=11428

gl,

ive just tested it on another machine, and same error. I just dont get this connection error, how would i find out what it means

Hi semaja2,

Your last posted script shows that you are trying to use a keychain named “Passwords”. If there is no keychain by that name then there’s bound to be something wrong. I thought you were planning on using the users default keycahin. You posted that in one of your posts.

The reason why I use the newly made keychain “Passwords” is that I don’t want to mix up this program with the default keychain which contains keys to things that are important. If anything goes wrong, then I can just delete this keychain. I try to not tell people what to do, but give suggestions.

When I have time I’ll write a script which tries to show you what is happening in the script. Unless, you figure it out with your own tryings.

gl,

yeh well i just couldnt work out how to use the login keychain, etc so i was copying exactly what you had, but ill be away for 8days but ill try to check back during the 8 days

thanks for all your help

Hey i managed to get internet at my hotel, i was wondering if you could build the code required for my task as i am having no luck and im really falling behind on the dead line for MultiAlarm, and if you have read the posts and about box your name is mentioned as the coder for numerous bits since youve really helped me on most of it

Hi semaja2,

Try this. Run Keychain Access. Go to the View menu and enable Show Status In Menu Bar. Make sure there is only the user keychain. Close Keychain Access. TheShow Status in Menu Bar adds the keychain icon to the menu bar. Look in there and see if there are any other keychains besides the user keychain. Keychain Access an Keychain Scripting is sometimes not in sync if things aren’t done perfectly through scripting.

Later,

well this is really becoming a issue and annoying me since i cant make it work no matter what is suggested, thanks a heap kel

how hard is it to use the login key or activate the screen saver with a password

or how would i make it so i can make a prefrence file

semaja2,

Did you try doing what I said and what happened? What did you see in the icon menu for Keycahin Scripting. This could be part of the reason, because sometimes you say that something is working now, but you don’t say what you did to fix it. Don’t have shame. Me personally, I don’t care, because I’ve done some really dumb things in the past and present. So, you should just say what you are doing or did. Who cares? Except me and all the other adults.

gl,

Hi semaja2,

I’m sorry but I give up. It’s too much of a one way street. Hope you have luck on your program.

gl,

thanks for all your help kel youve been great help with MultiAlarm, i guess i will have to drop the feature for the time being as i cant nor you can work it out, btw the site it http://semaja2.net and im planning to do a few final touch ups and remove the features which rely on certain code which is well bugged :stuck_out_tongue:

also for the apple script forum you guys have been great and are added under supporters on my site and are also in the program, thanks again guys you will probally hear from me again when i try again at adding the needed features.