Applescript to open javascript bookmark

i have the following javascript that searches the current site (very handy)

javascript:Qr=prompt('Search%20Site%20for','');if(Qr)location.href='http://www.google.com/search?&q=site:'+encodeURIComponent(window.location.hostname)+'+'+escape(Qr)

I am a launchbar user, but launchbar will not open this javascript bookmark.

Can i create an applescript that runs the javascript bookmark (in chrome, which is my default browser)

Have tried:-

tell application "Google Chrome"	
	execute javascript "javascript:Qr=prompt('Search%20Site%20for','');if(Qr)location.href='http://www.google.com/search?&q=site:'+encodeURIComponent(window.location.hostname)+'+'+escape(Qr"
end tell

but nothing happens…

In Safari, simply making a dummy bookmark, then changing the bookmark address to the desired javascript works.

Is that different from what you want?

Peter B.


I use Peter’s method in Safari, but you can do it in an applescript too (an example for Readability, but use your JS):


(*
The following JavaScript from Arc90 "makes reading on the web more enjoyable by removing the clutter around what you're reading" and the Readability site allows you to set Style, Size and Margin in the material. It is normally used by dragging a link from the Readability page to your Bookmarks Bar, and that puts the JavaScript below as the content of the bookmark. The page thus created in your browser has a link on it to return to the original source.

Visit "http://lab.arc90.com/experiments/readability/" for your own copy.

Because I'm an avid Quicksilver fan (but there are lots of alternatives), I prefer to set up a trigger to run the script below so I can enhance the readability of an article I'm looking at with a simple key combination. Although it cannot render every site I visit, it does remarkably well.
-- Note that the script below has been altered for readability. The original is all one string.
*)

set JS to "readStyle='style-novel';
	readSize='size-large';
	readMargin='margin-wide';
	_readability_script=document.createElement('SCRIPT');
	_readability_script.type='text/javascript';
	_readability_script.src='http://lab.arc90.com/experiments/readability/js/readability.js?x='+(Math.random());
	document.getElementsByTagName('head')[0].appendChild(_readability_script);
	_readability_css=document.createElement('LINK');
	_readability_css.rel='stylesheet';
	_readability_css.href='http://lab.arc90.com/experiments/readability/css/readability.css';
	_readability_css.type='text/css';
	_readability_css.media='screen';
	document.getElementsByTagName('head')[0].appendChild(_readability_css);
	_readability_print_css=document.createElement('LINK');
	_readability_print_css.rel='stylesh  eet';
	_readability_print_css.href='http://lab.arc90.com/experiments/readability/css/readability-print.css';
	_readability_print_css.media='print';
	_readability_print_css.type='text/css';
	document.getElementsByTagName('head')[0].appendChild(_readability_print_css);"

tell application "System Events" to set last_app to item 1 of (get name of processes whose frontmost is true)
if last_app is "Safari" then
	tell document 1 of application "Safari" to do JavaScript JS
else if last_app is "NetNewsWire" then
	tell document 1 of application "NetNewsWire" to do JavaScript JS
else
	beep 3
end if

hi peter,

i have a bookmark set up in chrome that runs the javascript fine. the problem i have is that the bookmark can’t be opened by launchbar…

so i wanted to create an applescript to open the bookmark, and run the applescript from launchbar…

Hi adam, as i mentioned to peter i want to open the bookmark via launchbar which won’t open the javascript bookmark…

this is the reason i wanted to try and run via applescript.

i amended your attached code to"

set JS to "javascript:Qr=prompt('Search%20Site%20for','');if(Qr)location.href='http://www.google.com/search?&q=site:'+encodeURIComponent(window.location.hostname)+'+'+escape(Qr)"
tell application "System Events" to set last_app to item 1 of (get name of processes whose frontmost is true)
tell application "System Events" to set last_app to item 1 of (get name of processes whose frontmost is true)
if last_app is "Safari" then
	tell document 1 of application "Safari" to do JavaScript JS
else
	beep 3
end if

but nothing at all happens (not even the ‘beep 3’)

any ideas?

This works here on 10.4.11 and Safari 4.1.3…


set JS to "javascript:Qr=prompt('Search Site For:','');if(Qr)location.href='http://www.google.com/search?&q=site:'+encodeURIComponent(window.location.hostname)+'+'+escape(Qr)"

tell application "Safari"
	activate
	try
		document 1
	on error
		make new document
	end try
	do JavaScript JS in document 1
end tell

Peter B.


hi peter, many thanks for the reply

i have that working now with safari, but after amending the script for google chrome get the following error

Well, I guess you’re on your own…

I don’t have Chrome or its AS dictionary, so can’t tell how it might be different from Safari’s.

Peter B.


Discussed in part here:

http://macscripter.net/viewtopic.php?id=34505

Don’t know whether things have changed or not.

Peter B.


this:

application “Google Chrome”
tell active tab of window 1 to execute javascript “Qr=prompt(‘Search%20Site%20for’,‘’);if(Qr)location.href='http://www.google.com/search?&q=site:‘+encodeURIComponent(window.location.hostname)+’+'+escape(Qr)
end tell

works!!