Applescript - 数字不会被关闭"无标题"

时间:2018-06-08 06:07:42

标签: applescript

我试图做一个将数据粘贴到Numbers中的AppleScript,然后将其导出为csv文件。

除了Numbers保留我之前的文档并且每次都打开它们之外,它一切正常。这导致了一个新的"无题"每次脚本运行时,如果脚本运行10次 - 数字将首先打开10个先前的文档。 我正在尝试退出而不保存关闭此文档而不保存而没有结果:(。

每次启动Numbers时,如何使数字不打开旧的未保存文件?

on run

set theFilePath to "APPLE SSD SMO128B Media:Users:Henrik:Desktop:my.csv"

tell application "Numbers"
    activate -- If script is run once before the old document that is exported but not saved will open here too
    set thisDocument to make new document
    tell thisDocument
        delete every table of every sheet
    end tell
end tell

tell application "System Events" to keystroke "v" using {option down, shift down, command down}
delay 1
tell application "Numbers"
    export front document as CSV to file theFilePath
    close thisDocument without saving
    delay 1
end tell
tell application "Numbers" to quit without saving

结束

1 个答案:

答案 0 :(得分:1)

为避免 Numbers 表格重新打开您创建的文件,您首先需要将文档实际保存到文件中,然后让 Finder 将其删除。 您可以使用以下代码:

set documentName to "Test"
set targetFolder to path to downloads folder
set myFile to ((targetFolder as string) & documentName & ".numbers")

tell application "Numbers"
    set myDocument to make new document
    
    #Save the file, to delete it just afterward
    save myDocument in file myFile
    
    # Export the document
    set exportFileName to documentName & ".pdf"
    set the targetFilePath to ((targetFolder as string) & exportFileName)
    export myDocument to file targetFilePath as PDF
    
    close
    quit saving no
end tell

tell application "Finder" to delete file myFile