1

I've started learning Applescript - and know no JavaScript - and am having problems getting this code to work.

I'm trying to get open a webpage in Safari and once the page has finished loading, execute some javascript against it.

The javascript is copied from a bookmarklet - when I use the bookmarklet button, the javascript works as expected. However when running my Applescript, it returns a "missing value" error and does nothing to the webpage.

Here is the Applescript:

set makeReadable to "javascript:(function()%7B_readableOptions=%7B'text_font':'quote(Palatino Linotype), Palatino, quote(Book Antigua), Georgia, serif','text_font_monospace':'quote(Courier New), Courier, monospace','text_font_header':'quote(Times New Roman), Times, serif','text_size':'14px','text_line_height':'1.5','box_width':'30em','color_text':'%23282828','color_background':'%23F5F5F5','color_links':'%230000FF','text_align':'normal','base':'blueprint','custom_css':''%7D;if(document.getElementsByTagName('body').length>0);else%7Breturn;%7Dif(window.$readable)%7Bif(window.$readable.bookmarkletTimer)%7Breturn;%7D%7Delse%7Bwindow.$readable=%7B%7D;%7Dwindow.$readable.bookmarkletTimer=true;window.$readable.options=_readableOptions;if(window.$readable.bookmarkletClicked)%7Bwindow.$readable.bookmarkletClicked();return;%7D_readableScript=document.createElement('script');_readableScript.setAttribute('src','http://readable-static.tastefulwords.com/target.js?rand='+encodeURIComponent(Math.random()));document.getElementsByTagName('body')%5B0%5D.appendChild(_readableScript);%7D)();"

tell application "Safari"
set theURL to "http://en.wikipedia.org/wiki/Rocky"
open location theURL
delay 20

set SelectedPage to document of window 1
do JavaScript makeReadable in SelectedPage
delay 15
end tell

Any assistance greatly appreciated!

4
  • what is the bookmarklet suppose to do Commented Sep 14, 2013 at 5:03
  • 1
    It converts a standard webpage article into a "readable" format, similar to Safari's built in "Reader" function. Commented Sep 14, 2013 at 5:14
  • your delays are way long just put in delay 1 after the open location and you should be good on the delays Commented Sep 14, 2013 at 5:15
  • yep, they were only that long while I was trying to work out why it wasn't working... thought it may have been running too quick :) thanks though! Commented Sep 14, 2013 at 5:25

1 Answer 1

1

You cannot just paste a bookmarklet (which is an executable URL) into a do JavaScript

I have no experience with AppleScript, but try

set makeReadable to "(function(){_readableOptions={'text_font':'quote(Palatino Linotype), Palatino, quote(Book Antigua), Georgia, serif','text_font_monospace':'quote(Courier New), Courier, monospace','text_font_header':'quote(Times New Roman), Times, serif','text_size':'14px','text_line_height':'1.5','box_width':'30em','color_text':'#282828','color_background':'#F5F5F5','color_links':'#0000FF','text_align':'normal','base':'blueprint','custom_css':''};if(document.getElementsByTagName('body').length>0);else{return;}if(window.$readable){if(window.$readable.bookmarkletTimer){return;}}else{window.$readable={};}window.$readable.bookmarkletTimer=true;window.$readable.options=_readableOptions;if(window.$readable.bookmarkletClicked){window.$readable.bookmarkletClicked();return;}_readableScript=document.createElement('script');_readableScript.setAttribute('src','http://readable-static.tastefulwords.com/target.js?rand='+encodeURIComponent(Math.random()));document.getElementsByTagName('body')[0].appendChild(_readableScript);})();"
Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Great thanks very much. Now to read through the change you made and work out how to apply it in the future... Thanks again!
I just changed the entities %XX to their appropriate characters {}[] and #

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.