Google Chrome: close tab and run javascript

Hello,

Google’s newest release of Chrome now supports AppleScript and I’m rewriting a few of my simple Safari scripts for use with Google’s browser. However, I’ve hit a few roadblocks and was hoping to get some info from the smart people here.

First, how to close a tab. In Safari you can run:

tell application "Safari" to close current tab of window 1

but in Chrome you cannot do the equivalent:

tell application "Google Chrome" to close active tab of window 1

You get the message:
error “Google Chrome got an error: active tab of window 1 doesn’t understand the close message.” number -1708 from active tab of window 1
This seems to me a somewhat basic request, but I can’t figure out how to do it (without employing user scripting).

Secondly, how to run javascript. For instance, in Safari you can run:

tell application "Safari" to do JavaScript "getSelection();" in document 1

The “do JavaScript” command is not in Chrome’s dictionary, but there is the “execute” command:
execute‚v : Execute a piece of javascript.
execute specifier : The tab to execute the command in.
javascript text : The javascript code to execute.
→ any
I’ve tried every combination I can think of to get the getSelection JavaScript to execute, but with no luck.
Much appreciated is any insight provided. Both of these are relatively simple, but they’ve already provided my daily limit of frustration, and Chrome’s scripting abilities are so new that I can’t find any place on the www that has an answer. Cheers.

Well, the syntax is a bit funny, but it does what it states :slight_smile:


tell application "Google Chrome"
	set chromewindows to every window
	
	repeat with chromewindow in chromewindows
		set windowtabs to every tab of chromewindow
		repeat with windowtab in windowtabs
			tell windowtab
				delete
			end tell
		end repeat
	end repeat
end tell

I love to see them support AppleScript.

Best regards from cold Berlin,

Martin

This seems to be the correct syntax, however, it always returns missing value in my tests:


tell application "Google Chrome"
	tell window 1
		tell tab 1
			execute javascript "document.title"
		end tell
	end tell
end tell

Piyomaro Software also reports this:

[url=http://piyocast.com/as/archives/1484]http://piyocast.com/as/archives/1484[/url]

Way to go, Google :slight_smile:

Thanks very much Michael.

Really unfortunate for Google to include entries in the AppleScript dictionary and then not support them. Hopefully this will change in the future, although a new version of Chrome just came out and js remains broken.

As to the ‘close tab’ request, your script seems to close every tab (and window) - I was hoping to close only the active tab. But thank you for your help.

I highly doubt you still need it, but maybe someone else is looking for it:


tell application "Google Chrome"
	delete tab (active tab index of window 1) of window 1
end tell

translation of the middle part: close tab number [fill in the blank] of the frontmost window