Search code examples
cocoaapplescriptapplescript-objc

Pass a window as argument to a function in applscript


I am working on automation of a website and want to make it a little modular. for example the code would be as follows:

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

problem with this approach is that if the theIndexOfW=3 and window with index 2 was closed by mistake, the script crashes, I can see there is property windowId, and I don't want to iterate over all the windows as that it is inefficient. What would be the best way to do it?


Solution

  • Windows Id is a persistant parameter even if other windows are closed. So use windowId instead of theIndexofW.