Locate and edit markdown counterpart of html file displayed in Safari

Hi!

This is the counterpart of my previous post, since markdown documents may be hyperlinked!

I have used webpage helper from the article at Mac OS X Autmation to implement it. (AppleScript: Launching Scripts From Links) You have to follow the article, and save my script into the webpagehelper app that can be downloaded, while you are at it, I have posted a script further down, that replaces the reveal application script of their app, as it is not practical!

I access the services of Webpagehelper.app through bookmarks:

This snippet goes into the main of the Webagehelper.app


property warnIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertCautionIcon.icns")

-- © McUsr 2012 you may not post it elsewhere or store it in a public accessible repository 
-- but refer to this link http://macscripter.net/edit.php?id=154895

property main_property : {}

on run
	local myUnescapedUrl, mrkDownCounterPart
	try
		
		if my isAvalidHtmlFileUrl(main_property) then
			
			set myUnescapedUrl to my decodefurl(main_property)
			
			
			set mrkDownCounterPart to findMarkdownCounterPart of me for myUnescapedUrl
			
			if mrkDownCounterPart = null then
				
				tell application "BBEdit"
					activate
					open myUnescapedUrl
					replace "\\n" using "\\r" searching in text 1 of text window 1 options {search mode:grep, starting at top:true}
					select insertion point before character 1 of text window 1
				end tell
				return true
			else if mrkDownCounterPart = false then
				
				tell application "SystemUIServer"
					activate
					display dialog "Ambiguity for file:" & return & return & myUnescapedUrl & return & "More than one markdown file with corresponding name  in sourcetree." buttons {"OK"} default button 1 with title "Webpage Helper" with icon warnIcon
				end tell
				return false
			else
				tell application "BBEdit"
					activate
					open mrkDownCounterPart
					select insertion point before character 1 of text window 1
				end tell
				return true
			end if
			
		else
			tell application "SystemUIServer"
				activate
				display dialog main_property & " is not a valid html document"
			end tell
			return false
		end if
		
	on error e number n
		logMessage2("WebPageHelper", " Edit current webfilebrombrowserinbbedit -- Error: " & e & " " & n)
		
	end try
end run

to findMarkdownCounterPart for ahtmlfile
	local pxFolder, nm, suf, parFol, counterPart, ct, markdowncounterpart
	set {pxFolder, nm, suf} to splitPxFile of me for ahtmlfile
	set parFol to parentFol of me for pxFolder
	set counterPart to ""
	
	set counterPart to paragraphs of (do shell script "find " & quoted form of parFol & " -name " & quoted form of (nm & ".text") & " -maxdepth 3 -print 2>/dev/null  || echo >/dev/null")
	set ct to length of counterPart
	if ct = 0 then
		return null
	else if ct > 1 then
		return false
	else -- if ct = 1 then
		set markdownlCounterPart to POSIX file counterPart as alias as text
		return markdownlCounterPart
	end if
end findMarkdownCounterPart

on logMessage2(procName, Msg)
	-- needs a decl like this in the run handler (implicit or explicit )
	do shell script "/usr/bin/logger -s -t " & quoted form of procName & " " & quoted form of Msg
end logMessage2

to splitPxFile for pxPath
	-- http://macscripter.net/viewtopic.php?id=39332
	local ppl, sl, s, tids, f
	set ppl to length of pxPath
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
	
	set s to item -1 of text items of pxPath
	set sl to length of s
	if sl = ppl then
		set s to missing value
		set pxPath to text items 1 thru -1 of pxPath as text
	else
		set pxPath to text items 1 thru -2 of pxPath as text
	end if
	set AppleScript's text item delimiters to "/"
	
	set f to item -1 of text items of pxPath
	if f = "" then set f to missing value
	try
		set pxPath to (items 1 thru -2 of text items of pxPath)
		set pxPath to (pxPath as text) & "/"
	on error
		set pxPath to missing value
	end try
	set AppleScript's text item delimiters to tids
	return {pxPath, f, s}
end splitPxFile

to parentFol for aPxPath
	local tids, parFol
	set {tids, my text item delimiters} to {my text item delimiters, "/"}
	
	if (character (length of aPxPath) of aPxPath) = "/" then
		set parFol to text items 1 thru -3 of aPxPath as text
	else
		set parFol to text items 1 thru -2 of aPxPath as text
	end if
	set my text item delimiters to tids
	if parFol = "" then set parFol to "/"
	return parFol
end parentFol


on isAvalidHtmlFileUrl(theUrl)
	local ok, astid
	log "here"
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	if not text item 1 of theUrl is "file" then
		set AppleScript's text item delimiters to astid
		return false
	end if
	set AppleScript's text item delimiters to "."
	if not text item -1 of theUrl is "html" then
		set AppleScript's text item delimiters to astid
		return false
	end if
	
	set AppleScript's text item delimiters to astid
	return true
end isAvalidHtmlFileUrl

on decodefurl(anUrlFromABrowser)
	-- 27/08/12 Tested!
	-- konverterer escaped chars tilbake til normal 
	-- fjerner file, og local host. 
	-- localhost starter helt til å begynne med i tilfelle.
	local tmpUrl
	
	set tmpUrl to my rawURLDecode(anUrlFromABrowser)
	
	set tmpUrl to my str_replace({substringToReplace:"file://", replacementString:"", OriginalString:tmpUrl})
	
	if (offset of "localhost" in tmpUrl) is 1 then set tmpUrl to text 10 thru -1 of tmpUrl
	
	return tmpUrl
end decodefurl

--  DJ Bazzie Wazzie http://macscripter.net/edit.php?id=154949
on rawURLDecode(str)
	return do shell script "/bin/echo -n " & quoted form of str & " | php -r ' echo rawurldecode(fgets(STDIN)); '"
end rawURLDecode

on str_replace(R) -- Returns modified string
	-- R {substringToReplace: _ent, replacementString: _rent,OriginalString: _str}
	local _tids
	set _tids to AppleScript's text item delimiters
	set AppleScript's text item delimiters to R's substringToReplace
	set _res to text items of R's OriginalString as list
	set AppleScript's text item delimiters to R's replacementString
	set _res to items of _res as string
	set AppleScript's text item delimiters to _tids
	return _res
end str_replace


Do yourself a favor and use this for revealing the Webpagehelper folder.


” © McUsr 2012 you may not post it elsewhere or store it in a public accessible repository 
” but refer to this link http://macscripter.net/edit.php?id=154895

set this_app to path to me
tell application "Finder"
	activate
	set visible of (every process whose visible is true and frontmost is false) to false
	set mpx to quoted form of (POSIX path of this_app as text)
	do shell script "open -R " & mpx
	reveal this_app
end tell
return true


:slight_smile:

The findMarkdownCounterPart handler is reinforced, due to permission errors from the find command when running it high up in the folder hierarchy.

Hello!

I have updated the decoding of urls to do the exact thing according to RFC 3986, as the old handlers only really considered 7-bit ascii, and not Utf-8.

Hello!

I have declared the variables local in the run handler, and put on a nicer warn icon for the dialog.