(*Timer Script 0.9
  Ajastin, joka laukeaa annetuna hetkenä ja halutuin välein.
  2000-10-21 Tuomas Rosberg.
  Public Domain.
  Tallenna Skriptieditorilla ohjelmana, rastit kohtiin ”älä näytä aloitusnäyttöä” ja ”pysy avoinna”. 
  Valitse ohjelma Finderissa, sitten Arkisto-valikon ”Näytä tietoja -> Muisti” 
  ja lisää haluttu koko 250 kt.
*)

property fileRef : "a string"
property haltScript : false
property prefsExist : false
property aDate : ""
property anInterval : 0

on run
   set fileRef to (path to preferences folder as text) & "Timer Script Lib" as text
   set haltScript to false
   -- The preferences file:
   try
      alias fileRef -- If this doesn't make a working file reference, an error occurs.
   on error -- A new preferences script file is needed.
      set d to current date
      set aString to "script
property targetDate : \"\" 
property theInterval : 0
end script"
      set anObject to run script aString
      set targetDate of anObject to d
      store script anObject in file fileRef with replacing
      set prefsExist to false
   end try
   -- Get and set the preferences:
   if prefsExist is true then
      set d to current date
      my loadLib()
      if aDate <= d then
         if aDate + (anInterval * minutes) <= d then -- The launch time is way too old.
            set prefsExist to false
            run me -- Back to the square one.
         end if
      end if
   else if prefsExist is false then
      set aDate to askDate()
      set d to current date
      if aDate is "" then
         my killProperties()
      else if aDate <= d then
         activate
         display dialog ¬
            "Valittu ajankohta " & return & aDate & return & "on jo ohi." buttons "OK" default button 1 with icon note
         my killProperties()
      else if aDate > d then
         set anInterval to askInterval()
         if anInterval is "" then
            set anInterval to 0
            my killProperties()
         else
            set x to load script alias fileRef
            set targetDate of x to aDate
            set theInterval of x to anInterval
            store script x in file fileRef with replacing
            set prefsExist to true -- On the next run the preferences won't be asked.
         end if
      end if
   end if
end run

on idle
   if haltScript is false then
      set workingApp to getFrontmost() -- Get the application in use.
      -- Read the preferences on every round:
      my loadLib()
      -- Evaluate the date:
      if aDate > (current date) then
         -- Continue idle for another 4 secs:
         return 4
      else if aDate <= (current date) then
         if aDate < ((current date) + (1 * minutes)) then -- The launch time is not too much over.
            my doThings()
            -- Now they are done and:
            if anInterval > 0 then
               activate
               display dialog ¬
                  "Ajastin laukesi. Haluatko laittaa ajastimen pois päältä? " buttons {"Kyllä", "En"} default button "En" with icon note
               set theResult to result
               -- Back to the app in use:
               my setFrontmost(workingApp)
               if button returned in theResult is "Kyllä" then
                  my killProperties()
               else
                  if addInterval() is false then
                     activate
                     display dialog ¬
                        "Virhe asetustiedoston \"" & return & fileRef & return & "\" kanssa. Tarkista tiedosto." buttons "Lopeta" default button 1
                     my setFrontmost(workingApp)
                     my killProperties()
                  end if
               end if
            else if anInterval = 0 then
               -- Back to the app in use:
               my setFrontmost(workingApp)
               my killProperties()
            end if
            return 4
         else -- The launch time is way too old.      
            activate
            display dialog ¬
               "Ajatimen laukeamisaika " & return & aDate & return & "on jo ohi." buttons "OK" default button 1 with icon note
            my setFrontmost(workingApp)
            my killProperties()
         end if
      end if
   else -- HaltScript is true.
      quit me
   end if
end idle

on loadLib()
   --Read the preferences:
   try
      set x to load script alias fileRef
      set aDate to targetDate of x
      set anInterval to theInterval of x
   on error
      set haltScript to true
   end try
end loadLib

on getFrontmost()
   tell application "Finder"
      set l to processes
      repeat with i in l
         if frontmost of i is true then
            exit repeat
         end if
      end repeat
   end tell
   return i
end getFrontmost

on setFrontmost(workingApp)
   tell application "Finder"
      set frontmost of workingApp to true
   end tell
end setFrontmost

on killProperties()
   set prefsExist to false
   set haltScript to true
end killProperties

on addInterval()
   set temp to aDate + (anInterval * minutes)
   try
      set x to load script alias fileRef
      set targetDate of x to temp
      store script x in file fileRef with replacing
      return true
   on error
      return false
   end try
end addInterval

on askDate()
   activate
   repeat
      display dialog ¬
         "Monenko kokonaisen päivän kuluttua ajastimen pitää laueta? " default answer "0" buttons {"Lopeta", "OK"} default button 2
      set theResult to result
      try
         set theDays to text returned in theResult
         set theDays to theDays as integer
         exit repeat
      on error
      end try
   end repeat
   set theButton to button returned in theResult
   if theButton is "Lopeta" then
      return ""
      -- goto 1000 and die :-)
   else -- theButton is "OK"
      repeat
         display dialog ¬
            "Mihin kellonaikaan? Anna aika esimerkin mukaisessa muodossa. " default answer "07:00" buttons "OK" default button 1
         set theTime to text returned of result
         try
            set tid to AppleScript's text item delimiters
            set AppleScript's text item delimiters to ":"
            set shortArm to first text item in theTime as integer
            set longArm to second text item in theTime as integer
            set timerTime to shortArm * 60 + longArm -- as minutes
            set AppleScript's text item delimiters to tid
            exit repeat
         on error
         end try
      end repeat
      set timerDays to theDays * 60 * 24 -- As minutes.
      set timerDays to round timerDays -- The minutes as integer.
      set timerTotal to timerDays + timerTime
      set d to current date
      set d to d - (time in d) -- Today at 00:00:00.
      set target to d + (timerTotal * minutes)
      return target
   end if
end askDate

on askInterval()
   activate
   display dialog ¬
      "Ajastimen laukeamisaika on " & return & aDate & return & "Miten ajastimen pitää laueta? " buttons {"Kerran", "Toistuvasti"} default button 2 with icon -16396
   set theButton to button returned of result
   if theButton is "Kerran" then
      set gap to 0
   else if theButton is "Toistuvasti" then
      display dialog ¬
         "Kuinka monen tunnin välein? " default answer "0,01" buttons "OK" default button 1
      set gap to text returned of result
      try
         set gap to gap as real
         set gap to round (gap * 60) -- As minutes
      on error
         set gap to ""
      end try
   end if
   return gap
end askInterval

on doThings()
   -- Write here whatever the timer has to do, for example:
   activate
   beep 1
   display dialog ¬
      "Huomaa ystävällisesti ajastimen laukeamisaika" & return & return & aDate & return buttons "OK" default button 1
end doThings