尝试使用AppleScript将.xls和.xlsx转换为.txt(制表符分隔),需要修复

时间:2013-08-15 18:49:36

标签: excel macos vba applescript ms-office

我从另一个帖子的答案中复制了这个。我试图将~300 .xls和.xlsx文件转换为制表符分隔。它们都在同一个文件夹中。如果有人知道更好的方法,请告诉我。

property type_list : {"XLS6", "XLS7", "XLS8", "XLSX"}
property extension_list : {"xls", "xlsx"}


on open these_workbooks
repeat with k from 1 to the count of these_workbooks
    set this_item to item k of these_workbooks
    set the item_info to info for this_item

    --this if statement tests to make sure the items you're converting are Excel spreadsheets and not folders or aliases
    if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then

        tell application "Finder" to open this_item

        tell application "Microsoft Excel"
            --this just tacks on ".txt" to your file name
            set workbookName to (name of active workbook & ".txt")
            --save the current open workbook as a tab-delimited text file
            tell active workbook to save workbook as filename workbookName file format text Mac file format
            close active workbook saving no
        end tell
    end if
end repeat
end open

on run
    display dialog "Drop Excel files onto this icon."
end run

所有这一切都是打开一个对话框,什么都不做。即使它是一个小滴,当我将文件拖到它时也没有任何反应。

1 个答案:

答案 0 :(得分:3)

在Applescript中,run处理程序在脚本或应用程序正常运行时运行。同时,open VarName处理程序在某些文件或文件为应用程序dropped onto the icon并且文件设置为变量VarName时运行。您发布的脚本巧妙地将display dialog放在on run处理程序中,以帮助您了解此用法。相反,将脚本保存为应用程序,然后将文件放在其上。

修改

在对Mountain Lion机器进行快速测试后,终于有了Excel 2011(我没有意识到我已经过度复杂了):

property type_list : {"XLS6", "XLS7", "XLS8", "XLSX"}
property extension_list : {"xls", "xlsx"}


on open these_workbooks
    repeat with k from 1 to the count of these_workbooks
        set this_item to item k of these_workbooks
        set the item_info to info for this_item

        --this if statement tests to make sure the items you're converting are Excel spreadsheets and not folders or aliases
        if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then

            tell application "Finder" to open this_item

            tell application "Microsoft Excel"
                --this just tacks on ".txt" to your file name
                set workbookName to (path to desktop as string) & "After:" & (name of active workbook & ".txt")
                display dialog workbookName
                --save the current open workbook as a tab-delimited text file
                tell active workbook to save workbook as filename workbookName file format text Mac file format
                close active workbook saving no
            end tell
        end if
    end repeat
end open

on run
    display dialog "Drop Excel files onto this icon."
end run