Jump to content

ماكروهات/رايتر/001

From The Document Foundation Wiki
This page is a translated version of the page Macros/Writer/001 and the translation is 100% complete.


الوصف

هذا التدريب يغطي كيفية كتابة ماكرو لـ "رايتر" والذي يُدرِج تعليقًا مخصصًا عند موضع المؤشر (مثلا لوضع قيم مسبقة لحقول "المؤلف" و/أو "التاريخ").

نستخدم com.sun.star.text.XTextViewCursor[1] و com.sun.star.text.textfield.Annotation[2] وأيضًا بنية com.sun.star.util.DateTime[3] لتحصل على بصمة وقت لوقت التنفيذ.

الشفرة

الماكرو التالي، الذي يمكن مثلاً أن يُربَط باختصار لوحة مفاتيح، يُدرِج تعليقًا مخصصًا عند موضع المؤشر الحالي.

Option Explicit

Sub InsertComment
    '''أدرج تعليقًا عند موضع المؤشر'''

    Dim oCurs As Object ' com.sun.star.text.text.XTextViewCursor
    Dim oAnnot As Object ' com.sun.star.text.textfield.Annotation
    Dim oDateTime As New com.sun.star.util.DateTime

    oCurs = ThisComponent.CurrentController.getViewCursor()
    oAnnot = ThisComponent.createInstance( _
        "com.sun.star.text.textfield.Annotation")

    With oDateTime ' com.sun.star.util.DateTime
        .Minutes = 20
        .Hours = 12
        .Day = 20
        .Month = 12
        .Year = 2012
    End With
   
    With oAnnot ' com.sun.star.text.textfield.Annotation
        .Author = "مؤلف التعليق"
        .Content = "محتوى التعليق"
        .DateTimeValue = oDateTime
    End With

    oAnnot.attach(oCurs.Start) ' أدرِج بداية المؤشر

End Sub ' InsertComment

باستخدام بايثون:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import uno

def InsertComment():
    """أدرج تعليقًا عند موضع المؤشر
    
    curs    com.sun.star.text.text.XTextViewCursor
    annot   com.sun.star.text.textfield.Annotation
    """
    dt = uno.createUnoStruct("com.sun.star.util.DateTime")

    thisComponent = XSCRIPTCONTEXT.getDocument()
    curs = thisComponent.CurrentController.getViewCursor()
    annot = thisComponent.createInstance(
        "com.sun.star.text.textfield.Annotation")

    dt.Minutes = 20
    dt.Hours = 12
    dt.Day = 20
    dt.Month = 12
    dt.Year = 2012

    annot.Author = "مؤلف التعليق"
    annot.Content = 'محتوى التعليق'
    annot.DateTimeValue = dt

    annot.attach(curs.Start)  # أدرِج بداية المؤشر

لا تُعمَل حسابات بصمة وقت في هذه الأمثلة.

ملاحظات