0

我正在尝试使用 Applescript 处理来自一个工作表的输入,以创建另一个具有特定名称的工作表。我可以毫无问题地创建新工作表,但如果我运行脚本两次,它(适当地)给我一个错误,因为具有该名称的工作表已经存在。

这是我尝试过的,但它在 if 语句上给了我一个错误('Can't get sheet who name = "[the value of nextTuesdaysName]"'):

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)

tell application "Numbers"
    tell document 1

        if (sheet whose name is nextTuesdaysName) exists then
               display dialog "There's already a worksheet named '" & 
                      nextTuesdaysName & "'" buttons {"Quit", "Replace"} default button 2 with icon 1
            # delete (first sheet whose name is nextTuesdaysName)
        end if
        set thisSheet to make new sheet with properties {name:nextTuesdaysName}
    end tell
end tell

如何构造 if 语句以检查命名工作表是否存在?

TIA

4

2 回答 2

0

@拉斯,

AppleScript 可以很好地检查某个对象是否存在而不会引发任何错误。你的错误是使用whom子句。正确的语法:

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)

tell application "Numbers" to tell document 1
    if (sheet nextTuesdaysName) exists then
        display dialog "There's already a worksheet named '" & nextTuesdaysName & "'" buttons {"Quit", "Replace"} default button 2 with icon 1
        -- delete (sheet nextTuesdaysName)
    end if
    set thisSheet to make new sheet with properties {name:nextTuesdaysName}
end tell
于 2021-07-06T09:32:39.447 回答
0

此脚本尝试制作新工作表。当它失败时——由于任何错误——它会咳出一个有两个选择的对话。“替换”选项首先删除该名称的现有工作表,然后创建另一个工作表。更新:包括特定的错误信息。

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)
tell application "Numbers"
set nextTuesdaysName to "somethingorother"
tell document 1
    try
        set thisSheet to make new sheet with properties {name:nextTuesdaysName}
    on error number -2763
        display dialog "Worksheet '" & nextTuesdaysName & "' already exists" buttons {"Quit", "Replace"} default button 2 with icon 1
        
        if button returned of the result is "Replace" then          
            delete sheet nextTuesdaysName
            set thisSheet to make new sheet with properties {name:nextTuesdaysName}
            
        end if
    end try
end tell
end tell

这是一种不使用错误处理的替代方法,而是添加一个 if...then 来测试工作表的存在。两者现在都包含基于日期的工作表命名机制。

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)

tell application "Numbers"
    tell document 1
        if name of sheets contains nextTuesdaysName then
            display dialog "There's already a worksheet named '" & nextTuesdaysName & "'" buttons {"Quit", "Replace"} default button 2 with icon 1
            
            if button returned of the result is "Replace" then
                delete sheet nextTuesdaysName
                set thisSheet to make new sheet with properties {name:nextTuesdaysName}
            end if
        else
            set thisSheet to make new sheet with properties {name:nextTuesdaysName}
        end if
    end tell
end tell
于 2021-07-04T02:23:47.700 回答