Open Current Safari URL in BBEdit through Tidy

168 views
Skip to first unread message

Rich F

unread,
Dec 23, 2014, 4:32:16 PM12/23/14
to bbe...@googlegroups.com
Hi folks.

Just got through a couple scripts to open the current URL in Safari into BBEdit.  Then I inserted a quick Tidy directive to clean it up.  I review ugly pages.  

AppleScript: Current URL to BBEdit.scpt:

tell application "Safari" to set myU to the URL of the current tab in window 1
set alpha to "ruby /Users/YOURNAME/Library/Scripts/YOURSCRIPTS/BBEdit_Current_Page.rb '" & myU & "' | /usr/local/bin/bbedit"
do shell script alpha
tell application
"BBEdit" to activate



And the Ruby file: BBEdit_Current_Page.rb:

#!/usr/bin/ruby

require 'open-uri'
require 'nokogiri'

url = ARGV[0].to_s
page = Nokogiri::HTML(open(url))
pagestring = page.to_s

cmd = %w{ tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes --force-output yes --indent yes --indent-spaces 2 --join-classes yes --join-styles yes --markup yes --output-html yes --quiet yes --show-errors 0 --show-warnings no --sort-attributes alpha --tab-size 2 --uppercase-attributes no --uppercase-tags no --vertical-space true --wrap 0}

result = IO.popen(cmd, 'r+') {|io| 
  io.puts pagestring
  io.close_write
  io.read
}

puts result



Obviously, change the path to wherever your Ruby script is.  

Cheers








Lawrence San

unread,
Dec 23, 2014, 7:19:11 PM12/23/14
to BBEdit-Talk
I use a Firefox extension called "View Source With" to open the current Firefox URL into BBEdit. Unlike other "view source" approaches, this attempts to open the actual server source (assuming you have permission to get to it, such as with your own development server), rather than create a clone -- often of the browser's interpretation of the source -- as most "view source" methods do.

I'm curious as to how your script compares to that. I don't know Ruby, but are you cloning the source, or are you attempting to access the actual server source file? I'm guessing it's the former, because it looks like you're doing some reformatting in your code, but I can't tell for sure.


Rich F

unread,
Dec 25, 2014, 12:22:46 PM12/25/14
to bbe...@googlegroups.com
It's interpreted, then just reflowed.  No replacements other than classes and styles.  Those parameters sent to Tidy can be changed.  

The point of this isn't to get my server code, but to grab the odd page and open it up in BBEdit to peruse the DOM, etc.  

Tommy Bollman

unread,
Jan 31, 2015, 9:02:46 AM1/31/15
to bbe...@googlegroups.com
Hello.

I use this as a bookmark in Safari. I have used RCDefaults, so the txtmt protocol triggers TextWrangler.

javascript:(function(){var%20pgsrc;try{pgsrc=window.frames[0].location.toString();}catch(err){pgsrc=window.location.href.toString();}var%20file_loc=pgsrc.indexOf('file://');if(file_loc==0){var%20segm_loc=pgsrc.indexOf('#');if(segm_loc>0){var%20tmpS=pgsrc.substr(0,segm_loc);pgsrc=tmpS;}var%20twCmd="txmt://open/?url="+pgsrc;window.open(twCmd,"_self");}})();

The code looks like this, before it is "minified", and it is made to work with my personal system, where I have pages inside a frame, in index.html. 

Version 2: Checks if the file protocol is in use, and chops off any segment identifier

(function () {
	var pgsrc ;
	try {
		pgsrc= window.frames[0].location.toString();
	}
	catch( err ) {
		pgsrc = window.location.href ;
	}
	var file_loc = pgsrc.indexOf('file://') ;
	if ( file_loc == 0 ) {
		var segm_loc = pgsrc.indexOf('#');
		if (segm_loc > 0 ) {
			// we have a segment identifier  on the end of the url.
			var tmpS = pgsrc.substr(0,segm_loc) ;
			pgsrc = tmpS ;
		}
		var twCmd = "txmt://open/?url="+pgsrc ;
		window.open(twCmd ,"_self");

	}
})();

Jimmbo

unread,
Feb 7, 2015, 1:17:55 PM2/7/15
to bbe...@googlegroups.com
Rich F,

Thanks, this is much needed. But the script is not working for me. I'm getting this response:

sh: /usr/local/bin/bbedit: No such file or directory
ruby: No such file or directory -- /Users/MYNAME/Library/Application (LoadError) number 127

I can't figure out what redirect is being referenced here.
Obviously my applications folder isn't at ~/Library.
BBedit lives at /Application/BBEdit (well, actually /Applications/BBEdit/BBEdit)

...but the "/usr/local/bin" bit is over my head.

Rich F

unread,
Feb 7, 2015, 7:39:51 PM2/7/15
to bbe...@googlegroups.com
You have to install BBEdit's command line tools, in the BBEdit menu.  

Todd Wachob

unread,
Feb 8, 2015, 9:17:37 AM2/8/15
to bbe...@googlegroups.com
From the first line in the error message, it looks like the BBEdit
command line tools aren't installed. You can do this from BBEdit->
Install Command Line Tools menu command, but only for the version of
BBEdit purchased directly from Bare Bones Software.

Unfortunately, I can't help with the Ruby error.

HTH,

Todd
Reply all
Reply to author
Forward
0 new messages