【发布时间】:2021-07-10 03:42:00
【问题描述】:
我尝试编写一个小的 AppleScript 来打开一个新的 Safari 窗口。此窗口应显示特定的 URL,并应以给定的大小打开。
额外的约束是:
- 不应打开额外的窗口。
- 它不会影响 Safari 的设置,将我给定的大小留在后面,使所有新窗口都变成那个大小。
我想出的是这样的:
-- Explanations in Comments.
on run
tell application "Safari"
set myURL to "https://…long url for The Moth FM…"
activate
-- activate will open a new empty window when Safari is not running.
-- It will bring any other window to the front when it's running.
try
set oldBounds to bounds of front window
-- I try to remember the bounds of the (new) window.
if "favorites://" = (URL of document of front window) then
-- if the window was an empty one, it was most probably a new window.
close front window
-- try to close it.
end if
on error errStr number errNum
-- all the above might fail.
-- So we do not know the original window size at the moment.
make new document with properties {URL:"favorites://"}
-- I open a brand new window,
set oldBounds to bounds of front window
-- so that I get the bounds of new windows.
close front window
-- Then I directly close it again.
end try
-- All the above is just preparation to rememver Safari's
-- default Window size (and position).
make new document with properties {URL:myURL}
-- Now I create the window I want
set bounds of front window to {21, 46, 621, 191}
-- and set its size.
-- This size has now become Safari's new default size. :(
make new document with properties {URL:"favorites://"}
-- So I create another window
set bounds of front window to oldBounds
-- and set its size to the default size retrieved above
close front window
-- and close it, as I don't need it.
end tell
end run
这似乎有点麻烦,也许这里有人知道如何在没有所有窗口“闪烁”的情况下更顺利地做到这一点?
【问题讨论】:
-
检查 Safari 是否有窗口(或正在运行)与没有窗口是否更简单?这就是我实现
newTab的方式。我也会使用处理程序来帮助提高可读性。 -
感谢@user3579815 的建议。你能告诉我如何可靠地做到这一点吗?我所有的尝试都非常不可靠。尽管如此,我仍然需要用旧尺寸打开和关闭一个窗口,以免我的小窗口成为新标准,对吧?
-
我试了一下,启动一个新窗口,然后启动一个临时窗口,并将其调整到所需尺寸不会使新窗口采用首选大小。必须有更好的方法来解决您的要求。我用来管理 Windows 的一件事是 Magnet 应用程序。对于自定义,我使用 AppleScript 来调整窗口的大小并将它们定位在我想要的位置。我不再依赖默认大小,当我这样做时,这很烦人:D
-
@user3579815 试试这个:启动 Safari。调整窗口大小。关闭 Safari。打开 Safari。新窗口应该有您调整大小的尺寸。现在试试这个:启动 Safari。调整窗口大小。打开一个新窗口。调整为您想要的默认大小。关闭那个窗口。关闭 Safari。打开 Safari。新窗口应该有新的默认尺寸。
标签: macos safari applescript