Getting Window ID on Google Chrome

I’ve started using Google Chrome on my Ventura computer and decided to get some basic AppleScript stuff working. I looked at the dictionary and searched this forum, but one thing that has me stumped is window ID. I’m probably making some rookie mistake but how do I get the window ID? Thanks.

tell application id "com.google.Chrome"
	set tabName to title of tab 1 of window 1 -- returns correct value
	set tabURL to URL of tab 1 of window 1 -- returns correct value
	set tabID to id of tab 1 of window 1 --> 7.45034274E+8
	set windowName to title of window 1 --> returns correct value
	set windowBounds to bounds of window 1 --> returns correct value
	set windowID to id of window 1 --> 7.45034273E+8
end tell
1 Like

There’s nothing wrong with the results you’re getting.
It’s just big numbers displayed as floats.
Try this: paste 1000000000 in a script window and compile…

Add this line at the end of your script and you will see that it will retrieve the right window:

return title of window id windowID = windowName
2 Likes

@ionah. Thanks for the explanation and suggested code, which worked great.

Just as a general aside, I had wanted to use the window ID from Chrome with the screencapture commandline utility as shown below. This works fine with Safari but not Chrome, and I assume that the window IDs are not functionally the same.


-- this works
tell application "Safari"
	activate
	delay 1
	set activeAppID to id of window 1
end tell
do shell script "screencapture -ox -l " & activeAppID & space & quoted form of "/Users/Robert/Desktop/Safari SS.png"

-- this doesn't work
tell application "Google Chrome"
	activate
	delay 1
	set activeAppID to id of window 1 --> 7.45036456E+8
end tell
set activeAppID to activeAppID as inches as text --> "745036456"
do shell script "screencapture -ox -l " & activeAppID & space & quoted form of "/Users/Robert/Desktop/Chrome SS.png"

Don’t know why Chrome is returning a so big number for the ID.
It may be encoded incorrectly.

Why do you need to use the window ID?
When activating Chrome, the first window is necessarily the good one.

do shell script "screencapture -ox " & "/Users/Robert/Desktop/Chrome SS.png"

Thanks @ionah. I need the screencapture to be of the window only, and that’s why I wrote the script as I did. Screencapture has a -w option which allows the user to click on the window that will be captured, and I’ll use that. The window ID returned by Google Chrome is very large, which is what had me confused to begin with. Anyways, I pretty much understand what’s going on now.

@peavine
Instead of relying on window ID, you could use the window bounds:

tell application "Google Chrome"
	activate
	set {theLeft, theTop, theRight, theBottom} to bounds of window 1
	set theBounds to ("" & theLeft & "," & theTop & "," & theRight - theLeft & "," & theBottom - theTop)
	do shell script "screencapture -R " & theBounds & space & quoted form of "/Users/Robert/Desktop/Chrome SS.png"
end tell

@ionah. That’s an excellent solution. Not only does it solve the issue with Google Chrome, but, by getting window position/size with System Events, the script is easily made to work with other apps that are not scriptable.

tell application "System Events"
	set activeApp to first process whose frontmost is true
	set {x, y} to position of window 1 of activeApp
	set {w, h} to size of window 1 of activeApp
	set theBounds to ("" & x & "," & y & "," & w & "," & h)
end tell
set ssFile to (POSIX path of (path to desktop)) & "windowcapture.png"
do shell script "screencapture -R " & theBounds & space & quoted form of ssFile