繁体   English   中英

在 MacOS 上使用 AppleScript 打开一个新的 Safari Window 而不会搞砸

[英]Open a new Safari Window on MacOS with AppleScript without messing things up

我试图写一个小的 AppleScript 它将打开一个新的 Safari Window。 此 window 应显示特定的 URL 并应以给定尺寸打开。

额外的约束是:

  1. 它不应打开额外的 window。
  2. 它不应该通过留下我给定的大小来扰乱 Safari 的设置,使所有新的 windows 都是那个大小。

我想出的是:

-- 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

这似乎有点麻烦,也许这里有人知道如何在没有所有窗口的情况下更顺利地做到这一点 - “闪烁”?

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

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. 调整 window 的大小。
  3. 关闭 Safari。
  4. 打开 Safari。

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

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. 调整 window 的大小。
  3. 打开一个新的 window。
  4. 调整为您想要的默认大小。
  5. 关闭 window。
  6. 关闭 Safari。
  7. 打开 Safari。

新的 window 应该具有新的默认尺寸。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM