【问题标题】:Open a new Safari Window on MacOS with AppleScript without messing things up使用 AppleScript 在 MacOS 上打开一个新的 Safari 窗口,而不会搞砸
【发布时间】:2021-07-10 03:42:00
【问题描述】:

我尝试编写一个小的 AppleScript 来打开一个新的 Safari 窗口。此窗口应显示特定的 URL,并应以给定的大小打开。

额外的约束是:

  1. 不应打开额外的窗口。
  2. 它不会影响 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


【解决方案1】:

这是我根据您的上一条评论使用的代码。随意更新您的需求,这不是一个真正正确的答案,但我希望它可以帮助您实现目标,祝您好运!

global DEFAULT_BOUNDS, CUSTOM_BOUNDS
-- set DEFAULT_BOUNDS to {x:missing value, y:missing value, w:missing value, h:missing value}
 set DEFAULT_BOUNDS to {x:210, y:146, w:621, h:491}
set CUSTOM_BOUNDS to {x:21, y:46, w:621, h:191}

-- part1() -- un/commen as needed
part2() -- un/comment as needed
  1. 启动 Safari。
  2. 调整窗口大小。
  3. 关闭 Safari。
  4. 打开 Safari。

新窗口应该有您调整大小的尺寸。

to part1()
    activate application "Safari"
    tell application "System Events" to tell process "Safari" to set theWindow to first window
    resize(theWindow, CUSTOM_BOUNDS)
    tell application "Safari" to quit
    repeat while running of application "Safari" is true
        delay 1
    end repeat
    activate application "Safari"
end part1

现在试试这个:

  1. 启动 Safari。
  2. 调整窗口大小。
  3. 打开一个新窗口。
  4. 调整为您想要的默认大小。
  5. 关闭那个窗口。
  6. 关闭 Safari。
  7. 打开 Safari。

新窗口应该有新的默认尺寸。

to part2()
    set theFirstWindow to newSafariWindow("https://www.example.com")
    resize(theFirstWindow, CUSTOM_BOUNDS)
    set theNewWindow to newSafariWindow("https://www.yahoo.com")
    resize(theNewWindow, DEFAULT_BOUNDS)

    tell application "Safari" to close first window
    tell application "Safari" to quit
    repeat while running of application "Safari" is true
        delay 1
    end repeat

    activate application "Safari"
end part2

一些辅助处理程序

on saveCurrentBounds(referenceWindow)
    tell application "System Events"
        set thePosition to position of referenceWindow
    
        set x of DEFAULT_BOUNDS to first item of thePosition
        set y of DEFAULT_BOUNDS to second item of thePosition
        set theSize to size of referenceWindow
    
        set w of DEFAULT_BOUNDS to first item of theSize
        set h of DEFAULT_BOUNDS to second item of theSize
    end tell
end saveCurrentBounds

on newSafariWindow(theUrl as text)
    if running of application "Safari" is false then
        do shell script ("open -a Safari " & theUrl)
    
        script WindowWaiter
            if (count of (windows of application "Safari")) is not 0 then return true
        end script
        wait on WindowWaiter
    else
        tell application "Safari" to make new document with properties {URL:theUrl}
    end if
    delay 1
    tell application "System Events" to tell process "Safari" to     return front window
end newSafariWindow

to resize(theWindow, newBounds)
    tell application "System Events"
        set position of the theWindow to {x of newBounds, y of newBounds}
        set size of the theWindow to {w of newBounds, h of newBounds}
    end tell
 end resize


to wait on scriptObj by sleep : 1 for ntimes : 1000
    repeat ntimes times
        try
            set handlerResult to run of scriptObj
            if handlerResult is not missing value then return handlerResult
        end try
        delay sleep
    end repeat
    return missing value
end exec

【讨论】:

  • 谢谢,但我给你的评论是让你看到调整大小 - 关闭的东西实际上会影响新打开的窗口的大小。但是我在您的代码中看到了看起来非常有用的技巧。非常感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多