A To be Universal Script for entering BBCode

Hello.

This is a consolidation of previous attempts.

I found out that I needed one for editing in Safari as well.
Huge thanks to Craig Williams for his tip in this thread.

It is most useful, when entering the quote, the url tag, and combinations of tags.

How it works:
You select some text you want to “BBCode” then you enter the codes delimited by spaces in the dialog box.

The url tag has this peculiar behavior that what comes after the url, is parsed to be put within the url tags.

I use to have bold, italic and underline tags on the outside of the color tags, so should you in order for it to work properly.

b color blue is an example of what I enter.
url in this thread is another example.
quote Bertan Russles Poodle is another one
i b u is yet another one.

It works with Safari, BBEdit, TextWrangler, TextEdit and Tex-Edit Plus
You should get at least the buttons in your local language.
It should work in Tiger too.

Enjoy! :slight_smile:


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?id=33516
script localButtons -- ©  KOENIG,Yvan
	property parent : AppleScript
	property Cancel_loc : missing value
	--	property Save_loc : missing value
	--	property Continue_loc : missing value
	property Ok_loc : missing value
	--	property Authenticate_loc : missing value
	--	property Skip_loc : missing value
	--	property Delete_loc : missing value
	--	property Eject_loc : missing value
	on initializeLocalizationValues()
		set localButtons's Cancel_loc to localButtons's getLocalizedString("Finder", "AL1") -- Annuler
		--	set localButtons's Save_loc to localButtons's getLocalizedString("Finder", "AL2") -- Enregistrer
		--set localButtons's Continue_loc to localButtons's getLocalizedString("Finder", "AL3") -- Continuer
		set localButtons's Ok_loc to my getLocalizedString("Finder", "AL4") -- OK
		--		set localButtons's Authenticate_loc to localButtons's getLocalizedString("Finder", "AL5") -- Authentifier
		--	set localButtons's Skip_loc to my getLocalizedString("Finder", "AL6") -- Ignorer
		--set localButtons's Delete_loc to localButtons's getLocalizedString("Finder", "AL7") -- Supprimer
		--	set localButtons's Eject_loc to localButtons's getLocalizedString("Finder", "AL8") -- Éjecter
	end initializeLocalizationValues
	on getLocalizedString(a, x)
		tell application a to return localized string x
	end getLocalizedString
end script
property parent : my localButtons
property scriptVersion : "0.2.1"
property BBCodeEditors : {"BBEdit", "Safari", "TextEdit", "TextWrangler", "Tex-Edit Plus"}
property scriptTitle : "BBCode Wrapper"
on run
	initializeLocalizationValues()
	-- tell application "Safari" to activate
	set currentApp to getCurrentAppsName() as list
	if BBCodeEditors contains currentApp then
		set currentApp to "" & currentApp
		if currentApp is "BBEdit" then
			doBBCodingInBBEdit()
		else if currentApp is "Safari" then
			doBBCodingInSafari()
		else if currentApp is "TextEdit" then
			doBBCodingInTextEdit()
		else if currentApp is "TextWrangler" then
			doBBCodingInTextWrangler()
		else if currentApp is "Tex-Edit Plus" then
			doBBCodingInTexPlus()
		end if
	else
		ShowSupportedEditorsAndDie()
	end if
end run

on doBBCodingInSafari()
	tell application "Safari"
		activate
		set theText to (do JavaScript "getSelection()" in its document 1)
	end tell
	set theResponse to my askForBBCode()
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	set theText to my wrapBBcode(theResponse, theText)
	set AppleScript's text item delimiters to tids
	set the clipboard to theText
	tell application "Safari" to activate
	tell application "System Events"
		keystroke "v" using command down
		delay 0.1
	end tell
end doBBCodingInSafari

on doBBCodingInTexPlus()
	
	tell application "Tex-Edit Plus"
		activate
		set theText to its selection
	end tell
	-- end tell
	set theResponse to my askForBBCode()
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	set theText to my wrapBBcode(theResponse, theText)
	set AppleScript's text item delimiters to tids
	tell application "Tex-Edit Plus"
		
		activate
		set its selection to theText
	end tell
	
	
end doBBCodingInTexPlus


on doBBCodingInTextEdit()
	
	tell application "TextEdit"
		activate
		tell its window 1
			activate
			tell application "System Events"
				tell application process "TextEdit"
					keystroke "c" using command down
					delay 0.1
				end tell
			end tell
			
		end tell
	end tell
	-- end tell
	set theText to the clipboard
	set theResponse to my askForBBCode()
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	set theText to my wrapBBcode(theResponse, theText)
	set AppleScript's text item delimiters to tids
	set the clipboard to theText
	tell application "TextEdit" to activate
	tell application "System Events"
		keystroke "v" using command down
		delay 0.1
	end tell
	
	
end doBBCodingInTextEdit

on doBBCodingInTextWrangler()
	tell application "TextWrangler"
		set theText to text of front text document
		set theSelection to the selection as string
		set theCharStart to (characterOffset of selection)
		set theResponse to my askForBBCode()
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
		set tmpStr to my wrapBBcode(theResponse, theSelection)
		set AppleScript's text item delimiters to tids
		set the selection to tmpStr
		set insertionPosition to (theCharStart + (length of tmpStr))
		--select (characters insertionPosition through (insertionPosition - 1)) of front text document
		select insertion point before character insertionPosition of front text document
	end tell
end doBBCodingInTextWrangler

on doBBCodingInBBEdit()
	tell application "BBEdit"
		set theText to text of front text document
		set theSelection to the selection as string
		set theCharStart to (characterOffset of selection)
		set theResponse to my askForBBCode()
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
		set tmpStr to my wrapBBcode(theResponse, theSelection)
		set AppleScript's text item delimiters to tids
		set the selection to tmpStr
		set insertionPosition to (theCharStart + (length of tmpStr))
		--select (characters insertionPosition through (insertionPosition - 1)) of front text document
		select insertion point before character insertionPosition of front text document
	end tell
end doBBCodingInBBEdit

on wrapBBcode(txtBBcodeCmd, theText)
	set exceptions to {"quote", "url"}
	set theCount to count words of txtBBcodeCmd
	set firstWord to word 1 of txtBBcodeCmd
	if firstWord is in exceptions then
		if firstWord is "quote" then
			set theResponse to firstWord & "=" & words 2 thru -1 of txtBBcodeCmd
			set tmpStr to "[" & theResponse & "]" & theText & "[/" & firstWord & "]"
		else -- url
			set theResponse to firstWord & "=" & theText
			set tmpStr to "[" & theResponse & "]" & words 2 thru -1 of txtBBcodeCmd & "[/" & firstWord & "]"
		end if
	else if theCount is 1 then
		set tmpStr to "[" & firstWord & "]" & theText & "[/" & firstWord & "]"
	else if theCount is 2 then
		if firstWord is "color" then
			set theResponse to firstWord & "=" & second word of txtBBcodeCmd
			set tmpStr to "[" & theResponse & "]" & theText & "[/" & firstWord & "]"
		else
			set theResponse to "" & word 2 of txtBBcodeCmd
			set tmpStr to wrapBBcode(theResponse, theText)
			set tmpStr to wrapBBcode(firstWord, tmpStr)
		end if
	else if theCount is greater than 2 then
		set theResponse to "" & words 2 thru -1 of txtBBcodeCmd
		set tmpStr to wrapBBcode(theResponse, theText)
		set tmpStr to wrapBBcode(firstWord, tmpStr)
	end if
	return tmpStr
end wrapBBcode

on askForBBCode()
	set genericFontIcon to a reference to file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GenericFontIcon.icns"
	tell me
		activate
		set theResponse to text returned of (display dialog "Wrap what tags around selection?" with title my scriptTitle buttons {my Cancel_loc, my Ok_loc} default answer "" default button 2 cancel button 1 with icon genericFontIcon)
	end tell
	return theResponse
end askForBBCode


on ShowSupportedEditorsAndDie()
	set stopIcon to a reference to file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:AlertStopIcon.icns"
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
	set EditorList to my BBCodeEditors as text
	set AppleScript's text item delimiters to tids
	set displayText to "Only the following editors are supported:

" & EditorList & "

I'm Quitting!"
	tell application "System Events"
		tell application process "Finder"
			activate
			display dialog displayText with title my scriptTitle buttons {my Ok_loc} with icon stopIcon
			error number -128
		end tell
	end tell
	
end ShowSupportedEditorsAndDie
-- 
on getCurrentAppsName()
	set Major to ((system attribute "sysv") mod 4096 div 16)
	if Major is 4 then -- TIGER
		tell application (path to frontmost application as string) to return (get name) -- from the post :
	else if Major is 5 then -- LEO
		tell application (path to frontmost application as Unicode text) to return (get name) -- from the post :
	else if Major is 6 then -- SNOWLEOPARD
		tell application (path to frontmost application as text) to return (get name)
	else
		tell me
			activate
			display alert "Versions of Mac Os X earlier than 10.4.0 is unsupported: your version is 10." & Major & "xx"
			error number -128
		end tell
	end if
end getCurrentAppsName

Best Regards

McUsr

Hello

Added support for TextWrangler, TextEdit and Tex-Edit Plus

Best Regards

McUsr

Hello.

Thanks to Yvan Koenig for asking me about superfluous calls to System Events and Finder.

The BBCode Wrapper should be a tad more responsive now!

Thanks!

Best Regards

McUsr

Hello.

Updated the documentation.

Ponders removing the whole thing from here to ScriptBuilders.

Best Regards

McUsr

Hello! :slight_smile:

I have debugged the WrabBBCode script, but utsing it from Safari seems broken, as I cant get the javascript get.selection() to work anymore.

Edit I may, but not right now! :slight_smile:

There were some problems with Applescripts text item delimiters, as I rewrote the “parser” to “parse” recursively.

it is smarter now, and handles things like u b color=red

It is better to edit posts offline anyway! :slight_smile:


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?id=33516
script localButtons -- ©  KOENIG,Yvan
	property parent : AppleScript
	property Cancel_loc : missing value
	--	property Save_loc : missing value
	--	property Continue_loc : missing value
	property Ok_loc : missing value
	--	property Authenticate_loc : missing value
	--	property Skip_loc : missing value
	--	property Delete_loc : missing value
	--	property Eject_loc : missing value
	on initializeLocalizationValues()
		set localButtons's Cancel_loc to localButtons's getLocalizedString("Finder", "AL1") -- Annuler
		--	set localButtons's Save_loc to localButtons's getLocalizedString("Finder", "AL2") -- Enregistrer
		--set localButtons's Continue_loc to localButtons's getLocalizedString("Finder", "AL3") -- Continuer
		set localButtons's Ok_loc to my getLocalizedString("Finder", "AL4") -- OK
		--		set localButtons's Authenticate_loc to localButtons's getLocalizedString("Finder", "AL5") -- Authentifier
		--	set localButtons's Skip_loc to my getLocalizedString("Finder", "AL6") -- Ignorer
		--set localButtons's Delete_loc to localButtons's getLocalizedString("Finder", "AL7") -- Supprimer
		--	set localButtons's Eject_loc to localButtons's getLocalizedString("Finder", "AL8") -- Éjecter
	end initializeLocalizationValues
	on getLocalizedString(a, x)
		tell application a to return localized string x
	end getLocalizedString
end script

property stopIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertStopIcon.icns")
property genericFontIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:GenericFontIcon.icns")
property parent : my localButtons
property cachespath : ((path to library folder from user domain as text) & "caches:" & "net.mcusr.bbcode.wrapper")

property BBCodeEditors : {"BBEdit", "Safari", "TextEdit", "TextWrangler", "Tex-Edit Plus"}
property scriptTitle : "BBCode Wrapper"
on run
	local prevBBCoding, script_cache
	try
		set script_cache to load script alias (my cachespath)
	on error
		script newScriptCache
			property prevBBCoding : ""
		end script
		
		set script_cache to newScriptCache
	end try
	set prevBBCoding to script_cache's prevBBCoding
	
	set AppleScript's text item delimiters to ""
	set appid to getfrontAppId()
	initializeLocalizationValues()
	set currentApp to getCurrentAppsName() as list
	
	
	if BBCodeEditors contains currentApp then
		set currentApp to currentApp as text
		if currentApp is "BBEdit" then
			doBBCodingInBBEdit(prevBBCoding, script_cache)
		else if currentApp is "Safari" then
			doBBCodingInSafari(prevBBCoding, script_cache)
		else if currentApp is "TextEdit" then
			doBBCodingInTextEdit(prevBBCoding, script_cache)
		else if currentApp is "TextWrangler" then
			doBBCodingInTextWrangler(prevBBCoding, script_cache)
		else if currentApp is "Tex-Edit Plus" then
			doBBCodingInTexPlus(prevBBCoding, script_cache)
		end if
		abortNicely({bundleIdFrontApp:appid})
	else
		ShowSupportedEditorsAndDie()
	end if
end run

on getfrontAppId() -- Returns bundleid of active app
	local frontappId
	set frontappId to ""
	tell application "System Events"
		set frontappId to bundle identifier of first application process whose frontmost is true
	end tell
	return frontappId
end getfrontAppId

on abortNicely(r) -- Returns Nothing
	-- R : {bundleIdFrontApp:frontappId}
	do shell script "open -b \"" & (bundleIdFrontApp of r) & "\""
end abortNicely

on doBBCodingInSafari(prevBBCoding, script_cache)
	tell application "Safari"
		activate
		do JavaScript "self.focus()"
		set theText to (do JavaScript "\"\"+window.getSelection();" in its document 1)
	end tell
	set theResponse to my askForBBCode(prevBBCoding, script_cache)
	if theResponse is not "" then
		set theText to my wrapBBcode(theResponse, theText)
		set the clipboard to theText
		tell application "Safari" to activate
		tell application "System Events"
			keystroke "v" using command down
			delay 0.1
		end tell
	end if
end doBBCodingInSafari

on doBBCodingInTexPlus(prevBBCoding, script_cache)
	
	tell application "Tex-Edit Plus"
		activate
		set theText to its selection
	end tell
	
	set theResponse to my askForBBCode(prevBBCoding, script_cache)
	if theResponse is not "" then
		set theText to my wrapBBcode(theResponse, theText)
		tell application "Tex-Edit Plus"
			
			activate
			set its selection to theText
		end tell
	end if
	
end doBBCodingInTexPlus

on doBBCodingInTextEdit(prevBBCoding, script_cache)
	
	tell application "TextEdit"
		activate
		tell its window 1
			activate
			tell application "System Events"
				tell application process "TextEdit"
					keystroke "c" using command down
					delay 0.1
				end tell
			end tell
			
		end tell
	end tell
	
	set theText to the clipboard
	set theResponse to my askForBBCode(prevBBCoding, script_cache)
	if theResponse is not "" then
		set theText to my wrapBBcode(theResponse, theText)
		set the clipboard to theText
		tell application "TextEdit" to activate
		tell application "System Events"
			keystroke "v" using command down
			delay 0.1
		end tell
	end if
end doBBCodingInTextEdit

on doBBCodingInTextWrangler(prevBBCoding, script_cache)
	tell application "TextWrangler"
		set theText to text of front text document
		set theSelection to the selection as string
		set theCharStart to (characterOffset of selection)
		set theResponse to my askForBBCode(prevBBCoding, script_cache)
		if theResponse is not "" then
			set tmpStr to my wrapBBcode(theResponse, theSelection)
			set the selection to tmpStr
			set insertionPosition to (theCharStart + (length of tmpStr))
			--select (characters insertionPosition through (insertionPosition - 1)) of front text document
			select insertion point before character insertionPosition of front text document
		end if
	end tell
end doBBCodingInTextWrangler

on doBBCodingInBBEdit(prevBBCoding, script_cache)
	tell application "BBEdit"
		set theText to text of front text document
		set theSelection to the selection as string
		set theCharStart to (characterOffset of selection)
		set theResponse to my askForBBCode(prevBBCoding, script_cache)
		if theResponse is not "" then
			set tmpStr to my wrapBBcode(theResponse, theSelection)
			set the selection to tmpStr
			set insertionPosition to (theCharStart + (length of tmpStr))
			select insertion point before character insertionPosition of front text document
		end if
	end tell
end doBBCodingInBBEdit

on wrapBBcode(txtBBcodeCmd, theText)
	-- når jeg legger inn farge ...
	
	set exceptions to {"quote", "url", "color"}
	
	set theCount to count words of txtBBcodeCmd
	if theCount is 1 then -- there may be more 
		if txtBBcodeCmd contains "=" then
			
			set thePos to offset of "=" in txtBBcodeCmd
			set first word of txtBBcodecmde to characters 1 thru (thePos - 1) of txtBBcodeCmd as text
			set txtBBcodeCmd to " " & (characters (thePos + 1) thru -1 of txtBBcodeCmd) as text
		else
			set firstWord to word 1 of txtBBcodeCmd
		end if
	else
		set firstWord to word 1 of txtBBcodeCmd
	end if
	set firstWord to word 1 of txtBBcodeCmd
	
	if firstWord is in exceptions then
		
		if firstWord is "quote" then
			set theResponse to firstWord & "=" & words 2 thru -1 of txtBBcodeCmd
			set tmpStr to "[" & theResponse & "]" & theText & "[/" & firstWord & "]"
		else if firstWord is "url" then
			set theResponse to firstWord & "=" & theText
			set tmpStr to "[" & theResponse & "]" & words 2 thru -1 of txtBBcodeCmd & "[/" & firstWord & "]"
		else -- color
			if (offset of space in firstWord) is 1 then
				set firstWord to characters 2 thru -1 of firstWord as text
			end if
			set theResponse to txtBBcodeCmd
			set tmpStr to "[" & theResponse & "]" & theText & "[/" & firstWord & "]"
			
		end if
	else if theCount is 1 then
		set tmpStr to "[" & firstWord & "]" & theText & "[/" & firstWord & "]"
	else if theCount is 2 then
		if firstWord is "color" then
			set theResponse to firstWord & "=" & second word of txtBBcodeCmd
			set tmpStr to "[" & theResponse & "]" & theText & "[/" & firstWord & "]"
		else
			set theResponse to "" & word 2 of txtBBcodeCmd
			set tmpStr to wrapBBcode(theResponse, theText)
			set tmpStr to wrapBBcode(firstWord, tmpStr)
		end if
	else if theCount is greater than 2 then
		set theResponse to characters ((length of firstWord) + 2) thru -1 of txtBBcodeCmd as text
		
		set tmpStr to wrapBBcode(theResponse, theText)
		set tmpStr to wrapBBcode(firstWord, tmpStr)
	end if
	return tmpStr
end wrapBBcode

on askForBBCode(prevBBCoding, script_cache)
	local theResponse
	tell me
		activate
		try
			set theResponse to text returned of (display dialog "Wrap what tags around selection?" default answer prevBBCoding with title my scriptTitle buttons {my Cancel_loc, my Ok_loc} default button 2 cancel button 1 with icon genericFontIcon)
			if theResponse ≠ prevBBCoding and theResponse is not "" then
				set script_cache's prevBBCoding to theResponse
				store script script_cache in cachespath replacing yes
			end if
		on error
			set theResponse to ""
		end try
	end tell
	return theResponse
end askForBBCode

on ShowSupportedEditorsAndDie()
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
	set EditorList to my BBCodeEditors as text
	set AppleScript's text item delimiters to tids
	set displayText to "The following editors are supported:

" & EditorList & "

Yours is not one of them: -> I'm Quitting!"
	tell me
		activate
		tell application "System Events"
			tell application process "Finder"
				activate
				display dialog displayText with title my scriptTitle buttons {my Ok_loc} with icon stopIcon
				error number -128
			end tell
		end tell
	end tell
end ShowSupportedEditorsAndDie
-- 
on getCurrentAppsName()
	set Major to ((system attribute "sysv") mod 4096 div 16)
	if Major < 4 then
		tell me
			activate
			display alert "Versions of Mac Os X earlier than 10.4.0 is unsupported: your version is 10." & Major & "xx"
			error number -128
		end tell
	else if Major is 4 then -- TIGER
		tell application (path to frontmost application as string) to return (get name) -- from the post :
	else if Major is 5 then -- LEO
		tell application (path to frontmost application as Unicode text) to return (get name) -- from the post :
	else if Major is 6 or Major > 6 then -- SNOWLEOPARD
		tell application (path to frontmost application as text) to return (get name)
	end if
end getCurrentAppsName

ScriptBuilders is no longer supported (unfortunately). A lot of good stuff in there is lost forever.

Hello!

I have fixed the JavaScripting, so now you can use it while editing online again! :smiley:

I have also made it to work a little bit better with Quicksilver, or any other taskswitcher.

I have posted the new copy in the post above.

Hello!

It became tedious to type in b color=blue again and again. So now the default answer is stored in a script property file, so I can use it with Quicksilver.

The script is in post #5.

Edit

I have made it terminate gracefully when you cancel insertion of BBcode.

I have modifed the Os X Version checking so it works for LIon and Mountain Lion. :expressionless:

Sorry about that!