问题描述
我正在尝试使用 Applescript 处理来自一个工作表的输入,以创建另一个具有特定名称的工作表。我可以毫无问题地创建新工作表,但是如果我运行该脚本两次,它(适当地)会给我一个错误,因为具有该名称的工作表已经存在。
这是我尝试过的,但它在 if 语句中给了我一个错误('无法获取名称 =“[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
解决方法
此脚本尝试制作新工作表。当它失败时——由于任何错误——它会发出一个有两个选择的对话。 “替换”选项首先删除该名称的现有工作表,然后创建另一个工作表。更新:包括具体的错误信息。
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,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
,
@Russ,
AppleScript 可以很好地检查某个对象是否存在而不会抛出任何错误。你的错误是使用了 whose 子句。正确的语法:
set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear,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