(* PPPäänavaus 1.0
   Skripti avaa PPP-yhteyden Open Transportia käyttäen. Se avaa myös valitut 
   tiedostot tai ohjelmat, kun yhteys on muodostunut. Modeemin asetukset ja 
   TCP/IP-asetukset on ensin laitettava kuntoon Modeemi- ja 
   TCP/IP-säätimissä.
   
   Tallenna Skriptieditorilla ohjelmaksi, rastit valintataulussa kohtiin 
   "pysy avoinna" ja "älä näytä aloitusnäyttöä". Laita modeemi päälle 
   ja kaksoisosoita ohjelmaa. 
   
   HUOMAA: Salasanasi tallentuu skriptiin! Älä siis jätä ajettua skriptiä 
   muiden ulottuville, jos useampi käyttää samalla koneella PPP-yhteyttä.
   
   Vaatii addin, jossa on Network Object Suite -sanasto ®1996 Apple Computer, Inc. 
   Esimerkiksi Remote Access Commands (Mac OS 8.x) tai PPP commands (Mac OS 7.x).
   Sellainen kuuluu käyttöjärjestelmään ja 
   sijaitsee Järjestelmäkansio:Scripting Additions: 

   2000 Tuomas Rosberg <vanhentunut>
   Ilmainen ja tarkoitettu yleiseen käyttöön (public domain).
   Lainattessa kirjoittajan nimi on mainittava.
   Koodilla ei ole mitään takuuta, ja sen sisältävää ohjelmaa käyttävät
   toimivat omalla vastuullaan.
   *)
property puhelinnumero : "puhelinnumero"
property tunnus : "tunnus"
property salasana : "salasana"
property tiedotSaatu : false
property avattavat : {}
property lopeta : false

on run
   set lopeta to false
   considering case -- Erotetaan pienet isoista kirjaimista. 
      if tiedotSaatu is false then
         my kysyTiedot() -- Ensimmäisellä ajokerralla.
      end if
      -- Sama testi uudestaan:
      if tiedotSaatu is true then -- Skripti on kerännyt hyväksyttävät yhteystiedot aiemmin.
         if state in (PPP status) is in {"idle", "Vapaa"} then -- PPP ei päällä ennestään.
            try
               PPP connect address puhelinnumero user name tunnus password salasana
            on error errr
               my anError(errr)
            end try
         else if state in (PPP status) is in {"connected", "Yhteys"} then -- PPP päällä ennestään.
            my jatkotoimet()
         end if
      else -- Hyväksyttäviä yhteystietoja ei ole saatu.
         set lopeta to true
      end if
   end considering -- Unohtaa sen pienen eron.
end run

on idle -- Kun "pysy avoinna" -ohjelma vastaanottaa systeemiltä idle eventin.
   if lopeta is false then
      try
         if state in (PPP status) is not in {"connected", "Yhteys"} then
         else -- PPP päällä.
            if avattavat is not {} then
               tell application "Finder"
                  repeat with a in avattavat
                     set a to a as alias
                     open a
                  end repeat
               end tell
               set lopeta to true -- Tarvittavat tiedostot avattu.
            end if
            my jatkotoimet()
         end if
      on error errr
         my anError(errr)
      end try
   else -- lopeta is true.
      quit me
   end if
   return 2
end idle

on jatkotoimet()
   activate me
   display dialog ¬
      "Etäyhteys (PPP) on auki. Sulje" & return & ¬
      "se istunnon jälkeen Säätimistä." & return & return & ¬
      "Voit lisätä tai poistaa" & return & ¬
      "avattavia kohteita. " buttons {"Lisää", "Poista", "Lopeta"} ¬
      default button "Lopeta" with icon note
   set painike to button returned of result
   if painike is "Lisää" then
      my avaa()
   else if painike is "Poista" then
      if avattavat is not {} then
         copy avattavat to lista -- Tekee varmuuskopion.
         set poistettavat to (choose from list avattavat with prompt ¬
            "Poista skriptistä kohteet: " with multiple selections allowed) -- Vaatii Mac OS 8.* standard additions.
         set avattavat to {}
         repeat with x in lista
            if x is not in poistettavat then
               set avattavat to avattavat & x
            end if
         end repeat
      else
         display dialog "Tyhjä lista. " buttons "Lopeta" default button 1
      end if
      set lopeta to true
   else if painike is "Lopeta" then
      set lopeta to true
   end if
end jatkotoimet

on avaa()
   set valmis to false
   repeat until valmis is true
      display dialog ¬
         "Haluatko vielä valita ohjelmia tai tiedostoja, " & return & ¬
         "jotka avataan kun PPP-yhteys muodostuu? " & return & return ¬
         buttons {"Ei", "Kyllä"} default button "Kyllä"
      if button returned of result is "Kyllä" then
         if avattavat is {} then
            set numero to 1
         else
            set numero to (count avattavat) + 1
         end if
         set f to (choose file with prompt ¬
            "Valitse avattava tiedosto " & numero)
         set f to f as text
         if f is not in avattavat then
            set avattavat to avattavat & f
            set numero to numero + 1
         end if
      else -- Painike "Ei".
         set valmis to true
      end if
   end repeat
end avaa

on anError(errr)
   display dialog "Tulos: " & return & ¬
      errr buttons "Lopeta" default button 1
   set lopeta to true
end anError

on kysyTiedot()
   repeat
      (* HUOMAA:
             Jos yhteystietojesi puhelinnumero, käyttäjätunnus tai salasana sisältää välilyönnin (space),
             joudut muuttamaan tekstikohteiden erotinta (AppleScript's text item delimiters).*)
      display dialog "Anna internetyhteytesi sisäänsoittonumero, " & return & ¬
         "käyttäjätunnus ja salasana. Erota ne" & return & ¬
         "välilyönnein (space). Tiedot ovat pakollisia." default answer ¬
         "puhelinnumero tunnus salasana" buttons {"Peru", "OK"} default button "OK"
      set tulos to result -- Sisältää valitun painikkeen ja annetun tekstin.
      set erotin to AppleScript's text item delimiters
      set AppleScript's text item delimiters to space
      if button returned in tulos is "OK" then
         try
            set puhelinnumero to first text item of text returned in tulos
            set tunnus to second text item of text returned in tulos
            set salasana to third text item of text returned in tulos
         on error errr
            my anError(errr)
            exit repeat
         end try
      else -- Peruttu.
         set lopeta to true
         exit repeat
      end if
      set AppleScript's text item delimiters to erotin
      set peitetty to ""
      repeat (count salasana) times
         set peitetty to peitetty & ""
      end repeat
      display dialog "Puhelinnumero: " & puhelinnumero & return & ¬
         "Tunnus: " & tunnus & return & ¬
         "Salasana: " & peitetty buttons {"Korjaa", "OK"} default button "OK"
      if button returned of result is "OK" then
         -- Skripti ei hyväksy tyhjiä arvoja yhteystietoihin:
         if puhelinnumero is not in {"puhelinnumero", " ", ""} then
            if tunnus is not in {"tunnus", " ", ""} then
               if salasana is not in {"salasana", " ", ""} then
                  set tiedotSaatu to true
                  my avaa() -- Kysyy avattavia tiedostoja.
                  exit repeat -- Tiedot hyväksytty.
               end if
            end if
         end if
      end if
   end repeat -- Lopettaa yhteystietojen kysymisen.
end kysyTiedot