Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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...

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

There are three problems with the script you posted, and one of them may be just a problem that was caused when you pasted the code into your question.

Starting from the beginning: You need to tell Chrome where to do the JavaScript, like this:

tell application "Google Chrome"
    tell active tab of window 1 to execute javascript "your script"
end tell

That will run the script in the current tab of the first window you opened (AFAIK, you can't yet get the active tab of the active window).

Next, you don't want that javascript: prefix; Chrome knows it's JavaScript.

Finally, you're missing a paren at the very end of your line. Again, I'm not sure if this is just a copy problem, but it's something to be aware of.

Making the above changes, I have this code:

tell 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

...which works.

share|improve this answer
    
you absolute beauty!!! you wont believe how much i enjoy using this script, such a timesaver. –  rapscalli Jul 25 '11 at 18:42
    
@rapscalli if this solved the problem, don't forget to accept the answer by clicking the check mark next to it. :) –  Nathan Greenstein Jul 25 '11 at 22:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.