Quantcast
Viewing all articles
Browse latest Browse all 11

getWindowByName and getTabByURL

was studying AppleScript and Ruby on Leopard for a while.

I created some AppleScript functions for Safari 3.1 on Leopard.

getWindowByName

on getWindowByName(appName)
    tell application "System Events"
        set isRunning to (count of (every process whose name is appName)) > 0
        if not isRunning then return missing value
    end tell
    tell application appName
        repeat with thisWindow in (every window whose document of it is not missing value)
            return thisWindow
        end repeat
        return missing value
    end tell
end getWindowByName
getWindowByName("Safari")

This returns an object of a Safari window, even if the window is hidden, minimized or in other Spaces.

getTabByURL

on getTabByURL(thisWindow, strURL)
    tell application "Safari"
        repeat with thisTab in tabs of thisWindow
            if URL of thisTab starts with strURL then
                return thisTab
            end if
        end repeat
        return missing value
    end tell
end getTabByURL
set thisWindow to getWindowByName("Safari")
if thisWindow is missing value then return
getTabByURL(thisWindow, "http://hahlo.com/")

This returns an object of a tab which displays “http://hahlo.com/”.


Viewing all articles
Browse latest Browse all 11

Trending Articles