首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试使用AppleScript将.xls和.xlsx转换为.txt (选项卡分隔),需要修复

尝试使用AppleScript将.xls和.xlsx转换为.txt (选项卡分隔),需要修复
EN

Stack Overflow用户
提问于 2013-08-15 18:49:36
回答 1查看 1.7K关注 0票数 2

这是我从另一条线上得到的答案。我正在尝试将~300个.xls和.xlsx文件转换为标签分隔符。它们都在同一个文件夹中。如果有人知道更好的方法,请告诉我。

代码语言:javascript
运行
复制
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

所有这些都是打开一个对话框而什么也不做。即使它是一个液滴,当我拖动一个文件到它时,什么也不会发生。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-15 19:00:47

在Applescript中,当脚本或应用程序正常运行时,将运行run处理程序。同时,当某个或多个文件是应用程序的掉到图标上并将文件设置为变量VarName时,将运行该VarName处理程序。您发布的脚本巧妙地将display dialog放置在on run处理程序中,以帮助您理解这种用法。相反,将脚本保存为应用程序,然后将文件放到它上。

编辑:

在一台最终拥有Excel 2011的山狮机器上进行了快速测试(我没有意识到自己有多复杂):

代码语言:javascript
运行
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18259576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档