Help us understand the problem. What is going on with this article?

Numbers,PagesファイルをExcel,Wordへ変換

More than 5 years have passed since last update.

Autometerでファイル変換.
本当はファイル名指定でexportをしたかったが,試行錯誤のため時間が足りず断念.
openしてexport,という解を採用.いちいちウィンドウが開くが仕方がない.

参考URL

ToDo

  • NumbersとPagesでコードが重複.関数を作って一括管理したい
  • open/closeせずに,exportだけで済ませたい

フォルダアクション

on run {input}

    -- 保存先
    set the defaultDestinationFolder to (POSIX file "/Users/foobar/Documents/" as alias)

    repeat with x in input
        set aFile_str_posix to (POSIX path of (x as Unicode text)) as Unicode text

        if {aFile_str_posix ends with ".numbers"} then
            tell application "Numbers"
                try
                    activate
                    open x

                    set exportFileExtension to "xls"

                    -- 拡張子を取り除く
                    set documentName to the name of the front document
                    if documentName ends with ".numbers" then ¬
                        set documentName to text 1 thru -9 of documentName

                    -- 同じファイル名が存在すれば"-1"などを付ける
                    tell application "Finder"
                        set newExportFileName to documentName & "." & exportFileExtension
                        set incrementIndex to 1
                        repeat until not (exists document file newExportFileName of defaultDestinationFolder)
                            set newExportFileName to ¬
                                documentName & "-" & (incrementIndex as string) & "." & exportFileExtension
                            set incrementIndex to incrementIndex + 1
                        end repeat
                    end tell
                    set the targetFileHFSPath to (defaultDestinationFolder as string) & newExportFileName


                    -- 変換
                    with timeout of 3 seconds
                        export front document to file targetFileHFSPath as Microsoft Excel
                    end timeout


                    close front document


                on error error_message number error_number
                    display alert "Numbers Failed" message ¬
                        "Error:     " & error_message & " " & "Error Number:  " & error_number ¬
                        as warning
                end try
            end tell
        end if

        if {aFile_str_posix ends with ".pages"} then
            tell application "Pages"
                try
                    activate
                    open x

                    set exportFileExtension to "doc"

                    -- 拡張子を取り除く
                    set documentName to the name of the front document
                    if documentName ends with ".pages" then ¬
                        set documentName to text 1 thru -7 of documentName

                    -- 同じファイル名が存在すれば"-1"などを付ける
                    tell application "Finder"
                        set newExportFileName to documentName & "." & exportFileExtension
                        set incrementIndex to 1
                        repeat until not (exists document file newExportFileName of defaultDestinationFolder)
                            set newExportFileName to ¬
                                documentName & "-" & (incrementIndex as string) & "." & exportFileExtension
                            set incrementIndex to incrementIndex + 1
                        end repeat
                    end tell
                    set the targetFileHFSPath to (defaultDestinationFolder as string) & newExportFileName


                    -- 変換
                    with timeout of 3 seconds
                        export front document to file targetFileHFSPath as Microsoft Word
                    end timeout


                    close front document


                on error error_message number error_number
                    display alert "Numbers Failed" message ¬
                        "Error:     " & error_message & " " & "Error Number:  " & error_number ¬
                        as warning
                end try
            end tell
        end if


    end repeat
end run
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away