(* MyProgram © kroko GNU GPL*)
global servvicename
set servvicename to "MyProgram"
if getrun() is servvicename then
-- we need to constantly call AppleScript Runner to activate, because user can
-- focus on other windows during this script exec proces
-- (this is a trimmed script from that I wrote as personalized iTunes script plugin- there calling
-- AppleScript Runner to activate was critical for itunes not to get in front of this window)
tell application "AppleScript Runner"
activate
display dialog servvicename & " is running" with title ¬
servvicename buttons {"Preferences", "Good to know", "Stop"} default button 2 with icon 1
end tell
if the button returned of the result is "Stop" then
setrun(servvicename & "Stopped")
else if button returned of the result is "Preferences" then
setprefs()
end if
else if getrun() is (servvicename & "Stopped") then
tell application "AppleScript Runner"
activate
display dialog servvicename & " is stopped" with title ¬
servvicename buttons {"Preferences", "Good to know", "Start"} default button 2 with icon 1
end tell
if the button returned of the result is "Start" then
setrun(servvicename)
else if button returned of the result is "Preferences" then
setprefs()
end if
else if getrun() is "cannotgetrun" then
tell application "AppleScript Runner"
activate
display dialog "Could't read " & servvicename & " Preferences.
Have You set them up?" with title ¬
servvicename buttons {"*censored* this", "Edit Preferences"} default button 2 with icon 0
end tell
if button returned of the result is "Edit Preferences" then
setprefs()
end if
else
tell application "AppleScript Runner"
activate
display dialog "Unknown error!
Couldn't talk to " & servvicename with title servvicename buttons {"Damn!"} default button 1 with icon 2
end tell
end if
on getrun()
tell application "Keychain Scripting"
try
set myKey to first key of current keychain ¬
whose service is servvicename
return name of myKey
on error
return "cannotgetrun"
end try
end tell
end getrun
on setrun(runstate)
tell application "Keychain Scripting"
try
set myKey to first key of current keychain ¬
whose service is servvicename
set programid to account of myKey
set programuri to comment of myKey
set programpw to password of myKey
delay 1
delete myKey
delay 1
make new generic key with properties {name:runstate, kind:"application password", account:programid, service:servvicename, comment:programuri, password:programpw}
on error
tell application "AppleScript Runner"
activate
display dialog "Error!" with title servvicename buttons {"Damn!"} default button 1 with icon 2
end tell
end try
end tell
end setrun
on setprefs()
tell application "AppleScript Runner"
activate
display dialog "Here You can (re)set " & servvicename & " account data " with title ¬
servvicename buttons {"Exit", "Continue"} default button 2 with icon 1
end tell
if the button returned of the result is "Continue" then
tell application "AppleScript Runner"
activate
display dialog "Enter new ID" with title ¬
servvicename default answer ""
end tell
set programid to (text returned of result)
tell application "AppleScript Runner"
activate
display dialog "Enter new Password" with title ¬
servvicename default answer "" with hidden answer
end tell
set programpw to (text returned of result)
tell application "AppleScript Runner"
activate
display dialog "Enter new URI" with title ¬
servvicename default answer "http://www.foo.com/"
end tell
set programuri to (text returned of result)
tell application "AppleScript Runner"
activate
display dialog "Do You want to start " & servvicename & "?" with title ¬
servvicename buttons {"No", "Yes"} default button 2 with icon 1
end tell
if the button returned of the result is "Yes" then
set runstate to servvicename
else
set runstate to (servvicename & "Stopped")
end if
tell application "Keychain Scripting"
try
set myKey to first key of current keychain ¬
whose service is servvicename
try
delete myKey
delay 0.2
make new generic key with properties {name:runstate, kind:"application password", account:programid, service:servvicename, comment:programuri, password:programpw}
on error
tell application "AppleScript Runner"
activate
display dialog "Error!" with title servvicename buttons {"Damn!"} default button 1 with icon 2
end tell
end try
on error
-- This will exec on "Could't read MyProgram Preferences", because set myKey has already failed once on getrun()
try
make new generic key with properties {name:runstate, kind:"application password", account:programid, service:servvicename, comment:programuri, password:programpw}
end try
end try
end tell
else
quit me
end if
end setprefs