LoginSignup
2
2

More than 5 years have passed since last update.

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

Posted at

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
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2