向chrome发送Ctrl+C时,不能获取选定的文本(C#)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了向chrome发送Ctrl+C时,不能获取选定的文本(C#)相关的知识,希望对你有一定的参考价值。

参考技术A 是不是因为没有模拟选中文本?
选中文本为空,当然剪贴板的内容为false追问

你认为我会这样2

追答

剪贴板为空,有三种情况:
1、在Chrome中,选中文本的模拟失败了。
2、发送Ctrl+C之前,Chrome未被active
3、你的Chrome网页,Ctrl+C热键受到干扰,例如快捷键冲突或被JS禁用

追问

不知道你是否写过这样的消息处理,
1,我是手动选定的文本,所以 2 同样否定
3,可以明确的告诉你,chrome已经接受ctrl+C,并且也正常处理,仅只它比我的if()执行时间晚了一点时间。
最后,烦请专业点。

追答

发送Ctrl+C之后,并且在if()语句之前,做个延时等待200ms的处理,应该就可以了

使用 Photoshop 脚本更改选定的文本图层语言

【中文标题】使用 Photoshop 脚本更改选定的文本图层语言【英文标题】:Changing selected text layers language with photoshop script 【发布时间】:2015-08-18 09:08:28 【问题描述】:

虽然我设法将一个功能性脚本一起更改所选图层的字典语言,但这还不是我所需要的。

我目前正在检索我选择的图层的索引,使用它们来获取图层名称——这是有问题的部分——然后​​根据在我显示的自定义对话框。

层名称的使用是一个非常半途而废的解决方案,我需要精确,我需要能够根据层 ID 或索引更改语言。不仅我的脚本容易出现名称冲突,而且我不知道这些选定的层将在哪里,是在文档的根目录上还是在 5 个组堆栈内。

这是我当前的代码:

主要功能

function main() 
    // Create window with language selection and retrieve selected value
    var lang = createWindow();

   // Retrieve selected layers
   var selectedLayers = getSelectedLayersIdx();

   // Look at each entry and change language when possible
   for(var a in selectedLayers)
        try 
           setLangByIndex(Number( selectedLayers[a]), lang );
         catch(err)    
   

createWindow 返回一个 lang 值,跳过它)

Mike 得到 Idx

function  getSelectedLayersIdx()
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    var desc = executeActionGet(ref);
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ) 
        desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
        var c = desc.count
        var selectedLayers = new Array();
        for(var i=0;i<c;i++) 
            try
                activeDocument.backgroundLayer;
                selectedLayers.push(  desc.getReference( i ).getIndex() );
             catch(e) 
                selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
            
        
     else 
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
        try 
            activeDocument.backgroundLayer;
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);
         catch(e) 
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));
        
    
    return selectedLayers;

设置语言的函数

function setLangByIndex(idx, lang)
    ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    var name = executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));

    var el = activeDocument.layers.getByName( name );
    // Temp quick block; MUST UPDATE IN CASE OF NEW LANGUAGES
     if (lang == 'pt_br' )
        el.textItem.language = Language.BRAZILLIANPORTUGUESE;
    else if (lang == 'en_us' )
        el.textItem.language = Language.ENGLISHUSA;
    else
        el.textItem.language = Language.ENGLISHUK;

我尝试了低效的解决方法:

使用 Adob​​e 的脚本侦听器通过 executeAction 更改语言, 但无法使其工作。语言部分在〜4堆叠内 描述符(和一个列表!)。我迷路了,不断收到错误 8800。 弄清楚如何使用 idx 获取对象 ref。失败的 很惨。

【问题讨论】:

如果没有示例 PSD,调试起来有点困难。您可以上传一个非常简化的 PhotoShop 文档吗?如果你正在上传,你是否也可以上传这些脚本。 今天早上我又试了一次,它成功了!我使用的 Script Listener 输出可以在这里找到:pastebin.com/U9fQKLvG 这是我当前的脚本:pastebin.com/kZf8aeDY 我不会将其作为答案发布,只是想看看是否有人有更好的建议(我的编码技能很弱!) .我使用了完整的 set 操作,因为 Override 不接受 idx。 PSD 示例(我认为不再需要它,但是...):goo.gl/C9SdNu 值得注意的是,我将使用此脚本的文件差异很大,因此不应仅针对此示例定制脚本. 【参考方案1】:

在我注意到一些与段落样式相关的恼人故障(您可以将其定义为 PS 上文本的默认设置,而不是 JS 对象)之后,我放弃了固执并停止避免通过索引数组执行新循环完成后重新选择图层。

/*
   *  Based in Mike Hale's "getSelectedLayersIdx" script and Adobe's Scripting Listener dump.
      Based the panel on Davide Barranca's code: http://www.davidebarranca.com/2012/10/scriptui-window-in-photoshop-palette-vs-dialog/

      Useful Resources:
      Photoshop char list: http://www.pcpix.com/Photoshop/char.htm
      ActionDescriptor methods: http://jongware.mit.edu/pscs5js_html/psjscs5/pc_ActionDescriptor.html
      TextItem methods: http://jongware.mit.edu/pscs5js_html/psjscs5/pc_TextItem.html
      You can fish for language codes here: http://wingp.googlecode.com/svn/photogp/photoshopapi/photoshop/PIStringTerminology.h
*/

/* ______________________________ SCRIPT ______________________________ */
var scriptAlert = "Language Change says";

// MUST UPDATE IF YOU WANT TO CHANGE THE AVAILABLE LANGUAGES
// The available languages, as  text displayed on dialog window : codename . See notes above for more language codes
var langs = ;
langs['Português (Brasil)'] = 'brazilianPortugueseLanguage';
langs['English (UK)'] = 'ukenglishLanguage';
langs['English (USA)'] = 'englishLanguage';

if(app.documents.length>0) // the script will be executed  only if an active document exists.

    var imgName = activeDocument.name;
    var extreme= imgName.substr(imgName.length - 5, 5);
    var dot = extreme.lastIndexOf(".");

    if(dot == -1)  // Checks if the active document  was saved or not.
        alert("Please, save your document!", scriptAlert, false);
     else 
        main(); // if the active document was saved, go ahead! 
    



function createWindow()  // Creates a panel asking for input
        var isDone, win, windowResource, radio_group, cancel, rb, i;

        //sentinel variable
        isDone = false;
        cancel = false;

        // Build dialog radio buttons
        i = 0;
        for( var k in langs ) 
            if( i==0 )
                rb = "rb"+i+": RadioButton  text:  '"+k+"', value: 1 , \ ";
            else
                rb += "rb"+i+": RadioButton  text:  '"+k+"' , \ ";
            i++;
        

        windowResource = "palette   \
            orientation: 'column', \
            alignChildren: ['fill', 'top'],  \
            preferredSize:[300, 130], \
            text: 'Change Spell Check Language',  \
            margins:15, \
            \
            optPanel: Panel  \
                orientation: 'row', \
                alignChildren: 'left', \
                margins:15, \
                text: ' Select the desired dictionary language for the select text ', \ "+  rb + " \
                 \
            \
            bottomGroup: Group \
                cancelButton: Button  text: 'Cancel', properties:name:'cancel', size: [120,24], alignment:['right', 'center'] , \
                applyButton: Button  text: 'Ok', properties:name:'ok', size: [120,24], alignment:['right', 'center'], active: true , \
            \
        ";
        win = new Window(windowResource);

        radio_group = win.optPanel; // set group

        // Match options with language
        function selected_rbutton (rbuttons)
        
             for (var i = 0; i < rbuttons.children.length; i++)
                 if (rbuttons.children[i].value == true)
                    return langs[rbuttons.children[i].text];
         

        win.bottomGroup.cancelButton.onClick = function() 
            win.close();
            return cancel = true;
        ;      
        // Button listeners
        win.bottomGroup.applyButton.onClick = function() 
          return isDone = true;
        ;             
        // don't forget this one!
        win.onClose = function() 
            return cancel = true;
        ;

        win.addEventListener("keydown", function (k)
         
             if (k.keyName == 'Enter')
                return isDone = true;
         );

        win.show();

        while (isDone === false) 
            try 
                app.refresh();
             catch(err)  // Esc cancels without errors
                return false;
            
        

       return cancel ?  false : selected_rbutton (radio_group); // return value, or false if cancelled



function  getSelectedLayersIdx() // Gets array of indexes of selected layers
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    var desc = executeActionGet(ref);
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ) 
        desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
        var c = desc.count
        var selectedLayers = new Array();
        for(var i=0;i<c;i++) 
            try
                activeDocument.backgroundLayer;
                selectedLayers.push(  desc.getReference( i ).getIndex() );
             catch(e) 
                selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
            
        
     else 
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
        try 
            activeDocument.backgroundLayer;
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);
         catch(e) 
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));
        
    
    return selectedLayers;



function selectLayerByIndex( selectedLayers, add ) // Selects layers by index. If fed an array + true add, selects them, otherwise provide a single value so it selects a single layer
    if (typeof(add)==='undefined') add = false;

    if ( add ) // If Add it's an array
        for(var a in selectedLayers)
            var desc = new ActionDescriptor();
                var ref = new ActionReference();
                ref.putIndex( charIDToTypeID( "Lyr " ), Number(selectedLayers[a]) );
            desc.putReference( charIDToTypeID( "null" ), ref );
            desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
            var idMkVs = charIDToTypeID( "MkVs" );
            desc.putBoolean( charIDToTypeID( "MkVs" ), false );

            executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
        
    else // Else I just need to make this layer active, it's a number
    
            var desc = new ActionDescriptor();
                var ref = new ActionReference();
                var idLyr = charIDToTypeID( "Lyr " );
                ref.putIndex( charIDToTypeID( "Lyr " ), selectedLayers );
            desc.putReference( charIDToTypeID( "null" ), ref );
            desc.putBoolean( charIDToTypeID( "MkVs" ), false );
        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
    



function setLangByIndex( idx, lang ) // Sets the dictionary language for a text layer based on its index
// Make layer active
    selectLayerByIndex( idx );

// And do tiny override
    var textObj = new ActionDescriptor();
        var target = new ActionReference();
        target.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "TxtS" ) ); // desired property: text style
        target.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) ); // the active layer
    textObj.putReference( charIDToTypeID( "null" ), target );
        var textStyle = new ActionDescriptor();
        textStyle.putInteger( stringIDToTypeID( "textOverrideFeatureName" ), 808466225 );
        textStyle.putInteger( stringIDToTypeID( "typeStyleOperationType" ), 3 );
        textStyle.putEnumerated( stringIDToTypeID( "textLanguage" ), stringIDToTypeID( "textLanguage" ), stringIDToTypeID( lang ) );

    textObj.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "TxtS" ), textStyle ); // to (end); text style, desc.
executeAction( charIDToTypeID( "setd" ), textObj, DialogModes.NO ); // execute



////////////////////////////////////
// Main function
function main() 
    // Create window with language selection and retrieve selected value
    var lang = createWindow();

    if ( !lang ) // If cancelled please stop
        return;

     // Retrieve selected layers
     var selectedLayers = getSelectedLayersIdx();

    // Look at each entry and change language when possible; otherwise skip to next item
    for(var k in selectedLayers)
       try 
            setLangByIndex(Number(selectedLayers[k]), lang);
         catch(err) 
            continue;
        
    
    selectLayerByIndex( selectedLayers, true );

【讨论】:

以上是关于向chrome发送Ctrl+C时,不能获取选定的文本(C#)的主要内容,如果未能解决你的问题,请参考以下文章

使用 C++\Qt 从当前窗口中获取选定的文本

c/c++如何向指定窗口发送ctrl+v然后回车

如何从 jquery 的文本区域中获取选定的文本? [复制]

Chrome 扩展获取选定的文本

Unix命令行中的Ctrl + Z和Ctrl + C的区别

如何将 control + L 和 control + C 发送到另一个应用程序?