(*  Processes 1.2
   Vaatimaton skripti, joka laatii luettelon kaikista käynnissä olevista prosesseista.
   Se näyttää myös ohjelman halutun (= varatun) ja käytössä olevan muistitilan 
   kilotavuina (1 kilobyte = 1024 bytes = 8192 bits).
   Tiedot prosesseista tallennetaan tekstitiedostoon käynnistyslevyn työpöytäkansioon.
    Finderin tarjoamat "Tietoja tästä koneesta" ja Ohjelmavalikko eivät näytä
   taustalla toimimaan kirjoitettuja prosesseja (esim. Print Monitor).

   2000 Tuomas Rosberg <vanhentunut>
   Lähdekoodi on ilmaista ja tarkoitettu yleiseen käyttöön (public domain).
   Sitä lainattessa kirjoittajan nimi on mainittava.
   Koodilla ei ole mitään takuuta, ja sen sisältävää ohjelmaa käyttävät
   toimivat omalla vastuullaan.
   *)

set dataBase to {}
set aRecord to {}

tell application "Finder"
   set usedTotal to 0
   set preferredTotal to 0
   set dataBase to {"Mac OS = System ", product version, "   Finder ", version & return & return & "Nimi" & tab & "Käytössä (kt)" & tab & "Varattu (kt)" & return & return}
   set allApps to every process
   repeat with anApp in allApps
      set preferredSpace to (total partition size of anApp) as integer
      set preferredSpace to round (preferredSpace / 1024)
      set preferredTotal to preferredTotal + preferredSpace
      set usedSpace to (partition space used of anApp) as integer
      set usedSpace to round (usedSpace / 1024)
      set usedTotal to usedTotal + usedSpace
      set anApp to name of anApp as text
      set aRecord to {anApp & tab & usedSpace & tab & preferredSpace & return}
      set dataBase to dataBase & aRecord
   end repeat
   set dataBase to dataBase & return & "Sovellusohjelmien käytössä yht." & tab & usedTotal & return & "Sovellusohjelmille varattu yht." & tab & preferredTotal
   my show(dataBase)
end tell

on show(dataBase)
   set dataBase to dataBase as text
   tell application "Finder"
      set startUpDisk to name of startup disk
   end tell
   --Laaditaan tiedostonimi, jossa on mukana kellonaika, jolloin saadaan kohtalaisen varmasti ainutkertainen nimi:
   set theDate to current date
   set aTime to time in theDate -- Sekunteja keskiyöstä lukien.
   set h to aTime div 3600
   set h to makeTwoDigit(h)
   set temp to aTime mod 3600
   set m to temp div 60
   set m to makeTwoDigit(m)
   set s to temp mod 60
   set s to makeTwoDigit(s)
   set theTime to {h, m, s} -- Kellonaika tunteina, minuutteina ja sekunteina.
   set tid to text item delimiters
   set text item delimiters to "*"
   set theTime to theTime as text
   set text item delimiters to tid
   set fileName to "Processes " & theTime
   set thePath to startUpDisk & ":Desktop Folder:" & fileName as text --Tiedostonimi valmis.
   --
   open for access file thePath with write permission -- Luo tiedoston.
   write dataBase to result
   close access file thePath -- Tiedosto valmis.
   tell application "Finder"
      set theScriptEditor to application file id "ToyS"
      open file thePath using theScriptEditor
   end tell
end show

on makeTwoDigit(i)
   if i < 10 then
      set i to "0" & i as text
   end if
   return i
end makeTwoDigit