2

我用拉丁字母自动填写表格,但在一个地方有一个字段我需要用俄语输入文本。

在填写之前,我用俄语复制了所需的单词,我想从剪贴板中输入它而不更改系统语言/编码/字符集。

换句话说。只需从剪贴板中用俄语字母输入一个单词,然后继续使用拉丁字母。

如何使用applescript来完成?

set r to the clipboard
write_string(r as text)

on write_string(the_string)
    tell application "System Events"
        
        repeat with the_character in the_string
            keystroke the_character
            delay (random number from 0.05 to 0.25)
        end repeat
        
    end tell
end write_string

壳牌我使用这部分像那样或什么?

write_string(r as text) encode('utf8')?

仅复制和粘贴俄语单词的解决方案不起作用。它需要被写出来。

请,直接挖哪条路。有任何想法吗?

4

3 回答 3

0

user3439894,您好!对于临时语言更改,您的 Big Sur 代码有效。谢谢你。我只做了一些小的改动(延迟+命令)。

set the clipboard to " Как это можно сделать с помощью?"
set r to the clipboard
write_string(r as text)

on write_string(the_string)
    
    tell application "System Events"
        key code 49 using command down --   ⌃Space
        delay 0.5
        repeat with the_character in the_string
            keystroke the_character
            delay (random number from 0.05 to 0.25)
        end repeat
        delay 0.5
        key code 49 using command down --   ⌃Space
    end tell
    
end write_string

但是当算法本身理解以什么编码来编写复制的文本时,我想找到一个解决方案。例如,如果我复制日文字符而不是俄文(我已安装在系统中),并且算法将准确地将日文字符输入到字段中,您认为这是可能的吗?也许用javascript?

于 2021-08-14T10:09:44.857 回答
0

此解决方案使用jackjr300在他的回答中编写的代码: Is it possible to keystroke special characters in AppleScript?

使用Xcode,我创建了命令行工具 TypeCharacters并将其复制到/usr/local/bin/

然后我将其合并到以下示例 AppleScript 代码中:

set the clipboard to "Русские персонажи & 日本語の文字"

set thisText to the clipboard

tell application "Safari" to activate
delay 0.5

my typeCharactersOf(thisText)

to typeCharactersOf(thisString)
    set theStringList to characters of thisString
    repeat with thisCharacter in theStringList
        do shell script ¬
            "/usr/local/bin/TypeCharacters " & ¬
            quoted form of thisCharacter
        delay (random number from 0.05 to 0.25)
    end repeat
end typeCharactersOf

其输出是:

在此处输入图像描述


笔记:

我使用谷歌翻译将“俄语字符”翻译成俄语,Русские персонажи,将“日语字符”翻译成日语,日本语の文字。

通过将本网站上的Safari写入答案文本框进行测试,无论输入源设置为什么,结果都是相同的。

上面显示的示例 AppleScript 代码在macOS Catalina下的脚本编辑器中进行了测试,系统偏好设置中 的语言和区域设置设置为英语(美国)——系统偏好设置>键盘>输入源中的主要和两个输入源美国俄语,以及为我工作,没有问题1

  • 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。

链接答案中用于在Xcode中创建命令行工具 的代码:TypeCharacters

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        if (argc > 1) {
            NSString *theString = [NSString stringWithUTF8String:argv[1]];
            UniChar uChar;
            CGEventRef keyEvent = CGEventCreateKeyboardEvent(nil, 0, true);
            for (int i = 0; i < [theString length]; i++)
            {
                uChar = [theString characterAtIndex:i];
                CGEventKeyboardSetUnicodeString(keyEvent, 1, &uChar);
                CGEventPost(kCGHIDEventTap, keyEvent); // key down
                CGEventSetType(keyEvent, kCGEventKeyUp);
                CGEventPost(kCGHIDEventTap, keyEvent); // key up (type the character)
                CGEventSetType(keyEvent, kCGEventKeyDown);
                [NSThread sleepForTimeInterval:0.001]; // wait 1/1000 of second, no need of this line on my computer, I use 0.001 to be safe, increase it If you still have issues
            }
            CFRelease(keyEvent);
        }
    }
    return 0;
}


注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。

于 2021-08-14T15:50:03.797 回答
-1

系统事件使用keystrokekey code在所选输入源的上下文中使用时,如下所列:系统偏好设置>键盘>输入源

换句话说,使用“如何做到这一点?”的谷歌翻译。俄语中,“Как это можно сделать с помощью?”,复制到剪贴板,我当前的输入源US,然后系统事件输出:aaa aaa aaaaa aaaaaaa a aaaaaaa?

要获得“Как это можно сделать с помощью?” 需要手动切换或以编程方式将输入源切换为例如俄语,而系统事件用于keystroke产生所需的结果。

示例 AppleScript 代码(如下所示)在macOS Catalina下的脚本编辑器中进行了测试,系统偏好设置中 的语言和区域设置设置为英语(美国)——系统偏好设置>键盘>输入源中的主要和两个输入源美国俄语,以及为我工作,没有问题1

  • 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。

示例 AppleScript 代码

set the clipboard to "Как это можно сделать с помощью?"

set theRussianText to the clipboard

my writeString(theRussianText)

to writeString(theString)
    set theStringList to characters of theString
    tell application "System Events"
        key code 49 using control down --   ⌃Space
        delay 0.25
        repeat with thisCharacter in theStringList
            keystroke thisCharacter
            delay (random number from 0.05 to 0.25)
        end repeat
        key code 49 using control down --   ⌃Space
    end tell
end writeString

笔记:

这使用默认键盘快捷键,在System Preferences > Keyboard > Shortcuts > Input Sources中,选择以前的输入源,因为我只列出了两个并且已经设置为US⌃Space切换到俄语,然后在执行时返回US第二次。

如果您在System Preferences > Keyboard > Input Sources中有两个以上的输入源,那么您将需要以不同的方式处理它。

例如,我使用了第三方实用程序 InputSourceSelector,如下面的AppleScript代码示例

set the clipboard to "Как это можно сделать с помощью?"

set theRussianText to the clipboard

my writeString(theRussianText)

to writeString(theString)
    set theStringList to characters of theString
    my switchInputSourceTo("com.apple.keylayout.Russian")
    delay 0.25
    tell application "System Events"
        repeat with thisCharacter in theStringList
            keystroke thisCharacter
            delay (random number from 0.05 to 0.25)
        end repeat
    end tell
    my switchInputSourceTo("com.apple.keylayout.US")
end writeString

to switchInputSourceTo(thisInputSource)
    do shell script ¬
        "/usr/local/bin/InputSourceSelector" & ¬
        space & "select" & space & thisInputSource
end switchInputSourceTo


正如您在此剪辑屏幕截图的底部所见,来自Script Editor,以紫色文本显示,“Как это можно сделать с помощью?” 被输入。

在此处输入图像描述



注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。

于 2021-08-14T01:26:36.610 回答