Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #######################
- -->> ABOUT
- #######################
- (*
- Phil Stokes -- 2018
- applehelpwriter.com
- sqwarq.com
- *)
- #######################
- -->> DESCRIPTION
- #######################
- (*
- If you use Slack, this script will allow you to write Slack-style formatting
- in a BBEDit document and then, through the 'Preview in BBEdit' menu item in BBEdit's
- 'Markdown' menu, it'll produce a simulation of what your message would look like in Slack.
- Useful if you're in a Slack group with editing timeouts but you have to post lengthy or heavily formatted messages.
- DEPENDENCIES: BBEdit either while still in trial mode or with a license, as you need the Markdown features.
- This script does NOT require Slack.app, but does assume you know how to format Slack messages.
- *)
- #######################
- -->> USAGE
- #######################
- (*
- For a full description of how to use it, see the blog post and video
- 'BBEdit: how to preview Slack messages' on applehelpwriter.com at
- http://applehelpwriter.com/2018/02/23/bbedit-how-to-preview-slack-messages/
- *)
- #######################
- -->> IMPORT STATEMENTS
- #######################
- use AppleScript version "2.4" -- Yosemite (10.10) or later
- use scripting additions
- use framework "Foundation"
- #######################
- -->> GLOBALS, CONSTANTS, etc
- #######################
- property NSMutableArray : a reference to current application's NSMutableArray
- property NSCharacterSet : a reference to current application's NSCharacterSet
- property NSString : a reference to current application's NSString
- property NSArray : a reference to current application's NSArray
- set previewDivider to "<strong><span style=\"color:darkBlue\">Slack Preview:</span></strong>"
- set breakLine to "<br></br>"
- set newLine to "
- "
- # debug symobls
- # set debugArray to NSArray's arrayWithArray:{}
- # set debugString to NSString's stringWithString:""
- #######################
- -->> HANDLERS
- #######################
- on replaceFormatString:oldFormat withTag:htmlTag inStr:aString
- set tagCount to 0
- set theMutant to NSMutableArray's new()
- set mStr to NSString's stringWithString:aString
- set slackStr to NSString's stringWithString:oldFormat
- set htmlStrOpen to NSString's stringWithFormat_("<%@>", htmlTag)
- set htmlStrClose to NSString's stringWithFormat_("</%@>", htmlTag)
- set theArray to mStr's componentsSeparatedByString:slackStr
- # set my debugArray to NSArray's arrayWithArray:theArray
- set tagCount to ((count of theArray) as list)
- repeat with i from 1 to tagCount
- set index_ to (i - 1)
- if index_ is greater than 0 and index_ mod 2 is not 0 then -- we're looking for odd numbers, 1, 3, 5...
- if oldFormat is "+++" then -- our dummy tag, remember, for Slack's '>>>'
- try
- set theFormatString to (theArray's objectAtIndex:index_)
- on error
- set theFormatString to (theArray's objectAtIndex:(index_ - 1))
- end try
- set theFormatString to (theFormatString's stringByReplacingOccurrencesOfString:(my newLine) withString:(NSString's stringWithFormat_("%@ ", my newLine)))
- set theNewString to NSString's stringWithFormat_("%@ %@%@", htmlStrOpen, theFormatString, htmlStrClose)
- end if
- -- this is actually for Slack's '>' code
- if oldFormat is ">>>" then
- try
- set theFormatString to (theArray's objectAtIndex:index_)
- on error
- set theFormatString to (theArray's objectAtIndex:(index_ - 1))
- end try
- set nl to (theFormatString's rangeOfString:(my newLine))
- if nl's |length| is greater than 0 then
- set preStr to (theFormatString's substringToIndex:(nl's location))
- set postStr to (theFormatString's substringFromIndex:((nl's location) + (nl's |length|)))
- set theNewString to NSString's stringWithFormat_("%@ %@%@%@%@", htmlStrOpen, preStr, htmlStrClose, my newLine, postStr)
- end if
- end if
- -- everything except the quote and @mentions tags, which are non-surrounding and a PIA!, we can deal with here:
- if tagCount is greater than 2 then
- set theFormatString to (theArray's objectAtIndex:index_)
- set theNewString to NSString's stringWithFormat_("%@%@%@", htmlStrOpen, theFormatString, htmlStrClose)
- end if
- try
- if ((count of theMutant) as list) < index_ then
- (theMutant's addObjectsFromArray:theArray)
- end if
- on error errMsg number errNum
- display dialog errNum & return & errMsg buttons "OK" default button "OK" with icon 1 with title "Slack Preview script"
- error number -128
- end try
- try
- (theMutant's replaceObjectAtIndex:index_ withObject:theNewString)
- on error errMsg
- display dialog errMsg & return & return & "Perhaps more helpfully: Is one of the " & oldFormat & " tags missing?" buttons "OK" default button "OK" with icon 1 with title "Slack Preview script"
- error number -128
- end try
- end if
- end repeat
- set theArray to (NSArray's arrayWithArray:theMutant)
- -- at last lets return and get out of here!
- return theArray's componentsJoinedByString:" "
- end replaceFormatString:withTag:inStr:
- on removePreviousPreviewOf:aText
- -- hmm, nothing much to say here, does what it says on the tin
- set m to NSString's stringWithString:aText
- set curtail to (m's rangeOfString:"<br></br>")
- if curtail's |length| > 0 then
- set m to (m's substringToIndex:(curtail's location))
- end if
- set m to m's stringByTrimmingCharactersInSet:(NSCharacterSet's whitespaceAndNewlineCharacterSet())
- return m as text
- end removePreviousPreviewOf:
- on colourMentions:aNSString startsWith:aBool
- set theMutant to NSMutableArray's new()
- set anArray to aNSString's componentsSeparatedByString:"@"
- (theMutant's addObjectsFromArray:anArray)
- set tagCount to ((count of anArray) as list)
- -- some finicking about because AS lists are 1-indexed but Obj-C arrays are 0-indexed
- set tagCount to tagCount - 1
- if tagCount > 0 then
- repeat with i from 1 to tagCount
- set index_ to i
- set theFormatString to (anArray's objectAtIndex:index_)
- set mentionString to theFormatString
- set spaceRange to (theFormatString's rangeOfString:" ")
- if spaceRange's |length| > 0 then
- set mentionString to (theFormatString's substringToIndex:(spaceRange's location))
- set theRest to (theFormatString's substringFromIndex:(spaceRange's location))
- else
- set theRest to ""
- end if
- set theNewString to NSString's stringWithFormat_("<span style=\"color:#0074BC; background-color: #EAF5FC;\">@%@</span>%@", mentionString, theRest)
- (theMutant's replaceObjectAtIndex:index_ withObject:theNewString)
- end repeat
- set anArray to (NSArray's arrayWithArray:theMutant)
- return anArray's componentsJoinedByString:" "
- else
- return aNSString
- end if
- end colourMentions:startsWith:
- on showPreview()
- tell application "BBEdit"
- set mentionStart to false -- default assumption that the content doesn't start with an '@' sign
- -- list of formatting we're currently supporting
- set tagList to {{slack:"```", html:"pre"}, {slack:"`", html:"code"}, {slack:"*", html:"strong"}, {slack:"_", html:"em"}, {slack:"~", html:"del"}, {slack:">>>", html:"blockquote"}, {slack:"+++", html:"blockquote"}} # '+++' is a dummy here, not a real Slack formatting option
- set m to contents of front document
- if m's text item 1 is "@" then set mentionStart to true
- -- if this isn't the first run, remove any previous preview
- set m to my removePreviousPreviewOf:m
- set m to m & my newLine
- -- reassign and replace the quote tags so that we can process them properly later
- set mStr to (NSString's stringWithString:m)
- set mStr to mStr's stringByReplacingOccurrencesOfString:">>>" withString:"+++"
- set mStr to mStr's stringByReplacingOccurrencesOfString:">" withString:">>>"
- repeat with aTag in tagList
- -- does the text contain the current tag word in the repeat loop? Let's remove unwanted detritus created by previous iterations through the loop
- if (mStr's containsString:(aTag's slack)) then
- set previewRange to (mStr's rangeOfString:(my previewDivider))
- set loc to previewRange's location
- if previewRange's |length| is 0 then
- set loc to mStr's |length| as integer
- end if
- set mStr to (mStr's substringToIndex:(loc))
- -- most of the heavy lifting is done here:
- set mStr to (my replaceFormatString:(aTag's slack) withTag:(aTag's html) inStr:mStr)
- end if
- end repeat
- -- now that we have the correct HTML tags, we can add styling to suit here
- set mStr to mStr's stringByReplacingOccurrencesOfString:"<blockquote>" withString:"<blockquote style=\"border-left: 5px solid; border-color:lightgrey;\">"
- set mStr to mStr's stringByReplacingOccurrencesOfString:"<code>" withString:"<code style=\"color:red; background-color: snow;font-size:1.2em; padding: 2px; border-style:solid;border-color: #d9d5d5; border-radius: 5px ;border-width: 2px;\">"
- set mStr to mStr's stringByReplacingOccurrencesOfString:"<pre>" withString:"<pre style=\"background-color: snow; font-size: 1.3em; padding: 6px; border-style:solid;border-color: #d9d5d5; border-radius: 5px ;border-width: 2px; color: #5a5a5a\">"
- set mStr to mStr's stringByReplacingOccurrencesOfString:(my newLine) withString:"<br/>"
- set spanOpen to "<span style=\"font-size:1em; font-family:Lucida Grande;line-height: 1.6em;\">"
- set spanClose to "</span>"
- set mStr to NSString's stringWithFormat_("%@%@%@", spanOpen, mStr, spanClose)
- -- one last bit of processing is to style the '@someone' keywords
- set mStr to my colourMentions:mStr startsWith:mentionStart
- -- at last, let's load up our formatted text!
- set m to m as text
- set contents of front document to m & my newLine & my newLine & my breakLine & my newLine & my previewDivider & my newLine & my breakLine & mStr
- end tell
- end showPreview
- #######################
- -->> COMMANDS
- #######################
- my showPreview() # call everything above!
- -- let's make the Preview window frontmost if it exists
- tell application "BBEdit"
- set didOpen to false
- set winList to every window
- repeat with win in winList
- set f to name of win
- if f contains "Preview" then
- set visible of win to true
- set didOpen to true
- activate
- end if
- end repeat
- -- if it doesn't exist,, lets say so:
- if didOpen is false then
- display dialog "Please open the Preview window in BBEdit (Control-Command-P), or from the menu bar, choose 'Markup > Preview in BBEdit'." buttons "OK" default button "OK" with icon 1 with title "Slack Preview script"
- end if
- end tell
- #EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement