Hey how would i go about making my program thats built in applescript have some form of saving the settings that it has?
If you’re app only needs to run on OS X v10.3 or later, then check out this article:
hey great guide, one problem? how do i read it and put it into my program.
Sorry about the huge code, what im trying to do is so they can set the sensitivity and it will carry over even if they exit the app and reload it
edit : sorry should mention that at the moment the current sensitivity parts in this code are old, but in the nib file i have a slider set up to give the number, the prefrence file out put is
starbuck:~ jamesas$ defaults read com.apple.MultiAlarm
{alarmSensitivity = 8; }
-- MultiAlarm.applescript
-- MultiAlarm
-- Created by semaja2 on 13/04/2006
--Name of Application
property appName : "MultiAlarm"
--machType is either powerbook or ibook or mbp
property machType : "powerbook"
property alarmActivated : "false"
--Cordinates
property thisHere : ""
property theUpperX : ""
property theLowerX : ""
property theUpperY : ""
property theLowerY : ""
--Sensitivity 2 for powerbook/ibook 6 for macbook
property sensitivity : "6"
--Password
property get_password : "0"
property the_password : "Hello"
property new_password : ""
--Sets old Volume
property theOldVolume : ""
--Sets what the Volume should be set to on alert
property alarmVolumeAlert : "100"
--Tells if voice should be used
property alarmVoice : false
--Used to keep alarm going
property alarmLocked : false
--Sounds
property alert_snd : ""
--When Application loads
on launched theObject
--REMOVED CODE
end launched
--When Application Loads NIB
on awake from nib theObject
set alert_snd to load sound "caralarm.aiff"
end awake from nib
--When Application is Idle
on idle theObject
--Checks if alarm has been set off
if alarmLocked is true then
alarmAlert(1)
--Checks if Alarm Should be set off
else if alarmActivated is true then
--runs motion cmd to find x and y axis
set theNewCoords to do shell script thisHere & "Contents/Resources/motion_" & machType & " " & 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
--Activates Lock
activateLock(1)
--Sounds Alarm
alarmAlert(1)
--Displays Growl Notification
GrowlNotify("Theft in progress", "Alarm", "Warning : Theft in progress")
end if
end if
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
else
display dialog "Alarm is not Armed"
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" attached to window "main"
beep 2
end if
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" attached to window "main"
beep 2
end if
--beep 2
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" attached to window "main"
beep 2
end if
--beep 2
--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" attached to window "main"
beep 2
end if
--beep 2
else if n is "Enable Alarm" then
alarmActivate(1)
else if n is "Disarm Alarm" then
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"
setPassword(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
else if n is "setSensitvity" then
set {text:sensitivity} to text returned of (display dialog "Please enter your required sensitivity" attached to window "about" default answer sensitivity) as string
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 first text item of theCoords
set theY to second 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
--SETS THE PASSWORD
on setPassword(thePassword)
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
--GETS KEYCHAIN PASSWORD
on 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
--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
---------------------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"
--------------------------------------------------
--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 & " " & machType
if theCoords is not "" then
set {theX, theY} to getXY(theCoords)
set theX to theX + 1000
set theY to theY + 1000
set theUpperX to theX + sensitivity
set theLowerX to theX - sensitivity
set theUpperY to theY + sensitivity
set theLowerY to theY - sensitivity
end if
--Displays growl notification
GrowlNotify("Alarm Activated", "Alarm", "Warning : Alarm Primed")
end alarmActivate
--Alarm Deactivation Script
on alarmDeactivate(do)
--Deactivates the check
set alarmLocked to false
set alarmActivated to false
--plays the disarming sound
beep 2
play (load sound "arm.wav")
--Displays growl notification
GrowlNotify("Alarm Deactivated", "Alarm", "Warning : Alarm Disarmed")
--Sets Volume to Volume before arming
set volume output volume theOldVolume
end alarmDeactivate
--Controls what happens when alarm is set off
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
return
end alarmAlert
--Controls what happens when alarm is told to lock
on activateLock(do)
set alarmLocked to true
set visible of window "main" to true
--tell application "ScreenSaverEngine"
-- activate
--end tell
end activateLock
The problem is that the article was cut off when it was moved to that area. We’re trying to fix it.
Sample:
on will finish launching theObject
try
tell user defaults
-- make defaults values if they don't exist
make new default entry at end of default entries with properties {name:"alarmSensitivity", content:0}
end tell
on error errMsg number errNum
log "Error " & errNum & ": " & errMsg
end try
end will finish launching
on whatever()
tell user defaults
set alarmSensitivity to content of default entry "alarmSensitivity"
end tell
end whatever
ok fairenough any link to the orginal guide?
Also with that code if i was to tell it every time the app started to run:
on will finish launching theObject
try
tell user defaults
– make defaults values if they don’t exist
make new default entry at end of default entries with properties {name:“alarmSensitivity”, content:6}
end tell
on error errMsg number errNum
log "Error " & errNum & ": " & errMsg
end try
does that mean every time it starts it will reset the setting or will it just put a default in if its not there
Any link to the orginal guide?
No.
does that mean every time it starts it will reset the setting or will it just put a default in if its not there
Sigh. that’s supposed to be in there too. From the default entry documentation:
If you attempt to make a new entry for a key that already exists, no new entry is created and the value for the key is not changed.
hey im having trouble with this, ive done everything and the slider saves its position but it seems that one boot and after pressing set it wont set the sensitivity variable. which of course screws the whole program ot screw up
-- MultiAlarm.applescript
-- MultiAlarm
-- Created by semaja2 on 13/04/2006
--Name of Application
property appName : "MultiAlarm"
--machType is either powerbook or ibook or mbp
property machType : "powerbook"
property alarmActivated : "false"
--Cordinates
property thisHere : ""
property theUpperX : ""
property theLowerX : ""
property theUpperY : ""
property theLowerY : ""
--Sensitivity 2 for powerbook/ibook 6 for macbook
property sensitivity : ""
--Password
property get_password : "0"
property the_password : "Hello"
property new_password : ""
--Sets old Volume
property theOldVolume : ""
--Sets what the Volume should be set to on alert
property alarmVolumeAlert : "100"
--Tells if voice should be used
property alarmVoice : false
--Used to keep alarm going
property alarmLocked : false
--Sounds
property alert_snd : ""
--When Application loads
on launched theObject
--REMOVED CODE
end launched
on will finish launching theObject
try
tell user defaults
-- make defaults values if they don't exist
make new default entry at end of default entries with properties {name:"alarmSensitivity", content:2}
end tell
on error errMsg number errNum
log "Error " & errNum & ": " & errMsg
end try
tell user defaults
set sensitivity to content of default entry "alarmSensitivity"
end tell
end will finish launching
--When Application Loads NIB
on awake from nib theObject
set alert_snd to load sound "caralarm.aiff"
end awake from nib
--When Application is Idle
on idle theObject
--Checks if alarm has been set off
if alarmLocked is true then
alarmAlert(1)
--Checks if Alarm Should be set off
else if alarmActivated is true then
--runs motion cmd to find x and y axis
set theNewCoords to do shell script thisHere & "Contents/Resources/motion_" & machType & " " & 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
--Activates Lock
activateLock(1)
--Sounds Alarm
alarmAlert(1)
--Displays Growl Notification
GrowlNotify("Theft in progress", "Alarm", "Warning : Theft in progress")
end if
end if
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
else
display dialog "Alarm is not Armed"
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" attached to window "main"
beep 2
end if
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" attached to window "main"
beep 2
end if
--beep 2
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" attached to window "main"
beep 2
end if
--beep 2
--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" attached to window "main"
beep 2
end if
--beep 2
else if n is "Enable Alarm" then
alarmActivate(1)
else if n is "Disarm Alarm" then
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"
setPassword(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
else if n is "setSensitvity" then
tell user defaults
set sensitivity to content of default entry "alarmSensitivity"
end tell
say sensitivity
--set {text:sensitivity} to text returned of (display dialog "Please enter your required sensitivity" attached to window "about" default answer sensitivity) as string
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 first text item of theCoords
set theY to second 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
--SETS THE PASSWORD
on setPassword(thePassword)
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
--GETS KEYCHAIN PASSWORD
on 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
--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
---------------------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"
--------------------------------------------------
--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 & " " & machType
if theCoords is not "" then
set {theX, theY} to getXY(theCoords)
set theX to theX + 1000
set theY to theY + 1000
set theUpperX to theX + sensitivity
set theLowerX to theX - sensitivity
set theUpperY to theY + sensitivity
set theLowerY to theY - sensitivity
end if
--Displays growl notification
GrowlNotify("Alarm Activated", "Alarm", "Warning : Alarm Primed")
end alarmActivate
--Alarm Deactivation Script
on alarmDeactivate(do)
--Deactivates the check
set alarmLocked to false
set alarmActivated to false
--plays the disarming sound
beep 2
play (load sound "arm.wav")
--Displays growl notification
GrowlNotify("Alarm Deactivated", "Alarm", "Warning : Alarm Disarmed")
--Sets Volume to Volume before arming
set volume output volume theOldVolume
end alarmDeactivate
--Controls what happens when alarm is set off
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
return
end alarmAlert
--Controls what happens when alarm is told to lock
on activateLock(do)
set alarmLocked to true
set visible of window "main" to true
--tell application "ScreenSaverEngine"
-- activate
--end tell
end activateLock
ok ive come to the conclusion that the variables are not being set when they are ment to during the script, any ideas why this would be :S
anyone?
well after doing a bit of research i was able to set the variables but unfortuantly the prefrence files are saved in unitext so they dont work as variables, does anyone know how to set them back to normal text so i can use them as variables