Applescript 检查特定数字 sheet 是否存在
Applescript check if specific Numbers sheet exists
我正在尝试使用 Applescript 处理来自一个作品的输入sheet 以创建另一个具有特定名称的作品。我可以毫无问题地创建新的 sheet,但是如果我 运行 脚本两次,它(适当地)会给我一个错误,因为具有该名称的 sheet 已经存在。
这是我试过的方法,但它在 if 语句上给了我一个错误('无法获取 sheet whose 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 语句来检查命名的 sheet 是否存在?
TIA
此脚本试图创建一个新的 sheet。当它失败时——由于任何错误——它会弹出一个有两个选择的对话。 'replace' 选择首先删除该名称的现有 sheet,然后创建另一个。更新:包括具体的错误信息。
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 来测试 sheet 是否存在。两者现在都包含基于日期的 sheet 命名机制。
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
@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, 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
我正在尝试使用 Applescript 处理来自一个作品的输入sheet 以创建另一个具有特定名称的作品。我可以毫无问题地创建新的 sheet,但是如果我 运行 脚本两次,它(适当地)会给我一个错误,因为具有该名称的 sheet 已经存在。
这是我试过的方法,但它在 if 语句上给了我一个错误('无法获取 sheet whose 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 语句来检查命名的 sheet 是否存在?
TIA
此脚本试图创建一个新的 sheet。当它失败时——由于任何错误——它会弹出一个有两个选择的对话。 'replace' 选择首先删除该名称的现有 sheet,然后创建另一个。更新:包括具体的错误信息。
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 来测试 sheet 是否存在。两者现在都包含基于日期的 sheet 命名机制。
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
@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, 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