For*_*orm 2 safari scripting applescript scripting-bridge
我已经制作了一个非常简单的AppleScript来关闭Safari中的标签.问题是,它有效,但不完全.只关闭了几个标签.这是代码:
tell application "Safari"
repeat with aWindow in windows
repeat with aTab in tabs of aWindow
if [some condition is encountered] then
aTab close
end if
end repeat
end repeat
end tell
Run Code Online (Sandbox Code Playgroud)
我也试过这个脚本:
tell application "Safari"
repeat with i from 0 to the number of items in windows
set aWindow to item i of windows
repeat with j from 0 to the number of tabs in aWindow
set aTab to item j of tabs of aWindow
if [some condition is encountered] then
aTab close
end if
end repeat
end repeat
end tell
Run Code Online (Sandbox Code Playgroud)
...但它也不起作用(相同的行为).
我在我的系统(MacBook Pro jan 2008)以及Tiger下的Mac Pro G5上尝试了这一点,并且脚本在两者上都失败了,尽管Tiger的描述错误要少得多.
运行该脚本几次会关闭几个选项卡,直到没有剩下任何一个,但在关闭几个选项卡后总是失败并出现相同的错误.在Leopard下我得到一个越界错误.由于我使用快速枚举(不使用"从0重复到窗口中的项目数量"),我不知道如何通过此方法获得越界错误...
我的目标是使用Cocoa Scripting Bridge从我的Objective-C Cocoa应用程序中关闭Safari中的选项卡,但是Scripting Bridge以相同的方式失败.不可删除的选项卡显示NULL在Xcode调试器中,而其他选项卡是有效的对象,我可以从中获取值(例如它们的标题).事实上,我首先尝试使用脚本桥然后告诉自己为什么不直接在AppleScript中尝试这个,我很惊讶地看到相同的结果.
我必须有一个明显的遗漏或其他东西...(似乎是Safari AppleScript支持我的一个错误...:S)我使用重复循环和Obj-C 2.0快速枚举来迭代集合之前的零问题,所以我真的没有看到这里有什么问题.
有人可以帮忙吗?
提前致谢!
小智 6
我有一个脚本关闭所有选项卡但不需要重复循环.
set closeTab to "Stack Overflow" as string
tell application "Safari"
close (every tab of window 1 whose name is not equal to closeTab)
end tell
Run Code Online (Sandbox Code Playgroud)
看看它是否适合你.
注意:将"Stack Overflow"更改为要保持打开的选项卡的标题名称.
这对我来说很简单
tell application "Safari"
close every window
end tell
Run Code Online (Sandbox Code Playgroud)
确定,您必须从计数到1,否则当您关闭窗口时,计数将关闭
tell application "Safari"
repeat with i from (count of windows) to 1 by -1
repeat with j from (count of tabs of window i) to 1 by -1
set thistab to tab j of window i
set foo to name of thistab
if foo is not equal to "bar" then close thistab
end repeat
end repeat
end tell
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12064 次 |
| 最近记录: |