AppleScript - скопируйте текст из другого приложения и вставьте в MS Word, не теряя формат - PullRequest
0 голосов
/ 18 марта 2020

Я хочу сделать следующее:

  1. Копировать текст (код в Script Debugger 7, вы можете изменить его на другое приложение);

  2. Активировать MS Word (уже открыт документ);

  3. Создать новую таблицу (строка 1 x столбец 1);

  4. Вставить текст к единственной ячейке в таблице.

Я написал этот код:

tell application "System Events"
    tell process "Script Debugger"
        keystroke "c" using {command down}
        delay 0.2           
    end tell
end tell

tell application "Microsoft Word"
    activate
    delay 0.3
    set theCode to the clipboard

    set newTable to make new table at active document with properties ¬
        {number of rows:1, number of columns:1}

    auto format table newTable table format table format classic1 ¬
        with apply borders

    set content of text object of (get cell from table newTable row 1 column 1) to theCode
end tell

Копирование и вставка работает нормально. Но проблема в том, что код в редакторе кода красочный, но это простой текст после вставки в Word. Если я использую системные события для нажатия клавиши cmd + v :

tell application "System Events"
    tell process "Script Debugger"
        keystroke "c" using {command down}
        delay 0.2
        --set theCode to the clipboard
    end tell
end tell

tell application "Microsoft Word"
    activate
    delay 0.2
    set newTable to make new table at active document with properties ¬
        {number of rows:1, number of columns:1}
    auto format table newTable table format table format classic1 ¬
        with apply borders
end tell

tell application "System Events"
    tell process "Microsoft Word"
        keystroke "v" using {command down}
        delay 0.2
    end tell
end tell

Текстовый формат в порядке, но у него есть другая проблема. Поэтому я хочу использовать первый метод для выполнения этой работы.
Надеюсь, кто-то может сказать мне, что не так с кодом первой версии. Большое спасибо.

...