批量将*.number转换为*.csv AppleScript

批量将*.number转换为*.csv AppleScript,applescript,Applescript,我正在寻找一个脚本,它可以将给定文件夹中的所有*.number文件批量转换为*.csv文件 我在GitHub上发现了以下内容,并按照评论建议中的建议添加了一行内容。当我运行脚本时,Numbers会启动并从指定的文件夹打开测试文件,但该文件不会导出。数字保持打开状态,终端错误与: /Users/Shared/Untitled.scpt:执行错误:Numbers收到错误:无效的密钥形式。(-10002) 脚本(位于/Users/Shared中)具有以下权限: -rwxr-xr-x #!/usr/bi

我正在寻找一个脚本,它可以将给定文件夹中的所有*.number文件批量转换为*.csv文件

我在GitHub上发现了以下内容,并按照评论建议中的建议添加了一行内容。当我运行脚本时,Numbers会启动并从指定的文件夹打开测试文件,但该文件不会导出。数字保持打开状态,终端错误与:

/Users/Shared/Untitled.scpt:执行错误:Numbers收到错误:无效的密钥形式。(-10002)

脚本(位于/Users/Shared中)具有以下权限: -rwxr-xr-x

#!/usr/bin/osascript

on run argv

    set theFilePath to POSIX file (item 1 of argv)
    set theFolder to theFilePath as alias
    tell application "Finder" to set theDocs to theFolder's items

    -- Avoid export privilege problem
    set privilegeFile to (theFolder as text) & ".permission"
    close access (open for access privilegeFile)

    repeat with aDoc in theDocs
        set docName to aDoc's name as text
        if docName ends with ".numbers" then
            set exportName to (theFolder as text) & docName
            set exportName to exportName's text 1 thru -9
            set exportName to (exportName & "csv")
            tell application "Numbers"
                open aDoc
                delay 5 -- may need to adjust this higher
                tell front document
                    export to file exportName as CSV
                    close
                end tell
            end tell
        end if
    end repeat

end run

有什么建议吗?

以下是我在macOS High Sierra为我所做的工作:

  • 终端中
  • 触摸屏;开放式e-2csv;chmod+x2csv
    
    •这将创建一个名为numb2csv的空ASCII文本文件
    •默认情况下,在文本编辑中打开numb2csv •使numb2csv文件可执行。

  • 将下面进一步显示的示例AppleScript代码复制并粘贴到打开的numb2csv文件中
  • 保存并关闭numb2csv文件
  • 终端中执行numb2csv可执行文件,例如:
  • /numb2csv“$HOME/Documents”
    
    • 这创建了一个与我的Documents文件夹中的每个Number文档同名的CSV文件,而不是遍历任何嵌套文件夹
    示例AppleScript代码:

    #/usr/bin/osascript
    运行argv
    设置POSIX文件的文件路径(argv的项目1)
    将文件夹设置为文件路径作为别名
    告诉应用程序“系统事件”将DOC设置为扩展名为“number”的文件夹项
    在DOCS中使用aDoc重复上述操作
    将docName设置为aDoc的名称作为文本
    将exportName设置为(文件夹为文本)&docName
    将exportName设置为exportName的文本1到-8
    将exportName设置为(exportName&“csv”)
    告诉应用程序“数字”
    发射
    开放式aDoc
    重复此步骤,直到存在文档1
    延迟3
    结束重复
    告诉前台文件
    导出到文件exportName作为CSV
    关闭
    结束语
    结束语
    结束重复
    告诉应用程序“编号”退出
    终点
    
    • 注意:按照编码,这将覆盖与处理的每个编号文件同名的现有CSV文件(如果它们已经存在)。如果不想覆盖现有文件,则需要附加编码
    如果收到脚本错误

    Numbers出现错误:无法将文档“name”导出为“name”。你没有权限

    根据我的经验,Numbers文档在导出之前没有完全打开,增加
    delay
    命令的值可以解决这个问题。当然,这是假设一个人在目标number文档存在的文件夹中实际拥有写入权限

    或者可以在
    告诉前台文档
    块中引入一个错误处理程序,如果我的理论正确,在导出之前目标文档未完全加载,则该错误处理程序将提供额外的时间,例如:

    更改:

    告诉前台文档
    导出到文件exportName作为CSV
    关闭
    结束语
    
    至:

    告诉前台文档
    尝试
    导出到文件exportName作为CSV
    关闭
    论错误
    延迟3
    导出到文件exportName作为CSV
    关闭
    结束尝试
    结束语
    


    注意:主要示例AppleScript代码就是这样,不包含任何适当的错误处理。用户有责任根据需要添加任何适当的错误处理。请查看中的语句和语句。另见。请参阅上面的示例。

    以下是我在macOS High Sierra为我所做的工作:

  • 终端中
  • 触摸屏;开放式e-2csv;chmod+x2csv
    
    •这将创建一个名为numb2csv的空ASCII文本文件
    •默认情况下,在文本编辑中打开numb2csv •使numb2csv文件可执行。

  • 将下面进一步显示的示例AppleScript代码复制并粘贴到打开的numb2csv文件中
  • 保存并关闭numb2csv文件
  • 终端中执行numb2csv可执行文件,例如:
  • /numb2csv“$HOME/Documents”
    
    • 这创建了一个与我的Documents文件夹中的每个Number文档同名的CSV文件,而不是遍历任何嵌套文件夹
    示例AppleScript代码:

    #/usr/bin/osascript
    运行argv
    设置POSIX文件的文件路径(argv的项目1)
    将文件夹设置为文件路径作为别名
    告诉应用程序“系统事件”将DOC设置为扩展名为“number”的文件夹项
    在DOCS中使用aDoc重复上述操作
    将docName设置为aDoc的名称作为文本
    将exportName设置为(文件夹为文本)&docName
    将exportName设置为exportName的文本1到-8
    将exportName设置为(exportName&“csv”)
    告诉应用程序“数字”
    发射
    开放式aDoc
    重复此步骤,直到存在文档1
    延迟3
    结束重复
    
    tell application "System Events" to set theDocs to theFolder's items whose name extension = "numbers"
    
    execution error: Can’t make file "file.numbers" of application "System Events" into the expected type. (-1700)
    
    #!/usr/bin/osascript
    
    log "Start"
    
    property exportFileExtension : "csv"
    
    tell application "Finder"
        activate
        set sourceFolder to choose folder with prompt "Please select directory."
        set fileList to name of every file of sourceFolder
    end tell
    
    set the defaultDestinationFolder to sourceFolder
    
    repeat with documentName in fileList
        log "documentName: " & documentName
    
        set fullPath to (sourceFolder as text) & documentName
        log "fullPath: " & fullPath
    
        if documentName ends with ".numbers" then
            set documentName to text 1 thru -9 of documentName
    
            tell application "Finder"
                set newExportItemName to documentName & "." & exportFileExtension
                set incrementIndex to 1
                repeat until not (exists document file newExportItemName of defaultDestinationFolder)
                    set newExportItemName to ¬
                        documentName & "-" & (incrementIndex as string) & "." & exportFileExtension
                    set incrementIndex to incrementIndex + 1
                end repeat
            end tell
            set the targetFileHFSPath to ¬
                (defaultDestinationFolder as string) & newExportItemName
    
            tell application "Numbers"
                launch
                open fullPath
                with timeout of 1200 seconds
                    export front document to file targetFileHFSPath as CSV
                end timeout
                close
            end tell
        end if
    end repeat