-- Type/Creator 3.5 
-- Näyttää tiedoston tyypin ja luojan. Voi myös muuttaa ne, jos tiedosto ei ole kansio, lukittu tai ohjelmatiedosto.

-- Datatiedoston luonutta ohjelmaa ei löydy levyiltäsi. Mitä teet?
-- a) Avaa tiedosto käyttäen kyseistä tiedostomuotoa ymmärtävää ohjelmaa, käyttäen ohjelman Open... tai Import... -komentoa.
-- -- Joudut avaamaan tiedoston kerrallaan. Et saa avattua kaksoisosoittamalla.
-- b) Käytä Mac OS 7.x säädintä Macintosh Easy Open muuttamaan kyseisen tiedostotyypin avaajaohjelmaa.
-- c) Käytä Mac OS 8.5 ja uudemman säädintä File Exchange muuttamaan kyseisen tiedostotyypin avaajaohjelmaa.
-- d) Muuta tiedoston luojamuuttuja vastaamaan kyseistä tiedostomuotoa ymmärtävän ohjelmasi luojamuuttujaa.
-- -- Viimeinen vaihtoehto on suositeltu vain niille, jotka tietävät, mitä tekevät. Laita myös alkuperäiset muuttujien arvot muistiin.

-- 1999 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.

property theName : ""
property theType : ""
property theCreator : ""
property userCancelled : false
property allSelected : false

on run -- käynnistys Omppuvalikosta tai Skriptieditorista.
   tell application "Finder"
      set theList to selection
   end tell
   my tellTypeCrea(theList)
end run

on open theList -- käynnistys raahaamalla tiedostoja kuvakkeen päälle.
   tellTypeCrea(theList)
end open

on tellTypeCrea(theList)
   set userCancelled to false -- ei ole peruutettu.
   set allSelected to false -- ei kaikkia valittuja tiedostoja kerralla.
   tell application "Finder"
      repeat with aFile in theList -- toistaa valittujen tiedostojen listalta tiedosto kerrallaan.
         if userCancelled is false then
            if allSelected is false then
               set thePath to aFile as text
               if the last character of thePath is not ":" then -- se ei ole kansio.
                  set theName to (name of aFile)
                  set theType to (file type of aFile)
                  set theCreator to (creator type of aFile)
                  display dialog ¬
                     "Tiedoston   ”" & theName & "" & return & return & ¬
                     "tyyppi on    " & theType & return & ¬
                     "ja luoja      " & theCreator buttons {"Muuta", "OK"} default button "OK"
                  if button returned of result is "Muuta" then
                     my verify(aFile)
                  end if
               else -- se on kansio.
                  display dialog ¬
                     "Kansiolla ”" & theName & "” ei ole tyyppiä tai luojaa." buttons "Ohita" default button 1
               end if
            else if allSelected is true then -- muuta kaikkien valittujen tiedostojen parametrit.
               exit repeat
            end if
         else if userCancelled is true then -- peruttu.
            exit repeat
         end if
      end repeat
   end tell
end tellTypeCrea

on verify(aFile) -- tarkistetaan, saako tiedoston tyyppiä ja luojaa muuttaa.
   tell application "Finder"
      if locked of aFile is false then -- se ei ole lukittu. 
         if theType is not "APPL" then -- se ei ole ohjelmatiedosto.
            my newParams(aFile)
         else -- se on ohjelmatiedosto.
            display dialog ¬
               "Ohjelmatiedoston tyyppiä ja luojaa ei saa muuttaa." buttons "OK" default button "OK"
         end if
      else --se on lukittu.
         display dialog ¬
            "Lukitun tiedoston tyyppiä ja luojaa ei saa muuttaa." buttons "OK" default button "OK"
      end if
   end tell
end verify

on newParams(aFile)
   set newType to ""
   set newCreator to ""
   repeat --kysyy tyyppiä, kunnes sen pituus on neljä merkkiä.
      display dialog ¬
         "Tiedoston tyyppimuuttujassa on neljä (4) merkkiä." & return & ¬
         "Muuta tiedoston  ”" & theName & "”  tyyppi muotoon:" & return default answer theType buttons {"Peru", "OK"} default button "OK"
      if button returned of result is "OK" then
         if (count characters in text returned of result) = 4 then
            set newType to text returned of result
            repeat --kysyy luojaa, kunnes sen pituus on neljä merkkiä.
               display dialog ¬
                  "Tiedoston luojamuuttujassa on neljä (4) merkkiä." & return & ¬
                  "Muuta luoja muotoon:  " default answer theCreator buttons {"Peru", "Muuta kaikki", "OK"} default button "OK"
               if button returned of result is "OK" then
                  if (count characters in text returned of result) = 4 then
                     set newCreator to text returned of result
                     my changeParams(aFile, newType, newCreator)
                     exit repeat --uusi luoja hyväksytty
                  end if
               else if button returned of result is "Muuta kaikki" then
                  if (count characters in text returned of result) = 4 then
                     set newCreator to text returned of result
                     set allSelected to true -- lopettaa tellTypeCrea(theList) toiston
                     my changeAll(newType, newCreator)
                     exit repeat -- uusi luoja hyväksytty
                  end if
               else if button returned of result is "Peru" then
                  set userCancelled to true --peruttu.
                  exit repeat
               end if
            end repeat --lopettaa luojan kysymisen.
            exit repeat --uusi tyyppi hyväksytty.
         end if
      else if button returned of result is "Peru" then
         set userCancelled to true
         exit repeat --peruttu.
      end if
   end repeat --lopettaa tyypin kysymisen.
end newParams

on changeParams(aFile, newType, newCreator) -- yksittäisen tiedoston parametrien muutos.
   set theOld to {theType, theCreator}
   set theNew to {newType, newCreator}
   considering case --AppleScript ei erota muuten isoja ja pieniä kirjaimia.
      if theNew is not theOld then --käyttäjä antoi jommalle kummalle muuttujalle uuden arvon.
         tell application "Finder"
            set file type of aFile to newType
            set creator type of aFile to newCreator
         end tell
      end if
   end considering
end changeParams

on changeAll(newType, newCreator) -- kaikkien valittujen tiedostojen parametrien muutos.
   set theOld to {theType, theCreator}
   set theNew to {newType, newCreator}
   considering case --AppleScript ei erota muuten isoja ja pieniä kirjaimia.
      if theNew is not theOld then --käyttäjä antoi jommalle kummalle muuttujalle uuden arvon.
         tell application "Finder"
            set theList to selection
            repeat with x in theList
               set thePath to x as text
               if the last character of thePath is not ":" then -- se ei ole kansio.
                  if locked of x is false then -- se ei ole lukittu. 
                     if file type of x is not "APPL" then -- se ei ole ohjelmatiedosto.                  
                        set file type of x to newType
                        set creator type of x to newCreator
                     end if
                  end if
               end if
            end repeat
         end tell
      end if
   end considering
end changeAll