-- bbedit_Utilities
-----------------------------------
-- PROPERTIES:
--!memberProperties: [#name: "bbedit_Utilities", #scripttype: #parent, #scriptSyntax: #lingo, #comments: "~/Documents/Scripts/lingo/commonMovieScript.ls"]
--
-- DESCRIPTION:
--               This script externalizes all functions which are related to the use of an external editor for script editing
--               It started as interface to BBEdit, hence the name, later I added support for arbitrary editors
--               as there is not BBEdit for windows. I tested with UltraEdit and Tortoise SVN for the merge/diff functionality
--               I'd appreciate any enhencements made for other external editors, but the above are the ones I use
--               and therefore I haven't tested against others
--
-- REQUIRES:
--               script "alexUtilities" -> ancestor -> basic function provider
--               most functionality is provided through the shell xtra and buddyApi xtra
--               although it should fail gracefully, if those are not present
--               not much will work without them
--
-- USAGE:
--               This script is instantiated and used in the "tell the stage" block and one of the functions
--               which are defined in script "OSCmenu_Utilities" and which was selected by the user
--               will be executed in "stage scope". That means, that references are made form within the stage context
--               e.g. member("foo") will refer to a member in one of the stages castlibs
--               sprite(x) will refere to sprite(x) in the stages score and so on
--
--               Please see the descriptions of many of those handlers at: http://www.farbflash.org/trac/HandlerMenu/wiki/utilityHandlers
-----------------------------------

property ancestor

on new me
  ancestor = new(script "alexUtilities")
  return me
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
on _______________BBEDIT_SCRIPTS end
end
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mOpen_Linked_Script_With_BBedit me, editor
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  editor = string(editor)
  if length(editor) < 1 then
    clistr = mFindUnixAppInPath(me, "bbedit")
    if length(clistr) < 1 then
      alert "BBedit not found in your path!"
      exit
    end if
  else
    clistr = QUOTE & editor & QUOTE
  end if
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)	
      if memref.type = #script then
        fname = memref.filename
        if fname.length then
          
          ppath = mConvertHFS2Unix(me, fname)
          -- ppath = mDoShellCmd(me, "osascript -e " & QUOTE & "return POSIX path of \" & QUOTE & fname & "\" & QUOTE & QUOTE)
          
          if charToNum(the last char of ppath) = 10 then delete the last char of ppath
          put " " & QUOTE & ppath & QUOTE after clistr
          
        else
          comm = line 1 of memref.comments
          if length(comm) > 0 then
            if char 1 of comm = "~" then put "$HOME" into char 1 of comm
            put " " & QUOTE & comm & QUOTE after clistr
          end if
        end if
      end if
    end repeat
  end repeat
  
  
  mDoShellCmd(me, clistr)
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mOpen_Linked_Script_With_ext_editor me
  editor = mGetExternalScriptEditor(me)
  if length(editor) < 1 then exit
  mOpen_Linked_Script_With_BBedit me, editor
end 

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mEdit_text_with_BBedit me
  mOpen_text_with_BBedit me, 1
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mOpen_text_with_BBedit me, doLiveEdit
  
  if not(the platform contains "mac") then
    alert "Sorry, BBEdit is a mac application!" & RETURN & "If you change this handler to match something appropriate for Windows, please drop me a note. alex@farbflash.de"
    exit
  end if
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  bbeditBinary = mFindUnixAppInPath(me, "bbedit")
  if length(bbeditBinary) < 1 then
    alert "BBEdit was not found in your PATH!"
    exit
  end if
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      
      memref = member(mem,cl)
      
      theText = ""
      theType = memref.type
      if theType = #script then
        theText = memref.scripttext
      else if theType = #text then
        theText = memref.html
      else if theType = #field then
        theText = memref.text
      end if
      
      if length(theText) > 0 then
        
        if doLiveEdit then
          ---------------------
          -- create a temporary file from scripttext:
          tempSrcHFSPath = mGetTempFilePath(me, "temp_BBEdit_file.ls")
          tempSrcPath = mConvertHfs2Unix(me, tempSrcHFSPath)
          
          
          theResult = mSaveTextToTempFile(me, theText, tempSrcHFSPath)
          ---------------------
          
          if theResult = 0 then
            alert "Unable to save file:" && tempSrcHFSPath
          else
            mDoShellCmd(me, bbeditBinary && "--wait --resume " & tempSrcPath)
            
            theText = mGetTextFromFile(me, tempSrcHFSPath)
            
            if length(theText) < 1 then
              alert "Something seems wrong: the length of the new text is 0, nothing changed. If this was apparently an empty text, please delete by hand."
            else
              if theType = #script then
                memref.scripttext = theText
              else if theType = #text then
                memref.html = theText
              else if theType = #field then
                memref.text = theText
              end if
            end if
          end if
          
        else
          
          theName = memref.name
          if length(theName) < 1 then
            theName = "member_" & memref.membernum & "_" & memref.castlibnum
          end if
          
          tempSrcHFSPath = mGetTempFilePath(me, theName & ".ls")
          tempSrcPath = mConvertHfs2Unix(me, tempSrcHFSPath)
          
          
          theResult = mSaveTextToTempFile(me, theText, tempSrcHFSPath)
          
          if theResult = 0 then
            alert "Unable to save temp file:" && tempSrcHFSPath
          else
            mDoShellCmd(me, bbeditBinary && tempSrcPath)
          end if
          
        end if
        
      end if
    end repeat
  end repeat
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mEdit_text_in_ext_editor me
  mOpen_text_with_ext_editor me, 1
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetMacBinary me, editor
  if not(editor starts "/") then
    editor = mConvertHFS2Unix(me, editor)
  end if
  
  if the last char of editor = "/" then delete the last char of editor
  
  olddelim = the itemdelimiter
  the itemdelimiter = "/"
  
  appname = the last item of editor
  if offset(".app", appname) = length(appname) - 3 then
    infoplist = editor & "/Contents/Info.plist"
    exename = mDoShellCmd(me, "grep -A1 -E '<key>CFBundleExecutable<\/key>'" && QUOTE & infoplist & QUOTE & " | grep -vE '<key>CFBundleExecutable</key>' | cut -d\> -f2 | cut -d\< -f1")
    if the last char of exename = RETURN then delete the last char of exename
    if the last char of exename = numToChar(10) then delete the last char of exename
    editor = editor & "/Contents/MacOS/" & exename
  end if
  
  the itemDelimiter = olddelim
  
  return editor
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mOpen_text_with_ext_editor me, doLiveEdit
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  isMac = (the platform contains "mac")
  
  editor = mGetExternalScriptEditor(me)
  if length(editor) < 1 then exit
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      
      memref = member(mem,cl)
      
      theText = ""
      theType = memref.type
      if theType = #script then
        theText = memref.scripttext
      else if theType = #text then
        theText = memref.html
      else if theType = #field then
        theText = memref.text
      end if
      
      if length(theText) > 0 then
        
        
        if doLiveEdit then
          ---------------------
          tempSrcHFSPath = mGetTempFilePath(me, "temp_LingoScript_file.ls")
          
          if isMac then
            waitparam = ""
            editor = mGetMacBinary(me, editor)
            tempSrcPath = mConvertHfs2Unix(me, tempSrcHFSPath)
          else
            waitparam = " /wait"
            tempSrcPath = tempSrcHFSPath
          end if
          ---------------------
          
          theResult = mSaveTextToTempFile(me, theText, tempSrcHFSPath)
          
          if theResult = 0 then
            alert "Unable to save temp file:" && tempSrcHFSPath
          else
            
            mDoShellCmd(me, QUOTE & editor & QUOTE && tempSrcPath & waitparam)
            
            theText = mGetTextFromFile(me, tempSrcHFSPath)
            
            if length(theText) < 1 then
              alert "Temp file:" && tempSrcHFSPath && "contains no text. Changes will not be written to script."
              
            else
              
              theText = mForceMacLineBreaks(me, theText)
              
              if theType = #script then
                memref.scripttext = theText
              else if theType = #text then
                memref.html = theText
              else if theType = #field then
                memref.text = theText
              end if
              
            end if
            
          end if
          
          
        else
          
          theName = memref.name
          if length(theName) < 1 then
            theName = "member_" & memref.membernum & "_" & memref.castlibnum
          end if
          ---------------------
          -- create a temporary file from scripttext:
          tempSrcHFSPath = mGetTempFilePath(me, theName & ".ls")
          
          if isMac then
            waitparam = " &"
            editor = mGetMacBinary(me, editor)
            tempSrcPath = mConvertHfs2Unix(me, tempSrcHFSPath)
          else
            waitparam = ""
            tempSrcPath = tempSrcHFSPath
          end if
          
          theResult = mSaveTextToTempFile(me, theText, tempSrcHFSPath)
          ---------------------
          
          if theResult = 0 then
            alert "Unable to save temp file:" && tempSrcHFSPath
          else
            mDoShellCmd(me, QUOTE & editor & QUOTE && tempSrcPath & waitparam)
          end if
          
        end if
        
      end if
    end repeat
  end repeat
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mReImport_text_Opened_In_ext_editor me
  mReImport_text_Opened_In_BBedit me
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mReImport_text_Opened_In_BBedit me
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      
      memref = member(mem,cl)
      
      theName = memref.name
      if length(theName) < 1 then
        theName = "member_" & memref.membernum & "_" & memref.castlibnum
      end if
      
      tempSrcPath = mGetTempFilePath(me, theName & ".ls")
      
      theText = mGetTextFromFile(me, tempSrcPath)
      
      if length(theText) > 0 then
        
        theText = mForceMacLineBreaks(me, theText)
        
        case memref.type of
          #script: memref.scripttext = theText
          #text: memref.html = theText
          #field: memref.text = theText
        end case
        
      else
        alert "Temp file:" && tempSrcPath && "contains no text. Changes will not be written to script."
        
      end if
      
    end repeat
  end repeat
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mForceMacLineBreaks me, theText
  rettext = ""
  olddelim = the itemdelimiter
  the itemdelimiter = numToChar(10)
  thirteen = numToChar(13)
  num = the number of items of theText
  repeat with n = num down to 1
    i = item n of theText
    put i after rettext
    if the last char of i <> thirteen then put thirteen after rettext
  end repeat
  the itemdelimiter = olddelim
  return rettext
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mCompareSelectedScriptmembersBatch me
  
  if not(the platform contains "mac") then
    alert "Sorry, BBEdit is a mac application!" & RETURN & "If you change this handler to match something appropriate for Windows, please drop me a note. alex@farbflash.de"
    exit
  end if
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  bbeditBinary = mFindUnixAppInPath(me, "bbedit")
  if length(bbeditBinary) < 1 then
    alert "BBEdit was not found in your PATH!"
    exit
  end if
  
  bbdiffpath = mFindUnixAppInPath(me, "bbdiff")
  if length(bbdiffpath) < 1 then
    alert "Consider installing BBEdits command line tools. If we could use bbdiff it would be way nicer."
  else
    if mCheckForXtra(me, "FileIO") = 0 then
      alert "FileIO Xtra missing!"
      exit
    end if
  end if
  
  textlist = []
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  memberlist = []
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memberlist.add(member(mem,cl))
    end repeat
  end repeat
  
  cnt = count(memberlist)
  if (cnt mod 2) or (cnt = 0) then
    alert "Must be an even number of selected members"
    exit
  end if
  cnt = cnt/2
  
  doit = 0
  
  repeat with n = 1 to cnt
    
    theText = ""
    
    memref = memberlist[n]
    if memref.type = #script then
      theText = memref.scripttext
    else if [#text, #field].getPos(memref.type) > 0 then
      theText = memref.text
    end if
    
    
    
    if length(theText) > 0 then
      
      
      fname = memref.name
      if length(fname) > 24 then
        fname = fname.char[1 .. 20] & "..." & fname.char[length(fname)]
      end if
      fname = fname & "___" & memref.membernum
      
      
      theText2 = ""
      
      memref2 = memberlist[n+cnt]
      if memref2.type = #script then
        theText2 = memref2.scripttext
      else if [#text, #field].getPos(memref2.type) > 0 then
        theText2 = memref2.text
      end if
      
      if length(theText2) > 0 then
        
        name1 = memref.name
        if length(name1) < 1 then name1 = string(memref)
        name2 = memref2.name
        if length(name2) < 1 then name2 = string(memref2)
        
        if theText = theText2 then
          
          put "Members " & name1 & " and " & name2 & " are identical"
          
        else
          
          put "Members " & name1 & " and " & name2 & " differ"
          
          if length(bbdiffpath) < 1 then
            
            --------------------------------------------- the old version created new documents in bbedit and I selected "diff" for each from the menu
            --------------------------------------------- bbdiff is cooler ;-)
            oldclipboardNum = new(#field)
            oldclipboardNum.pasteClipBoardInto()
            
            neuer = new(#field)
            
            neuer.text = theText
            copyToClipBoard(neuer)
            mDoShellCmd(me, "pbpaste |" && bbeditBinary)
            
            
            neuer.text = theText2
            copyToClipBoard(neuer)
            mDoShellCmd(me, "pbpaste |" && bbeditBinary)
            
            
            
            if oldclipboardNum.type <> #empty then copyToClipBoard(oldclipboardNum)
            oldclipboardNum.erase()
            neuer.erase()
            --------------------------------------------
            
          else
            
            if doit = 0 then
              
              --              tempSrcPath = mDoShellCmd(me, "mktemp -d -t temp_BBDiff_folder1", 1)
              --              tempSrcPath = tempSrcPath[1] & "/"
              
              tempSrcPath = "/tmp/temp_BBDiff_folder1/"
              
              mDoShellCmd(me, "mkdir -p " & tempSrcPath)
              mDoShellCmd(me, "rm -r " & tempSrcPath)
              
              -- tempSrcHFSPath = mDoShellCmd(me, "osascript -e" && QUOTE & "return POSIX file \" & QUOTE & tempSrcPath & "\" & QUOTE & " as string" & QUOTE, 1)
              -- tempSrcHFSPath = tempSrcHFSPath[1]
              tempSrcHFSPath = mConvertUnix2Hfs(me, tempSrcPath)
              
              
              --              tempTgtPath = mDoShellCmd(me, "mktemp -d -t temp_BBDiff_folder2", 1)
              --              tempTgtPath = tempTgtPath[1] & "/"
              
              tempTgtPath = "/tmp/temp_BBDiff_folder2/"
              
              mDoShellCmd(me, "mkdir -p " & tempTgtPath)
              mDoShellCmd(me, "rm -r " & tempTgtPath)
              
              --tempTgtHFSPath = mDoShellCmd(me, "osascript -e" && QUOTE & "return POSIX file \" & QUOTE & tempTgtPath & "\" & QUOTE & " as string" & QUOTE, 1)
              --tempTgtHFSPath = tempTgtHFSPath[1]
              tempTgtHFSPath = mConvertUnix2Hfs(me, tempTgtPath)
              
              doit = 1
            end if
            
            mSaveTextToTempFile(me, theText, tempSrcHFSPath & fname)
            mSaveTextToTempFile(me, theText2, tempTgtHFSPath & fname)
            
          end if
          
        end if
        
      else
        put "skipping" && memref && "and" && memref2 && "because the length of the text of" && memref2 && "is 0"
      end if
      
    else
      put "skipping" && memref && "because the length of the text is 0"
    end if
    
  end repeat
  
  --  put "mCompareSelectedScriptmembers 7"
  
  if doit = 1 then
    --    put bbdiffpath && QUOTE & tempSrcPath & QUOTE && QUOTE & tempTgtPath & QUOTE
    
    mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE && QUOTE & tempSrcPath & QUOTE && QUOTE & tempTgtPath & QUOTE)
    
  else
    
    --    put "doit = 0"
    
    if length(bbdiffpath) > 0 then
      alert "No differences found"
    end if
    
  end if
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


on mBBDiff me, memref1, memref2
  
  memref1 = member(memref1)
  
  
  if not(the platform contains "mac") then
    alert "Sorry, BBEdit is a mac application!" & RETURN & "If you change this handler to match something appropriate for Windows, please drop me a note. alex@farbflash.de"
    exit
  end if
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  
  bbdiffpath = mFindUnixAppInPath(me, "bbdiff")
  if length(bbdiffpath) < 1 then
    alert "This function only works with BBDiff, a command line tool provided by BBEdit. Consider installing BBEdits command line tools, it is really helpful."
    exit
  end if
  
  ---------------------
  -- create a temporary file from scripttext:
  
  tempSrcHFSPath = mGetTempFilePath(me, "temp_BBDiff_file1.ls")
  tempSrcPath = mConvertHfs2unix(me, tempSrcHFSPath)
  
  st1 = memref1.scripttext
  theResult = mSaveTextToTempFile(me, st1, tempSrcHFSPath)
  
  if theResult = 0 then
    alert "Problems saving file:" && tempSrcHFSPath && "Can't proceed"
    exit
  end if
  ---------------------
  
  
  
  if not(voidP(memref2)) then memref2 = member(memref2)
  
  ----------- there are 2 options for this handler:
  
  if ilk(memref2) = #member then -- option 1: we have a second member and diff against the second member
    
    st2 = memref2.scripttext
    if st1 = st2 then
      put "identical scripttext"
      exit
    end if
    
    ---------------------
    -- create a temporary file from scripttext:
    tempSrcHFSPath2 = mGetTempFilePath(me, "temp_BBDiff_file2.ls")
    tempSrcPath2 = mConvertHfs2unix(me, tempSrcHFSPath2)
    
    comm = tempSrcPath2
    
    theResult = mSaveTextToTempFile(me, st2, tempSrcHFSPath2)
    if theResult = 0 then
      alert "Problems saving file:" && tempSrcHFSPath && "Can't proceed"
      exit
    end if
    ---------------------
    
  else -- option 2: we have NO second member and diff against the file at the path in the member comments (=> alex linked scripts UNIX(!) path)
    comm = line 1 of memref1.comments
    
  end if
  
  
  
  if comm.length then -- comm is our second file, we only diff, if there is either a second member OR a path in member.comments
    
    if char 1 of comm = "~" then
      put mDoShellCmd(me, "echo -n $HOME") into char 1 of comm
    end if
    
    mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE & " --ignore-curly-quotes --ignore-spaces --wait --resume " &QUOTE& tempSrcPath &QUOTE&&QUOTE& comm &QUOTE)
    
    -- since we used the --wait and the --resume switch, we will only come to this line AFTER the diff process in bbedit is finished
    
    
    -------- now write the results of the BBEdit diff back into the script members
    memref1.scripttext = mGetTextFromFile(me, tempSrcHFSPath)
    
    if ilk(memref2) = #member then
      memref2.scripttext = mGetTextFromFile(me, tempSrcHFSPath2)
    end if
    
    
  else
    
    alert "member has no comments!"
    
  end if
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mBBDiff_LiveCompare_SelectedScript me
  cl = the activecastlib
  sel = the selection of castlib cl
  if count(sel) > 0 then
    memref1 = member(sel[1][1], cl)
    if sel[1][2] <> sel[1][1] then
      memref2 = member(sel[1][2], cl)
    else
      if count(sel) < 2 then
        alert "Please select two castmembers!"
        exit
      end if
      memref2 = member(sel[2][1], cl)
    end if
    mBBDiff me, memref1, memref2
  end if
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mLiveCompare_SelectedScript me, convertLineBreaksToUnix
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  if count(sel) > 0 then
    memref1 = member(sel[1][1], cl)
    if sel[1][2] <> sel[1][1] then
      memref2 = member(sel[1][2], cl)
    else
      if count(sel) < 2 then
        alert "Please select two castmembers!"
        exit
      end if
      memref2 = member(sel[2][1], cl)
    end if
  else
    alert "Please select two castmembers!"
    exit
  end if
  
  memtype = memref1.type
  if [#script, #field, #text].getPos(memtype) < 1 then
    alert "The members must be either #script, #field or #text members!"
    exit
  else
    case memtype of
        
        -----------------------
      #script:
        ext1 = ".ls"
        st1 = memref1.scripttext
        
        -----------------------
      #text:
        ext1 = ".html"
        st1 = memref1.html
        
        -----------------------
      otherwise:
        ext1 = ".txt"
        st1 = memref1.text
        
    end case
    
  end if
  
  memtype = memref2.type
  if [#script, #field, #text].getPos(memtype) < 1 then
    alert "The members must be either #script, #field or #text members!"
    exit
  else
    case memtype of
        
        -----------------------
      #script:
        ext2 = ".ls"
        st2 = memref2.scripttext
        
        -----------------------
      #text:
        ext2 = ".html"
        st2 = memref2.html
        
        -----------------------
      otherwise:
        ext2 = ".txt"
        st2 = memref2.text
        
    end case
  end if
  
  if not(the platform contains "mac") then
    
    bbdiffpath = mGetSVNDiffBinaryPath(me)
    
    if length(bbdiffpath) < 1 then exit
    
    isMac = 0
  else
    isMac = 1
    bbdiffpath = mFindUnixAppInPath(me, "bbdiff")
    if length(bbdiffpath) < 1 then
      alert "This function only works with BBDiff, a command line tool provided by BBEdit. Consider installing BBEdits command line tools, it is really helpful."
      exit
    end if
    
  end if
  
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  
  name1 = memref1.name & "_m" & memref1.memberNum & "_c" & memref1.castlibnum
  fname1 = mGetTempFilePath(me, name1 & ext1)
  
  name2 = memref2.name & "_m" & memref2.memberNum & "_c" & memref2.castlibnum
  fname2 = mGetTempFilePath(me, name2 & ext2)
  
  ------------------------------------------ trac subversion support works better with unix linebreaks...
  if voidP(convertLineBreaksToUnix) then convertLineBreaksToUnix = 1
  if convertLineBreaksToUnix = "" then convertLineBreaksToUnix = 1
  
  if convertLineBreaksToUnix = 1 then
    st1 = mConvertLineBreaksToUnix(me, st1)
    st2 = mConvertLineBreaksToUnix(me, st2)
  end if
  -----------------------------------------
  
  
  theResult = mSaveTextToTempFile(me, st1, fname1)
  theResult = mSaveTextToTempFile(me, st2, fname2)
  ---------------------
  
  
  if isMac then
    
    
    theResult = mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE & " --ignore-curly-quotes --ignore-spaces --wait --resume " &QUOTE& mConvertHFS2Unix(me, fname1) &QUOTE&&QUOTE& mConvertHFS2Unix(me, fname2) &QUOTE && "2>&1", RETURN, 0, 1)
    
    -- since we used the --wait and the --resume switch, we will only come to this line AFTER the diff process in bbedit is finished
    
    if count(theResult) > 0 then
      if theResult[1] contains "no such file or directory" then
        put theResult[1]
      else
        put "No differences found for member: " & memref1.name && "(" & memref1 & ") and " & memref2.name && "(" & memref2 & ")"
      end if
      writeBack = 0
      
    else
      
      -------- now write the results of the BBEdit diff back into the script members
      st1 = mGetTextFromFile(me, fname1)
      st2 = mGetTextFromFile(me, fname2)
      
      writeBack = 1
      
    end if
    
    ---------------------- windows
  else
    
    
    theResult = mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE && QUOTE & fname1 &QUOTE&&QUOTE& fname2 &QUOTE, RETURN, 0, 0, 0)
    
    -------- now write the results of the BBEdit diff back into the script members
    st1 = mGetTextFromFile(me, fname1)
    st2 = mGetTextFromFile(me, fname2)
    
    writeBack = 1
  end if
  
  
  if writeBack = 1 then
    
    
    if convertLineBreaksToUnix = 1 then
      st1 = mConvertLineBreaksToMac(me, st1)
      st2 = mConvertLineBreaksToMac(me, st2)
    end if
    
    memtype = memref1.type
    case memtype of
        -----------------------
      #script:
        memref1.scripttext = st1
        
        -----------------------
      #text:
        memref1.html = st1
        
        -----------------------
      otherwise:
        memref1.text = st1
        
    end case
    
    memtype = memref2.type
    case memtype of
        -----------------------
      #script:
        memref2.scripttext = st2
        
        -----------------------
      #text:
        memref2.html = st2
        
        -----------------------
      otherwise:
        memref2.text = st2
        
    end case
    
  end if
  
  
end





-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
on _______________LINKED_SCRIPTS_HANDLERS me
end
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


-- LINKED SCRIPTS
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- refresh the linked member after the source of the linked script was changed

on mRefreshScripts me
  
  CurrentOSXUserName = ""
  isMac = (the platform contains "mac")
  if isMac then CurrentOSXUserName = mGetCurrentOSXUserName(me)
  
  cl = the activecastlib
  sel = the selection of castlib cl
  anz = sel.count
  repeat with n = 1 to anz
    repeat with m = sel[n][1] to sel[n][2]
      memref = member(m, cl)
      if length(memref.filename) then
        memref.filename = memref.filename
      else
        if memref.type = #script then
          aPath = mGetFilePathFromMemberComments(me, memref, isMac, CurrentOSXUserName)
          if length(aPath) > 0 then
            dertext = mGetTextFromFile(me, aPath)
            if length(dertext) > 0 then
              put "updating scripttext of member:" memref.name && "(" & memref & ")"
              memref.scripttext = dertext
            end if
          end if
        end if
      end if
    end repeat
  end repeat
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- unlink all linked scripts from all castlibs (when transferring a movie to another machine or before publishing)

on mUnLinkAllScripts me
  cnum = the number of castlibs
  repeat with l = 1 to cnum
    mnum = the number of members of castlib l
    repeat with n = 1 to mnum
      memref = member(n, l)
      if memref.type = #script then
        if (memref.linked = 1) then memref.linked = 0
      end if
    end repeat
  end repeat
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- unlink the selected scriptmembers

on mUnLinkScripts me
  cl = the activecastlib
  sel = the selection of castlib cl
  anz = sel.count
  repeat with n = 1 to anz
    repeat with m = sel[n][1] to sel[n][2]
      memref = member(m, cl)
      if memref.type = #script then
        if (memref.linked = 1) then memref.linked = 0
      end if
    end repeat
  end repeat
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetUserHomeFolderPath me
  isMac = (the platform contains "mac")
  if isMac then
    CurrentOSXUserName = mGetCurrentOSXUserName(me)
    return "/Users/" & CurrentOSXUserName
  end if
  
  homedir = baSysFolder("personal")
  old = the itemdelimiter
  the itemdelimiter = "\"
  delete the last item of homedir
  delete the last item of homedir
  the itemdelimiter = old
  return homedir
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- get the absolute pathname of alex linked library script from members comment
-- memref is required => member reference
-- isMac and CurrentOSXUserName is optional and will be computed if not provided
-- in repeat loops provide these values to avoid recomputing them on each iteration

on mGetFilePathFromMemberComments me, memref, isMac, CurrentOSXUserName
  
  aPath = ""
  
  mp = the moviepath
  if length(mp) < 1 then mp = the applicationpath
  delim = the last char of mp
  olddelim = the itemdelimiter
  the itemdelimiter = delim
  
  if voidP(isMac) then isMac = (the platform contains "mac")
  if isMac then
    if voidP(CurrentOSXUserName) then
      CurrentOSXUserName = mGetCurrentOSXUserName(me)
    end if
  end if
  
  comm = line 1 of memref.comments
  if comm.length > 0 then
    
    
    if comm starts "~" then -- OSX current user directory
      
      put mGetUserHomeFolderPath(me) into char 1 of comm
      aPath = comm
      --      if isMac then
      --        put "/Users/"&CurrentOSXUserName into char 1 of comm
      --      else
      --        offs = offset("/", comm)
      --        repeat while offs > 0
      --          put "\" into char offs of comm
      --          offs = offset("/", comm)
      --        end repeat
      --        put baSysFolder("personal") into char 1 to 2 of comm
      --        aPath = comm
      --      end if
    end if
    
    if comm starts "/" then -- OSX/Unix style path
      
      if isMac then
        hdname = getOsDirectory()
        hdname = hdname.item[1]
        the itemdelimiter = "/"
        delete item 1 of comm
        itemCnt = comm.item.count
        repeat with i = 1 to itemCnt
          put delim & comm.item[i] after hdname
        end repeat
        
        aPath = hdname
        
      else
        
        if comm starts "/Users/alex/" then
          put mGetUserHomeFolderPath(me) & "\" into char 1 to 12 of comm
        end if
        aPath = comm
        
      end if
      
      
    else if comm starts "@" then -- relative path with @
      
      if delim <> "" then
        offs = offset("", comm)
        repeat while offs > 0
          put delim into char offs of comm
          offs = offset("", comm)
        end repeat
      end if
      
      if delim <> "\" then
        offs = offset("\", comm)
        repeat while offs > 0
          put delim into char offs of comm
          offs = offset("\", comm)
        end repeat
      end if
      
      offs = offset("/", comm)
      repeat while offs > 0
        put delim into char offs of comm
        offs = offset("/", comm)
      end repeat
      
      delete char 1 of comm
      if char 1 of comm = delim then delete char 1 of comm
      
      if char 1 of comm = delim then
        delete the last item of mp
        repeat while char 1 of comm = delim
          delete the last item of mp
          delete char 1 of comm
        end repeat
        put delim after mp
        aPath = mp & comm
      end if
      
    else -- absolute path
      aPath = comm
    end if
    
  end if
  
  the itemdelimiter = olddelim
  
  return aPath
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- link the selected scriptmembers to the files specified in the first line of the members comments

on mLinkScripts me
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  mp = the moviepath
  if length(mp) < 1 then mp = the applicationpath
  delim = the last char of mp
  olddelim = the itemdelimiter
  the itemdelimiter = delim
  
  isMac = (delim = ":")
  if isMac then CurrentOSXUserName = mGetCurrentOSXUserName(me)
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)    
      if memref.type = #script then
        memname = memref.name
        comm = line 1 of memref.comments
        if comm.length then
          
          aPath = mGetFilePathFromMemberComments(me, memref, isMac, CurrentOSXUserName)
          
          fio = (xtra "fileio").new()
          if objectP(fio) then
            fio.openFile(aPath, 0)
            if fio.status() = 0 then
              fio.closeFile()
              
              scomm = memref.comments
              stype = memref.scripttype
              sthumb = memref.thumbnail
              --                memref.linkAs(aPath)
              memref.filename = aPath
              
              memref.thumbnail = sthumb
              memref.scripttype = stype
              memref.comments = scomm 
              
            end if
          end if
          fio = 0
          
          
        else
          
          if memname.length then
            
            hdname = getOsDirectory()
            anfp = hdname.item[1] & delim & "Users"& delim & CurrentOSXUserName & delim & "Documents"& delim
            aPath = anfp & "Scripts"& delim & "lingo"& delim & memname & ".ls"
            
            fio = (xtra "fileio").new()
            if objectP(fio) then
              fio.openFile(aPath, 0)
              if fio.status() = 0 then
                fio.closeFile()
                
                
                stype = memref.scripttype
                sthumb = memref.thumbnail
                
                memref.filename = aPath
                
                memref.thumbnail = sthumb
                memref.scripttype = stype
                
                --                memref.comments = "/Users/"&mGetCurrentOSXUserName()&"/Documents/Scripts/lingo/" & memname & ".ls"
                memref.comments = "~/Documents/Scripts/lingo/" & memname & ".ls"
                
              else
                
                aPath = anfp & "ALIEN"& delim & "myLinkedScripts"& delim & memname & ".ls"
                
                fio.openFile(aPath, 0)
                if fio.status() = 0 then
                  fio.closeFile()
                  
                  
                  stype = memref.scripttype
                  sthumb = memref.thumbnail
                  
                  memref.filename = aPath
                  
                  memref.thumbnail = sthumb
                  memref.scripttype = stype
                  --                  memref.comments = "/Users/"&mGetCurrentOSXUserName()&"/Documents/ALIEN/myLinkedScripts/" & memname & ".ls"
                  memref.comments = "~/Documents/ALIEN/myLinkedScripts/" & memname & ".ls"
                  
                else
                  put "member: " & memname && "doesn't have a comment nor a linked script with the member name !"
                end if
                
                
              end if
            end if
            fio = 0
            
          end if
          
        end if
      end if
    end repeat
  end repeat
  
  the itemdelimiter = olddelim
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetCurrentOSXUserName me
  
  if mCheckForXtra(me, "FileXtra4") = 1 then
    xtraInstance = new(xtra "FileXtra4")
    theUser = xtraInstance.fx_FolderGetSpecialPath("kCurrentUserFolderType")
    xtraInstance = 0
    
    olddelim = the itemdelimiter
    the itemdelimiter = the last char of the applicationpath
    delete the last item of theUser
    theUser = the last item of theUser
    the itemdelimiter = olddelim
    
  else if mCheckForXtra(me, "Shell") = 1 or mCheckForXtra(me, "ff_shell") = 1 then
    theUser = mDoShellCmd(me, "whoami")
    if chartonum(the last char of theUser) = 10 then delete the last char of theUser
    
  else
    theUser = ""
    
  end if
  
  return theUser
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- compare selected scripts to the .ls script which is defined in their members comments
-- if a difference is found some lines will get put into the message window to open the script in bbedit to do a file compare

on mCheckLSChange me
  
  
  bbdiffpath = ""
  if the platform contains "mac" then
    
    if mCheckForXtra(me, "Shell") = 1 or mCheckForXtra(me, "ff_shell") = 1 then
      
      bbeditBinary = mFindUnixAppInPath(me, "bbedit")
      
      bbdiffpath = mFindUnixAppInPath(me, "bbdiff")
      if length(bbdiffpath) < 1 then
        alert "Consider installing BBEdits command line tools. If we could use bbdiff it would be way nicer."
      end if
    end if
    
    if length(bbdiffpath) > 0 then
      if mCheckForXtra(me, "FileIO") = 0 then
        alert "FileIO Xtra missing!"
        exit
      end if
    end if
    
  end if
  
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  mp = the moviepath
  if not mp.length then mp = the applicationpath
  delim = the last char of mp
  olddelim = the itemdelimiter
  the itemdelimiter = delim
  
  isMac = (the platform contains "mac")
  if isMac then CurrentOSXUserName = mGetCurrentOSXUserName(me)
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)    
      if memref.type = #script then
        memname = memref.name
        comm = line 1 of memref.comments
        
        if length(comm) < 1 then
          -- put memref.name && "(" & memref & ") has no comments - not linked" 
        else
          
          aPath = mGetFilePathFromMemberComments(me, memref, isMac, CurrentOSXUserName)
          
          if length(aPath) < 1 then
            -- put memref.name && "(" & memref & ") has no comments - not linked" 
          else
            
            fio = (xtra "fileio").new()
            
            if objectP(fio) then
              
              fio.openFile(aPath, 0)
              
              if fio.status() <> 0 then
                put memref.name && "(" & memref & "): Couldn't open file:" && aPath && "fileIO error:" && fio.error(fio.status())
              else
                
                vergText = fio.readFile()
                fio.closeFile()
                memscr = memref.scripttext
                
                if vergText = memscr then
                  put memref.name && "(" & memref & ") has no changes"
                else
                  
                  isEqual = 0
                  if mCheckForXtra(me, "Pregex") = 1 then
                    
                    memscrLi = [memscr]
                    pregex_replace(memscrLi, "\s", "ig", "")
                    memscrLi = memscrLi[1]
                    
                    vergTextLi = [vergText]
                    pregex_replace(vergTextLi, "\s", "ig", "")
                    vergTextLi = vergTextLi[1]
                    
                    isEqual = (memscrLi = vergTextLi)
                  end if
                  
                  if isEqual <> 0 then
                    put memref.name && "(" & memref & ") has no changes"
                  else
                    
                    if length(bbdiffpath) < 1 then
                      
                      
                      if not(the platform contains "mac") then
                        put "-------------------------------------------------------"
                        put RETURN&"neuer = new(#field)"&RETURN&"neuer.text = "&memref&".scripttext"&RETURN&"neuer.copyToClipBoard()"&RETURN&"neuer.erase()"&RETURN
                        if length(bbeditBinary) > 0 then
                          put RETURN&"shell_cmd("&QUOTE&"PBPaste |" && bbeditBinary && ";" && bbeditBinary && line 1 of memref.comments & QUOTE&")"&RETURN
                        else
                          put "BBEdit not found in PATH"
                        end if
                      else
                        
                        if (mCheckForXtra(me, "Shell") + mCheckForXtra(me, "ff_shell")) = 0  then
                          put "-------------------------------------------------------"
                          put RETURN&"neuer = new(#field)"&RETURN&"neuer.text = "&memref&".scripttext"&RETURN&"neuer.copyToClipBoard()"&RETURN&"neuer.erase()"&RETURN
                          if length(bbeditBinary) > 0 then
                            put RETURN&"shell_cmd("&QUOTE&"PBPaste |" && bbeditBinary && ";" && bbeditBinary && line 1 of memref.comments & QUOTE&")"&RETURN
                          else
                            put "BBEdit not found in PATH"
                          end if
                        else
                          
                          neuer = new(#field)
                          neuer.text = memscr
                          neuer.copyToClipBoard()
                          neuer.erase()
                          
                          comm = line 1 of memref.comments
                          if char 1 of comm = "~" then put "$HOME" into char 1 of comm
                          
                          
                          if length(bbeditBinary) > 0 then
                            mDoShellCmd(me, "PBPaste |" && bbeditBinary && ";" && bbeditBinary && comm)
                          else
                            put "BBEdit not found in PATH"
                          end if
                          
                        end if
                        
                      end if
                      
                    else
                      
                      tempSrcHFSPath = mGetTempFilePath(me, "temp_BBEdit_file.ls")
                      tempSrcPath = mConvertHfs2unix(me, tempSrcHFSPath)
                      
                      theResult = mSaveTextToTempFile(me, memref.scripttext, tempSrcHFSPath)
                      if theResult <> 0 then
                        
                        if char 1 of comm = "~" then put "$HOME" into char 1 of comm
                        
                        mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE & " --ignore-curly-quotes --ignore-spaces --wait --resume " &QUOTE& tempSrcPath &QUOTE&&QUOTE& comm &QUOTE)
                        
                        newscripttext = mGetTextFromFile(me, tempSrcHFSPath)
                        if length(newscripttext) then
                          memref.scripttext = mGetTextFromFile(me, tempSrcHFSPath)
                        else
                          alert "Something seems wrong: the length of the new text is 0, nothing was changed now. If this was puprosely an empty text, please delete by hand."
                        end if
                        
                      end if
                      
                      
                    end if
                    
                  end if
                  
                  
                end if
                
              end if
              
            end if
          end if
        end if
        fio = 0
        
      end if
      
    end repeat
  end repeat
  
  the itemdelimiter = olddelim
  
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

-- open the associated .ls, which is found in the member comments in bbedit
on mBBedit me
  
  if not(the platform contains "mac") then
    alert "Sorry, BBEdit is a mac application!" & RETURN & "If you change this handler to match something appropriate for Windows, please drop me a note. alex@farbflash.de"
    exit
  end if
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  clistr = mFindUnixAppInPath(me, "bbedit")
  if length(clistr) < 1 then
    alert "BBEdit was not found in your PATH!"
    exit
  end if
  
  -- home = mDoShellCmd(me, "echo -n $HOME")
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)    
      if memref.type = #script then
        memname = memref.name
        comm = line 1 of memref.comments
        if comm.length then
          if char 1 of comm = "~" then
            -- put home into char 1 of comm
            put "$HOME" into char 1 of comm
          end if
          put " "&comm after clistr
        end if
      end if
    end repeat
  end repeat
  
  mDoShellCmd(me, clistr)
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mExportListOfUsedLinkedScripts me
  
  str = ""
  clnum = the number of castlibs
  repeat with cl = 1 to clnum
    anz = the number of members of castlib cl
    repeat with n = 1 to anz
      memref = member(n, cl)
      if memref.type = #script then
        comm = line 1 of memref.comments
        if length(comm) > 0 then put "Membername:" && memref.name & TAB & "("&comm&")" & Return after str
      end if
    end repeat
  end repeat
  delete the last char of str
  
  mn = the moviename
  --  offs = offset(".", mn)
  --  if offs > 0 then mn = mn.char[1 .. offs-1]
  
  spl = mSplitPath(me, mn)
  
  mSaveToTextFile me, str, "", "Save list of used linked scripts", spl[#basename] & "_linkedScripts_List.txt"
  
end


