-- ApplicationAlias 1.5
-- Skripti katsoo valittuun kansioon ja sen alakansioihin ja luo aliaksen
-- jokaiselle löytämälleen ohjelmatiedostolle (tyyppi APPL), jonka ehdotettu muistikoko on yli 500 kt.
-- (Teksturi II:n koko on 512 kt.) Aliakset sijoitetaan uuteen Omenavalikon kansioon. 
-- Skripti aloittaa kysymällä, missä kansiossa ohjelmatiedostoja on.
-- Vaikka Macintoshin levyjä voi valita kuin kansioita, käynnistyslevyn Työpöytä 
-- ei osaa kertoa skriptille, missä muut levyt todellisuudessa sijaitsevat.

-- 2000 Tuomas Rosberg  <vanhentunut>
-- Osa lähdekoodista on peräisin Applen Laita alias Omenavalikkoon -skriptistä.
-- 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 success : {}
--
on run
   tell application "Finder"
      set the_items to selection
      if (count items in the_items) = 1 then
         if creator type of item 1 in the_items is in {"aplt", "dplt"} then -- yksi skripti valittuna, tämän skriptin tiedosto.
            set the_items to {}
         end if
      end if
   end tell
   ApplicationAlias(the_items)
end run

on open the_items
   ApplicationAlias(the_items)
end open

on ApplicationAlias(the_items)
   set success to false -- viritetään onnistumisen odotukset korkealle.
   set the_chosen_folder to ""
   set target_folder_name to " Ohjelmat"
   tell application "Finder"
      activate
      display dialog "Ole hyvä ja anna Omenavalikon kansion nimi." & return & ¬
         "Ohjelmiesi aliakset sijoitetaan sinne." & return ¬
         default answer target_folder_name buttons {"Peru", "OK"} default button "OK" with icon note
      if button returned of result is "OK" then
         set target_folder_name to text returned of result
         if not (exists of folder ¬
            target_folder_name of apple menu items folder of startup disk) then -- tarkistaa, onko kansio olemassa.
            make new folder at apple menu items folder ¬
               with properties {name:target_folder_name} -- luo uuden kansion.
         end if -- huom. virheen sattuessa ei viestejä.
         if the_items is not {} then -- kohteita on valittuina Finderissa.            
            set AppleScript's text item delimiters to ":"
            set the_probe to item 1 of the_items as text
            if the last character of the_probe is ":" then -- se on kansio.
               set the_chosen_folder to item the_probe of application "Finder"
            else -- se on tiedosto.
               if class of item 1 in the_items is application file then -- se on ohjelmatiedosto.
                  set the_count to number of text items in the_probe
                  set the_probe to text items 1 through (the_count - 1) of the_probe as text
                  set the_chosen_folder to item the_probe ¬
                     of application "Finder" -- ohjelmatiedoston kansio valittu.
               else -- se ei ole ohjelmatiedosto.
                  set the_chosen_folder to (choose folder with prompt ¬
                     "Valitse kansio, jossa on ohjelmia tai ohjelmakansioita:")
               end if
            end if
         else
            set the_chosen_folder to (choose folder with prompt ¬
               "Valitse kansio, jossa on ohjelmia tai ohjelmakansioita:")
         end if
         set the_list to every application file in the_chosen_folder -- jokainen ohjelma valitussa kansiossa.
         set folder_list to every folder in the_chosen_folder -- jokainen alakansio valitussa kansiossa.
         repeat with the_app in the_list
            my make_alias(the_app, target_folder_name)
         end repeat
         repeat with sub_folder in folder_list
            try -- muuten roskakorista ja muista kuin käynnistyslevystä virheilmoituksia, jos työpöytäkansio valittuna.
               set the_list to every application file in sub_folder -- jokainen ohjelmatiedosto alakansiossa.
               repeat with the_app in the_list
                  my make_alias(the_app, target_folder_name)
               end repeat
            on error
            end try
         end repeat
      end if
      if success is true then
         open folder target_folder_name of apple menu items folder of startup disk
         set selected_window to window of folder target_folder_name of apple menu items folder of startup disk
         set view of selected_window to 2
         set popup of selected_window to true -- vain systeemissä 8.
         set pulled open of selected_window to true -- vain systeemissä 8.
      else -- yhtään aliasta ei luotu.
         if (exists of folder target_folder_name of apple menu items folder of startup disk) is true then
            set target_folder_contents to every item in folder target_folder_name of apple menu items folder of startup disk
            if target_folder_contents is {} then
               delete folder target_folder_name of apple menu items folder of startup disk
            end if
         end if
         if (exists of folder target_folder_name of apple menu items folder of startup disk) is true then
            set target_folder_contents to every item in folder target_folder_name of apple menu items folder of startup disk
            if target_folder_contents is {} then
               delete folder target_folder_name of apple menu items folder of startup disk
            end if
         end if
         display dialog ¬
            "Yhtään aliasta ei luotu." buttons "OK" default button 1
      end if
   end tell
end ApplicationAlias

on make_alias(the_app, target_folder_name)
   tell application "Finder"
      try -- Finderin voi olla mahdotonta tulkita vanhojen ohjelmien ehdotettua kokoa.
         set s_size to suggested size of the_app
         if s_size is greater than 500000 then
            make new alias file at folder target_folder_name ¬
               of apple menu items folder of startup disk to the_app
            set success to true
         end if
      on error
         return "Ohjelman " & name of the_app & " ehdotettu muistikoko puuttuu. Jatkan."
      end try
   end tell
end make_alias