Dialog

Hey guys how would i go about making a prefrence thingo for a apple script so that a user can set a passcode and then when my alarm in my applescript is set off it brings up a dialog asking for that passcode, and if the correct code is put in it disables the alarm

http://swat.gotdns.com/index.php/archives/8 – ibookalarm source code

Hi semaja2,

It’s hard to think of a good way to do something if you don’t know the overall picture. Personally, I don’t follow links that, I don’t know, so you might want to explain more. Off hand I would say that I wouldn’t store a password in a preference file. It’s easy to read preference files. You could encrypt the text, but it might be better to store your passwords in a keychain. You post sound very interesting.

gl,

ok well ill give the over view of the whole script

When the script is running it checks with a motion.c(uses SMS to find axis) when the axis change it will sound the alarm, now what i would like is once the alarm is set off it requires a password to turn off.

now storing the password in the keychain would be great but i have no idea how to do that

Hi semaja2,

Just a few months ago I went through a long post on Keychain Scirpting, but I forgot a lot. As long as you know how to read dictionaries, it’s kind of easy although there may be a few things you might have to experiment with.

The first thing I did is open the dictionary for Keychain Scripting and I notice that the main object of the application is ‘keychain’. You start your script like this in the Script Editor:

tell app “Keychain Scripting”
every keychain
end tell

Lo and behold, when I look in the result window, I find:

{keychain “kel” of application “Keychain Scripting”}

What a big surprise, huh? Now I know that there are no other keychains for me (the user). I must have deleted the other keychains for me the current user. Anyway, I look in the dictionary for Keychain Scripting again:

Class keychain: A set of related keys to applications and services
Plural form:
keychains
Elements:
key by numeric index, satisfying a test
Properties:
name string [r/o] – the name of the keychain
locked boolean [r/o] – true if the keychain is locked, false if the keychain is unlocked

semaja2, sorry but I just got a phone cal and need to go. I’l be back later unless someone else chimes in. You should experiment with keychain scripting and keychain access, or unix and you can enter passwords in AppleScript Studio with bullets.

gl,

thanks for the reply, illl have a look around, whats this applescript studio application, ive just been using xcode 2.2

Yes. Using AppleScript Studio is good and fun.

You can create password fields.

gl,

ok i found what you mean, any guides on linking the gui to the apple script, also how do i get to the dictonary

Hi semaja2,

Here’s an example of how you might make a new key in a new keychain named “Passwords”.

property the_password : “passwords”

tell application “Keychain Scripting”
try
set pw_keychain to keychain “Passwords”
on error
set pw_keychain to (make new keychain with properties ¬
{name:“Passwords”})
end try
unlock pw_keychain with password the_password
set current keychain to pw_keychain
try
set the_key to first key of pw_keychain whose name is “key1”
on error
set the_key to (make new generic key with properties {name:“key1”, password:“key1”})
end try
password of the_key
end tell

When the script asks for a password for the new keycahin, enter “passwords”. To get properties of keys, you would need the keychain password in this line:

unlock pw_keychain with password the_password

From there, you can get a stored password in a key.

To learn how to see the dictionary of an application, search your Help menu. Basically, in Script Editor, you choose the menu item Open Dictionary or something like that.

gl,

I think something like this might be the way to go:

-- IF you don't want to put your password in clear text in a script, then you can add your password in the Keychain Access application and use it from your script. 
-- In Keychain access, click File>New Password Item..., give it a name, put your account shortname in account, and enter the password. I called mine ASPW. Highlight it in the password list and under the Attributes button enter its kind as generic key. This is chosen because there aren't many of them and the search is much faster. 

-- The script used  is as follows:
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

set IPW to text returned of (display dialog "Enter your password" default answer "Password here" with hidden answer)
if IPW = getPW("ASPW") then beep 3

ok i got that to kinda work, the with hidden answer wouldnt work tho, also how would i make it so the dialog does not stop the alarm from making noises when the dialog is up

Also how would i make it so when the password is entered it turns off the alarm, by simply exiting or just telling it to not go off again until it goes to its orginal axis

-- 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 : ""
property thisHere : ""
property theUpperX : ""
property theLowerX : ""
property theUpperY : ""
property theLowerY : ""

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
	
	alarmActivate(1)
	
end launched

on idle theObject
	--runs motion cmd to find x and y axis
	set theNewCoords to do shell script thisHere & "Contents/Resources/motion " & machType
	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 alarmActivated to "0"
			
		end if
	end if
end idle



on getXY(theCoords)
	set oldTID to AppleScript's text item delimiters
	set text item delimiters to {" "}
	set theX to first text item of theCoords
	set theY to second text item of theCoords
	set text item delimiters to oldTID
	return {theX, theY}
end getXY

on getSystem(theMachine)
	set oldTID to AppleScript's text item delimiters
	set text item delimiters to {" "}
	set theModel to first text item of theMachine
	set text item delimiters to oldTID
	return {theModel}
end getSystem

on smsSystem(theCMD)
	set oldTID to AppleScript's text item delimiters
	set text item delimiters to {" "}
	set smsState to first text item of theCMD
	set text item delimiters to oldTID
	return {smsState}
end smsSystem



--Alarm Activation Script
on alarmActivate(do)
	
	---------------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"
	--------------------------------------------------
	
	--plays the arming sound
	beep
	play theArming
	
	--runs motion cmd to find x and y axis
	set theCoords to do shell script thisHere & "Contents/Resources/motion " & machType
	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

on alarmAlert(do)
	--Play alarm sound
	--play (load sound "caralarm.aiff")
	--Say
	--say "Help! I am being stolen."
	beep
	--Checks if growl has already been activated
	if alarmActivated is less than 1 then
		--Set the volume to MAX
		set volume 100
		set alarmActivated to "1"
		GrowlNotify("Theft in progress", "Alarm", "Warning : Theft in progress")
		
		
		set IPW to text returned of (display dialog "Enter your password" default answer "Password here" with hidden)
		if IPW = getPW("MultiAlarm") then
			play (load sound "arm.aiff")
			beep 3
			set volume 50
		end if
		beep 3
		beep 3
	end if
	return
	
	
end alarmAlert


--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 Deactived\",\"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


I can answer one question: If you are running Jaguar, “hidden answer” doesn’t work - it’s an improvement in Tiger.

In general terms, the answer to the second question is that you should set things up so setting the alarm requires a password, but if the alarm is going, then the same dialog appears and the password stops it.

Hi Adam,

I don’t think Growl works in Jaguar.

gl,

Disregard the last post. I just found the older version.

gl,

I can’t get Growl to work here.

Anyway, AppleScript Studio has its own ‘display dialog’ and I don’t know if your version has a ‘hidden’ property. When you have yoru project open, check out the dictionary there under events for the Panel suite.

I don’t know why you don’t use the built in secure text field. If you attach it to a window, then your script continues making sounds or whatever.

gl,

Here’s an example if you want to look at it. In interface builder, I just added a text field named “secure” to the main window named “main”. I added a menu with menu items “go” and “stop”. The end editing handler is connected to the secure text field. Here’s the quick script:

property get_password : false
property the_password : “password”

on idle theObject
if get_password then
set visible of window “main” to true
set get_password to false
end if
beep 1
return 2
end idle

on choose menu item theObject
set n to name of theObject
if n is “go” then
set get_password to true
else if n is “stop” then
set get_password to false
end if
end choose menu item

on end editing theObject
set c to content of text field “secure” of window “main”
if c is the_password then
set visible of window “main” to false
end if
end end editing

The idle handler continuously beeps. If you select the menu item “go”, then get_password is set to true. The idle handler then show the window. When the user enters the password and presses return, the end editing handler closes the window if theuser entered “password”. The “stop” menu item is not needed, but this is how you can detedt which menu item was chosen. All you really need to do is set the get_pasword flag.

gl,

Thanks for the reply, im trying to build the interface but it seems interface builder wants to be difficult and crash every time i goto save :S

Operating System: Mac OS X (10.4)

It’s such a waste of time when they start aciting up. :confused:

I forgot to say, but think you know, to set the window to not be visible a launch in Interface Builder.

gl,

ok this is getting annoying, no matter what i do it crashes, ive tryed reinstalling and deleting the prefrence files but with no success, any ideas?

ok i have no idea why this isnt working, but i have a screen shoot of what i would like the disarm screen to look like

http://swat.gotdns.com/multialarm/disarm.tiff

i have a theory but have yet to test it, it might have something to do with my project file so if you mind helping try using the source code as well

http://swat.gotdns.com/multialarm/alarm.zip

Hi semaja2,

I don’t follow links, I don’t know, but what people say they do is clean all targets in Project Builder. I also read that there are new bugs with xcode. You might want to post some of your code in the AppleScript Studio section and maybe someone might see something that would cause errors. Try the cleaning targets first.

gl,