Apple Script запускается, но сохраненные документы не отображаются (ошибки не отображаются) - PullRequest
0 голосов
/ 17 мая 2018

Этот код выполняется, но по какой-то причине после его сохранения файлы не сохраняются и ошибки не появляются. Идеи?

AppleScript

tell application "Finder"
    set fl to files of folder POSIX file "/Users/abc/folder" as alias list
end tell

repeat with f in fl

tell application "Microsoft Word"
    activate
    open f
    set theActiveDoc to the active document
end tell

delay 1

tell application "System Events"
    keystroke "a" using command down
    keystroke "c" using command down
end tell

tell application "Finder"
    set filename to name of f
    set the_path to POSIX file "/Users/abc/folder2/" & filename
end tell

tell application "Microsoft Word"
    close theActiveDoc saving no
    set new_document to make new document
    paste special (text object of selection) data type paste text
    save as new_document file name the_path
    close active document
end tell

end repeat

решение

установить the_path в (текст POSIX "/ Users / abc / folder2 /" как текст) и имя файла

1 Ответ

0 голосов
/ 17 мая 2018

Вот совершенно другой маршрут. Это решение полностью исключает необходимость использования Microsoft Word. Я настроил этот сценарий для циклического прохождения каждого файла в указанной папке (при условии, что все файлы в этой папке являются либо документами Microsoft Word, либо обычными текстовыми файлами, либо файлами форматированного текста)… берет содержимое каждого файла и создает новый файл .docx с этим содержимым в новой папке

Если этот код сохранен как приложение, вы можете перетащить файлы и / или папки непосредственно на значок этого приложения для обработки. Если это приложение запускается напрямую, оно спросит вас, хотите ли вы обработать файл или файлы или обработать папку со всем ее содержимым

on open theFiles2
    --  Handle the case where the script is launched by dropping
    -- file or folders onto this applet's icon
    tell application "Finder"
        set theFiles to files of entire contents of folder theFiles2 as alias list
    end tell
    repeat with aFile from 1 to count of theFiles
        set thisFile to item aFile of theFiles
        set thisFile2 to thisFile
        set thisFile to POSIX path of thisFile
        tell application "Finder"
            set fileName to name of thisFile2
            set fileName to (characters -5 thru 1) of fileName as string
            set thePath to (path to desktop as text) & fileName
            try
                set newFolder to make new folder ¬
                    at (path to desktop) ¬
                    with properties {name:fileName}
            end try
        end tell
        set thePath to POSIX path of alias thePath
        set theScript to do shell script "textutil -convert docx " & quoted form of ¬
            thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
    end repeat
end open

on run
    --  Handle the case where the script is launched without any dropped files
    set sourceFolder to display dialog ¬
        "Would You Like To Choose A Folder With All Of Its Contents Or Individual Files Within A Folder?" buttons {"Cancel", "Select Files", "Select Folder"} ¬
        default button 3 ¬
        cancel button ¬
        "Cancel" with title ¬
        "withTitleText" with icon 1 ¬
        giving up after 20
    if button returned of sourceFolder is "Select Folder" then
        set sourceFolder to ((choose folder) as text)
        tell application "Finder"
            set theFiles to files of entire contents of folder sourceFolder as alias list
        end tell
        repeat with aFile from 1 to count of theFiles
            set thisFile to item aFile of theFiles
            set thisFile2 to thisFile
            set thisFile to POSIX path of thisFile
            tell application "Finder"
                set fileName to name of thisFile2
                try
                    set fileName to (characters -5 thru 1) of fileName as string
                end try
                set thePath to (path to desktop as text) & fileName
                try
                    set newFolder to make new folder ¬
                        at (path to desktop) ¬
                        with properties {name:fileName}
                end try
            end tell
            set thePath to POSIX path of alias thePath
            set theScript to do shell script "textutil -convert docx " & quoted form of ¬
                thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
        end repeat
    else if button returned of sourceFolder is "Select Files" then
        set theFiles to choose file with prompt ¬
            "Choose Files" invisibles false ¬
            multiple selections allowed true ¬
            as text
        repeat with aFile from 1 to count of theFiles
            set thisFile to item aFile of theFiles
            set thisFile2 to thisFile
            set thisFile to POSIX path of thisFile
            tell application "Finder"
                set fileName to name of thisFile2
                try
                    set fileName to (characters -5 thru 1) of fileName as string
                end try
                set thePath to (path to desktop as text) & fileName
                try
                    set newFolder to make new folder ¬
                        at (path to desktop) ¬
                        with properties {name:fileName}
                end try
            end tell
            set thePath to POSIX path of alias thePath
            set theScript to do shell script "textutil -convert docx " & quoted form of ¬
                thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
        end repeat
    end if
end run

Этот код может быть немного неаккуратным, и может быть лучшее решение, но ... Кажется, он хорошо работает на моей системе, работающей с последней версией macOS High Sierra

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...