0 Replies Latest reply on Apr 5, 2011 11:17 PM by Bradley.Ni

    How Indesign CS3 scripting to handle CompositeFont?

    Bradley.Ni

      Hi Dears,

       

      I want to write a script to have below functions:

      1. List all documents and mappint with Asian fonts in a loclaized Chinese document from English.

      2. Save the font mapping as CompositeFonts, replace the English fonts with the compositefonts, then same as template.

      3. User can select a template in next time, the script will automatting apply the templates.

       

      there are two steps I can't success:

      1. save the font mapping as compositefonts. the scriptes can run without any error and I can find the correct compositefonts in compositefonts panel. The issue is I can't find all these fonts in ID font list. That means I can't apply these fonts manually in ID, and can't applt through script, it report no such font when use ChangeTextPreference method.

       

      2. When I save and open the document again, the combined fonts can appear in font list, but application report duplicate font names and add a "-1" into each compositefont.

       

      complete script

       


      Set Myindesign=createobject("Indesign.application.cs3")

      if myindesign.documents.count<1 then
        msgbox "没有打开的文档,请重试!"

      end if
      set mydocument=myindesign.activedocument
      dim myenglishfont()
      redim myenglishfont(mydocument.fonts.count-1)
      dim myfontlist()
      'redim myfontlist(myindesign.fonts.count-1)
      dim mydropdown()
      redim mydropdown(mydocument.fonts.count-1)
      i=1
      main
      sub main ()

      myDisplayDialog myInDesign, mydocument

      end sub

      Function myDisplayDialog(myInDesign, mydocument)
      on error resume next
          myObject = "None"
          Set myDialog = myInDesign.Dialogs.Add
          myDialog.Name = "FindChangeByList"
           With myDialog.DialogColumns.Add
               with .BorderPanels.add
         with .DialogColumns.Add
          With .DialogRows.Add
                      With .StaticTexts.Add
                              .StaticLabel = "英文字体"
                      End With
          End With
            
          for each myfont in mydocument.fonts

           With .DialogRows.Add
            set myEnglishfont(i-1)=.CheckboxControls.add
                       myEnglishfont(i-1).CheckedState=true
            myEnglishfont(i-1).staticLabel =myfont.name
            set mystatic=.StaticTexts.add
            mystatic.StaticLabel=myfont.fontstylename 
           end with
           i=i+1

          next
          

                
         end with


        end with
           End With

       


           With myDialog.DialogColumns.Add
               with .BorderPanels.add
         with .DialogColumns.Add
          With .DialogRows.Add
                      With .StaticTexts.Add
                              .StaticLabel = "亚洲语言字体"
                      End With
          End With
          n=1

          getfontlist

          for j=0 to i-2
           With .DialogRows.Add
                       set mydropdown(j)=.dropdowns.add
            mydropdown(j).stringlist=myfontlist
           End With

          next
         
               
         end with
        end with
           End With

      with mydialog.dialogcolumns.add

        with .dialogrows.add

         set mysavecombinedfonts=.CheckboxControls.add
         mysavecombinedfonts.StaticLabel="是否存储复合字体模版"
         mysavecombinedfonts.CheckedState=true
        
       
        end with

        with .dialogrows.add

         set mytemplatename=.TextEditboxes.add  
       
        end with

       

       

      end with

          myResult = myDialog.Show
          If myResult = True Then

           if mysavecombinedfonts.CheckedState=true then
        if mytemplatename.EditContents="" then
         msgbox "未指定模版的名字,自动存储名为“无名称”的模版,请到Adobe InDesign CS3\Fonts文件夹下手动修改"
         mytemplatename.EditContents="无名称"
        end if

        set fs=createobject("scripting.filesystemobject")
        if (fs.folderexists(myindesign.filepath & "\fonts")) then
         set foldr1=fs.getfolder(myindesign.filepath & "\fonts")
        else
         fs.createfolder myindesign.filepath & "\fonts"
         set foldr=fs.getfolder(myindesign.filepath & "\fonts")
        end if

        set foldr=fs.createfolder(myindesign.filepath & "\fonts\" & mytemplatename.EditContents)
        set fs=nothing
           end if

      for j=0 to ubound(myEnglishfont)

        if myEnglishfont(j).CheckedState=true then
       
         if mydropdown(j).selectedindex<0 then
          
          msgbox myEnglishfont(j).StaticLabel & "没有选择亚洲字体,此复合字体未保存"
         else
         
          applycombinedfont myEnglishfont(j).StaticLabel, myfontlist(mydropdown(j).selectedindex), mydocument, foldr
         end if
        end if
      next
          End If
      End Function

      sub applycombinedfont (EnglishFont, AsianFont, mydocument, foldr)

       

      Englishname1=left(myindesign.fonts(englishfont).fontfamily,4)
      Englishname2=myindesign.fonts(englishfont).fontstylename
      Asianname1=left(myindesign.fonts(AsianFont),4)
      Asianname2=myindesign.fonts(AsianFont).fontstylename
      set mycombinedfont=mydocument.CompositeFonts.add


      mycombinedfont.name=Englishname1 & Englishname2 & "-"&Asianname1&Asianname2


      mycombinedfont.CompositeFontEntries(1).appliedfont=myindesign.fonts(AsianFont).fontfamily

      mycombinedfont.CompositeFontEntries(1).fontstyle=myindesign.fonts(AsianFont).fontstylenam e


      mycombinedfont.CompositeFontEntries(2).appliedfont=myindesign.fonts(AsianFont).fontfamily

      mycombinedfont.CompositeFontEntries(2).fontstyle=myindesign.fonts(AsianFont).fontstylenam e

      mycombinedfont.CompositeFontEntries(3).appliedfont=myindesign.fonts(AsianFont).fontfamily
      mycombinedfont.CompositeFontEntries(3).fontstyle=myindesign.fonts(AsianFont).fontstylenam e

      mycombinedfont.CompositeFontEntries(4).appliedfont=myindesign.fonts(AsianFont).fontfamily
      mycombinedfont.CompositeFontEntries(4).fontstyle=myindesign.fonts(AsianFont).fontstylenam e

      mycombinedfont.CompositeFontEntries(5).appliedfont=myindesign.fonts(englishfont).fontfami ly
      mycombinedfont.CompositeFontEntries(5).fontstyle=myindesign.fonts(englishfont).fontstylen ame

      mycombinedfont.CompositeFontEntries(6).appliedfont=myindesign.fonts(englishfont).fontfami ly
      mycombinedfont.CompositeFontEntries(6).fontstyle=myindesign.fonts(englishfont).fontstylen ame

      set fs=createobject("scripting.filesystemobject")

      if (fs.folderexists(foldr)) then
        fs.createtextfile foldr & "\" & mycombinedfont.name & ".txt", true
        set mytxtfile=fs.OpenTextFile(foldr & "\" & mycombinedfont.name & ".txt",8,,-1)
        mytxtfile.writeline myindesign.fonts(AsianFont).fontfamily
        mytxtfile.writeline myindesign.fonts(AsianFont).fontstylename
        mytxtfile.writeline myindesign.fonts(englishfont).fontfamily
        mytxtfile.writeline myindesign.fonts(englishfont).fontstylename

       

      end if

      set fs=nothing

      end sub

      sub changefonts()

      myInDesign.FindTextPreferences = idNothingEnum.idNothing
      myInDesign.ChangeTextPreferences = idNothingEnum.idNothing
      myInDesign.FindChangeTextOptions.CaseSensitive = false
      myInDesign.FindChangeTextOptions.IncludeFootnotes = true
      myInDesign.FindChangeTextOptions.IncludeHiddenLayers = true
      myInDesign.FindChangeTextOptions.IncludeLockedLayersForFind = true
      myInDesign.FindChangeTextOptions.IncludeLockedStoriesForFind = true
      myInDesign.FindChangeTextOptions.IncludeMasterPages = true
      myInDesign.FindChangeTextOptions.WholeWord = false
      myInDesign.findTextPreferences.AppliedFont  = myindesign.fonts(englishfont).fontfamily
      myInDesign.findTextPreferences.FontStyle  = myindesign.fonts(englishfont).fontstylename
      msgbox "1"
      myInDesign.changeTextPreferences.AppliedFont  = mycombinedfont.name
      msgbox "2"
      mydocument.changeText

      myInDesign.FindTextPreferences = idNothingEnum.idNothing
      myInDesign.ChangeTextPreferences = idNothingEnum.idNothing

      end sub

      sub getfontlist ()

      on error resume next
      for m=1 to myindesign.fonts.count-1
        if myindesign.fonts(m).WritingScript>0 then

         redim preserve myfontlist(n-1)
         myfontlist(n-1)=myindesign.fonts(m).name
           
         n=n+1
        end if
          

      next
      err.clear
      On Error GoTo 0
      end sub