将 window 作为参数传递给 applescript 中的函数

Pass a window as argument to a function in applscript

我正在致力于网站的自动化,并希望将其模块化。例如代码如下:

global _width
global _height
global windowId
global baseURL
global theIndexOfW
on openApplicationWindow()
    tell application "Google Chrome"
         set w to make new window with properties {bounds:{50, 50, _width * (0.9), _height * (0.9)}}
         set URL of active tab of w to (baseURL)
         set windowId to id of w
         set theIndexOfW to index of w
    end tell
end openApplicationWindow

on repeatUrls()
    tell application "Google Chrome"
        set w to get window theIndexOfW
        if w exists then
            #do something
        else
            my openApplicationWindow()
        end if

    end tell
end repeatUrls

这种方法的问题是,如果索引为 2 的 theIndexOfW=3 和 window 被错误地关闭,脚本会崩溃,我可以看到有 属性 windowId ,我不想遍历所有 windows,因为它效率低下。 最好的方法是什么?

Windows Id 是一个永久参数,即使其他 windows 已关闭。所以使用 windowId 而不是 theIndexofW.