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!
-- 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