home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 February (Special) / Chip-Special_2006-02_Nero-7.bin / vypalovaci_utility / subtitle_workshop / SubtitleWorkshop251.exe / PascalScripts / Example.pas < prev    next >
Pascal/Delphi Source File  |  2004-02-10  |  12KB  |  221 lines

  1. // -------------------------------------------------------------------------- //
  2. //                  Subtitle Workshop - Pascal scripts sample                 //
  3. //                        Copyright ⌐ 2001-2004 URUSoft                       //
  4. //                           http://www.urusoft.net                           //
  5. //                                                                            //
  6. // This sample will go through all the subtitles in the file and will delete  //
  7. // all the ones which's text length is longer than 20 characters. This is     //
  8. // just a small sample of what you can do with Pascal Scripts in Subtitle     //
  9. // Workshop.                                                                  //
  10. //                                                                            //
  11. // This code uses only few of the functions available. A list of all the      //
  12. // available functions/procedures and their explanation is below. Just note   //
  13. // that in every function that use the "Num" parameter, it refers to the      //
  14. // subtitle number.                                                           //
  15. //                                                                            //
  16. // Declaration : function IsOriginalLoaded: Boolean;                          //
  17. // Use         : returns true if the original file is loaded                  //
  18. //                                                                            //
  19. // Declaration : function IsTranslatedLoaded: Boolean;                        //
  20. // Use         : returns true if the translated file is loaded                //
  21. //                                                                            //
  22. // Declaration : procedure EnableWorkArea;                                    //
  23. // Use         : enables the working area (when no file is loaded)            //
  24. //                                                                            //
  25. // Declaration : function GetSubtitleCount: Integer;                          //
  26. // Use         : retrieves the total number of subtitles that are loaded      //
  27. //                                                                            //
  28. // Declaration : function IsSubtitleSelected(const Num: Integer): Boolean;    //
  29. // Use         : returns true if a subtitle is selected, and false if not     //
  30. //                                                                            //
  31. // Declaration : function GetSubtitleText(const Num: Integer): String;        //
  32. // Use         : returns the text of a subtitle                               //
  33. //                                                                            //
  34. // Declaration : procedure SetSubtitleText(const Num: Integer;                //
  35. //                                         const Text: String);               //
  36. // Use         : sets the text of a specified subtitle to the value you pass  //
  37. //                                                                            //
  38. // Declaration : function GetSubtitleTrans(const Num: Integer): String;       //
  39. // Use         : returns the translation of a subtitle                        //
  40. //                                                                            //
  41. // Declaration : procedure SetSubtitleTrans(const Num: Integer;               //
  42. //                                          const Text: String);              //
  43. // Use         : sets the translation of a subtitle to the value you pass     //
  44. //                                                                            //
  45. // Declaration : function GetSubtitleInitialTime(const Num: Integer): Integer;//
  46. // Use         : returns the initial time of a subtitle, in milliseconds      //
  47. //                                                                            //
  48. // Declaration : procedure SetSubtitleInitialTime(const Num: Integer;         //
  49. //                                                const InitialTime: Integer);//
  50. // Use         : sets the initial time of a subtitle to the value you pass,   //
  51. //               in milliseconds                                              //
  52. //                                                                            //
  53. // Declaration : function GetSubtitleFinalTime(const Num: Integer): Integer;  //
  54. // Use         : returns the final time of a subtitle, in milliseconds        //
  55. //                                                                            //
  56. // Declaration : procedure SetSubtitleFinalTime(const Num: Integer;           //
  57. //                                              const FinalTime: Integer);    //
  58. // Use         : sets the final time of a subtitle to the value you pass,     //
  59. //               in milliseconds                                              //
  60. //                                                                            //
  61. // Declaration : procedure InsertSubtitle(const Pos: Integer;                 //
  62. //                                       const InitialTime,                   //
  63. //                                             FinalTime: Integer;            //
  64. //                                       const Text, Translation: String);    //
  65. // Use         : inserts a subtitle. Position is where you want to insert it. //
  66. //                                                                            //
  67. // Declaration : procedure DeleteSubtitle(const Num: Integer);                //
  68. // Use         : deletes the specified subtitle                               //
  69. //                                                                            //
  70. // Declaration : function MsgBox(const AMsg, BCap1, BCap2, BCap3: String;     //
  71. //                               const IconInd: Integer): Integer;            //
  72. // Use         : displays a message box. AMsg is the text. BCap1 is the       //
  73. //               text of the first button. BCap2 and BCap3 are the text of    //
  74. //               the second and third buttons respectively. If you want them  //
  75. //               to be hidden, just pass '' (blank string) as the parameter.  //
  76. //               IconInd is the icon index used by the MessageBox() function, //
  77. //               the constants are defined in Delphi's Windows.pas            //
  78. //               (eg. MB_ICONERROR, MB_ICONQUESTION, etc).                    //
  79. //                                                                            //
  80. // Date and time functions available:                                         //
  81. //                                                                            //
  82. //   function FormatDateTime(const Format: String;                            //
  83. //                           DateTime: TDateTime): String;                    //
  84. //   function GetYear: Integer;                                               //
  85. //   function MyGetMonth: Integer;                                            //
  86. //   function MyGetDay: Integer;                                              //
  87. //   function MyGetHour: Integer;                                             //
  88. //   function MyGetMinute: Integer;                                           //
  89. //   function MyGetSecond: Integer;                                           //
  90. //   function MyGetDate: String;                                              //
  91. //   function MyGetTime: String;                                              //
  92. //   function MyGetDateTime: String;                                          //
  93. //                                                                            //
  94. // -------------------------------------------------------------------------- //
  95.  
  96. program DeleteShortSubs;
  97.  
  98. // -----------------------------------------------------------------------------
  99.  
  100. const MB_ICONQUESTION = $00000020;
  101.  
  102. // -----------------------------------------------------------------------------
  103.  
  104. var
  105.   // We declarate the variables that are going to be our controls
  106.   Form : TForm;
  107.   Pnl  : TPanel;
  108.   Chk  : TCheckBox;
  109.   Btn1 : TButton;
  110.   Btn2 : TButton;
  111.  
  112. // -----------------------------------------------------------------------------
  113.  
  114. procedure Btn1Click(Sender: TObject);
  115. var
  116.   a: Integer;
  117. begin
  118.   for a := GetSubtitleCount-1 downto 0 do
  119.   begin
  120.     if Length(GetSubtitleText(a)) > 20 then
  121.     begin
  122.       if Chk.Checked then
  123.       begin
  124.         case MsgBox('Do you want to delete subtitle number ' + IntToStr(a) + '?', '&Yes', '&No', '&Cancel', MB_ICONQUESTION) of
  125.           1: DeleteSubtitle(a);
  126.           2: SetSubtitleText(a, 'YOU DECIDED NOT TO DELETE THIS SUBTITLE! :)');
  127.           3: exit;
  128.         end;
  129.       end else
  130.         DeleteSubtitle(a);
  131.     end;
  132.   end;
  133. end;
  134.  
  135. // -----------------------------------------------------------------------------
  136. var
  137.   n  : Integer;
  138. begin
  139.   // Enable working area if no file is loaded
  140.   if IsOriginalLoaded = False then EnableWorkArea;
  141.  
  142.   // "now" is a Delphi function to get current date and time
  143.   // For more information on "FormatDateTime" refer to Delphi's help
  144.   InsertSubtitle(0,1000,2000, 'We insert new subtitle, in position 0 (first node)', 'This is the translation');
  145.   InsertSubtitle(1,3000,4000, 'We insert new subtitle, in position 1 (second node)', 'This is the translation');
  146.   InsertSubtitle(2,5000,6000, FormatDateTime('c', now), 'This is the translation');
  147.  
  148.   n := GetSubtitleCount;
  149.   InsertSubtitle(n, GetSubtitleFinalTime(n-1)+1000, GetSubtitleFinalTime(n-1)+3000,
  150.                  'We insert new subtitle, in position ' + IntToStr(n) + ' (last node)', 'This is the translation');
  151.  
  152.   // Create the main form. It's not mandatory to create one - I do it here just
  153.   // to show you what can be done.
  154.   Form := TForm.Create(Application);
  155.   try
  156.  
  157.     Form.Font.Name   := 'Tahoma';
  158.     Form.Font.Size   := 8;
  159.     Form.Caption     := 'Delete long subtitles';
  160.     Form.BorderStyle := bsDialog;
  161.     Form.BorderIcons := [];
  162.     Form.Position    := poScreenCenter;
  163.     Form.Width       := 206;
  164.     Form.Height      := 107;
  165.  
  166.     // Create panel, to make the form look nicer
  167.     Pnl        := TPanel.Create(Application);
  168.     Pnl.Left   := 8;
  169.     Pnl.Top    := 8;
  170.     Pnl.Height := 35;
  171.     Pnl.Width  := 185;
  172.     Pnl.Parent := Form;
  173.  
  174.     // Create the "Show confirmation" checkbox
  175.     Chk            := TCheckBox.Create(Application);
  176.     Chk.Parent     := Pnl;
  177.     Chk.ParentFont := True;
  178.     Chk.Top        := 8;
  179.     Chk.Left       := 8;
  180.     Chk.Height     := 17;
  181.     Chk.Width      := 169;
  182.     Chk.Caption    := 'Confirm each deletion';
  183.  
  184.     // Create Ok button
  185.     Btn1             := TButton.Create(Application);
  186.     Btn1.Parent      := Form;
  187.     Btn1.Default     := True;
  188.     Btn1.Caption     := '&Ok';
  189.     Btn1.ModalResult := mrOk;
  190.     Btn1.OnClick     := @Btn1Click;
  191.     Btn1.Top         := 50;
  192.     Btn1.Left        := 8;
  193.     Btn1.Width       := 88;
  194.     Btn1.Height      := 25;
  195.     Btn1.Font.Style  := Btn1.Font.Style + [fsBold];
  196.  
  197.     // Create Cancel button
  198.     Btn2             := TButton.Create(Application);
  199.     Btn2.Parent      := Form;
  200.     Btn2.Caption     := '&Cancel';
  201.     Btn2.ModalResult := mrCancel;
  202.     Btn2.Top         := 50;
  203.     Btn2.Left        := 104;
  204.     Btn2.Width       := 88;
  205.     Btn2.Height      := 25;
  206.  
  207.     // Show the form, as modal, so that you can't use Subtitle Workshop while
  208.     // the form is loaded.
  209.     Form.ShowModal;
  210.  
  211.   finally
  212.     // Free everything from memory
  213.     Chk.Free;
  214.     Pnl.Free;
  215.     Btn1.Free;
  216.     Btn2.Free;
  217.     Form.Free;
  218.   end;
  219.  
  220. end.
  221.