-- EditScript 1.0
--This script let's user to choose a script to edit from the folder of the frontmost application.
--To choose one of your own scripts and open it in the Script Editor, save this script as an application
--in your scripts folder.

-- Tuomas Rosberg <vanhentunut> 2000
-- This source code is free and in the public domain.
-- This source code is made available in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

on run
   tell application "Finder"
      set theScriptEditor to (application file id "ToyS") --the Script Editor
      --
      set allProcesses to every process
      repeat with aProcess in allProcesses
         if aProcess is frontmost then
            set thisScript to (get file of aProcess) --the application running this script
            exit repeat
         end if
      end repeat
      --
      set aFolder to folder of thisScript
      set theFiles to every file in aFolder
      set theList to {}
      set theAllowed to {"dplt", "aplt", "ToyS"} -- the creator types of Script Editor
      set avoidThese to {(theScriptEditor as alias), (thisScript as alias)} --to make it impossible to open it using it:-)
      repeat with x in theFiles
         if creator type of x is in theAllowed then
            if (x as alias) is not in avoidThese then
               set x to name of x as text
               set theList to theList & x
            end if
         end if
      end repeat
      --
      if theList is {} then
         display dialog ¬
            "There are no editable scripts in the folder of the frontmost application. " buttons "Sorry" default button 1
      else -- theList contains some scripts
         set aFolder to aFolder as string
         set theOnes to (choose from list theList with prompt ¬
            "Choose a script to edit:" OK button name "Edit" cancel button name "Cancel" with multiple selections allowed)
         if theOnes is not false then
            repeat with f in theOnes
               set thePath to aFolder & f
               set theFile to thePath as alias
               open theFile using theScriptEditor
            end repeat
         end if
      end if
      --
   end tell
end run