user2778459
user2778459

Reputation: 13

Javascript code not working from Applescript?

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!

Upvotes: 1

Views: 287

Answers (1)

mplungjan
mplungjan

Reputation: 178422

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);})();"

Upvotes: 1

Related Questions