2011年4月24日日曜日

FDKサンプルからスクリプトに移植1

FM10のスクリプトはサンプルファイルがほとんど無く、リファレンスガイドすらない状態なので調査も兼ねてFDKサンプルのtextを移植してみました。
移植してみた雑感としては
  • 基本はFDKの仕様をオブジェクト化したという感じ(F_ObjHandleTの数値は各オブジェクトのidプロパティにある)
  • F_Printfに対応する命令がない?(今回はalertでごまかしてある)
  • alert(Javascript)とAlert(FrameMaker)とは違う
  • やっぱりポインタを気にすることないので非常に楽(特に文字列!)
XML読み書きのアプリケーションやDDE、COM、Win32APIを利用したいという場面以外ではFDKを使用するということはなくなるのではないだろうか。
var mMenu = app.GetNamedMenu("!MakerMainMenu");
var nMenu = mMenu.DefineAndAddMenu("APIMenu", "TextApi");

nMenu.DefineAndAddCommand(1, "GetTextCmd", "Get Text","");
nMenu.DefineAndAddCommand(2, "AddTextCmd","Insert Text","\\!GT");
nMenu.DefineAndAddCommand(3, "DeleteTextCmd", "Delete Text", "\\!IT");
nMenu.DefineAndAddCommand(4, "ColorTextCmd", "Color Text", "\\!DT");
nMenu.DefineAndAddCommand(5, "FontSizeCmd", "Change Font Size", "\\!CT");

UpdateMenus();

function Command(command)
{
    var doc = app.ActiveDoc;
    if(doc.id === 0)
        Alert("Please selected Document.", Constants.FF_ALERT_CONTINUE_WARN);

    switch(command)
    {
    /* Get text from selection. */
    case 1: 
        var tr = doc.TextSelection;
        
        if (tr.beg.obj.id === 0 || ((tr.beg.obj.id === tr.end.obj.id) && (tr.beg.offset === tr.end.offset)))
        {
            Alert("Please select some text and try again.", Constants.FF_ALERT_CONTINUE_WARN);
            break;
        }
        var textItems = doc.GetTextForRange(tr, Constants.FTI_String);
        alert(CreateStringFromTextItems(textItems));
        break;
        
    /* Add text. */
    case 2:
        var tr = doc.TextSelection;
        if (tr.beg.obj.id === 0)
        {
            Alert("Please select an insertion point and try again.", Constants.FF_ALERT_CONTINUE_WARN);
            break;
        }
        if (!((tr.beg.obj.id === tr.end.obj.id) && (tr.beg.offset === tr.end.offset)))
        {
            if (Alert("Do you wish to overwrite the selected text?", Constants.FF_ALERT_NO_DEFAULT))
                break;
        }
        doc.AddText(tr.beg, "The new CoffeeTool\011");
        break;

    /* Delete selected text. */
    case 3:
        var tr = doc.TextSelection;
        if (tr.beg.obj.id === 0 || ((tr.beg.obj.id === tr.end.obj.id) && (tr.beg.offset === tr.end.offset)))
        {
            Alert("Please select some text and try again.", Constants.FF_ALERT_CONTINUE_WARN);
            break;
        }

        doc.DeleteText(tr);
        break;

 /* Change the color of selected text to Red. */
 case 4:
        var tr = doc.TextSelection;
        if (tr.beg.obj.id === 0 || ((tr.beg.obj.id === tr.end.obj.id) && (tr.beg.offset === tr.end.offset)))
        {
            Alert("Please select some text and try again.", Constants.FF_ALERT_CONTINUE_WARN);
            break;
        }
        var color = doc.GetNamedObject(Constants.FO_Color, "Red");  // Color name is different in each language.
        var props = doc.GetTextProps(tr.beg);
        var i = GetPropIndex(props, Constants.FP_Color);
        props[i].propVal.obj = color;
        doc.SetTextProps(tr, props);
        break;

    /* Change the font size of the selected text to 30 points. */
    case 5:
        var tr = doc.TextSelection;
        if (tr.beg.obj.id === 0 || ((tr.beg.obj.id === tr.end.obj.id) && (tr.beg.offset === tr.end.offset)))
        {
            Alert("Please select some text and try again.", Constants.FF_ALERT_CONTINUE_WARN);
            break;
        }
        /* Allocate memory for the property list. */
        props = AllocatePropVals(1);
        /* Set up the properties. */
        props[0].propIdent.num = Constants.FP_FontSize;
        props[0].propVal.ival = 30 * 65536;
        props[0].propVal.valType = Constants.FT_Metric;
        /* Apply the property list to the text selection. */
        doc.SetTextProps(tr, props);
        break;
    }
}

/***************************************************************
* Create a string from an TextItems structure.
****************************************************************/
function CreateStringFromTextItems(textItems)
{
    var s = "";
    for (var i = 0; i < textItems.length; i++)
    {
        if (textItems[i].dataType === Constants.FTI_String)
        {
            s += textItems[i].sdata;
        }
    }
    
    return s;
}

0 件のコメント:

コメントを投稿