Created
August 4, 2017 15:43
-
-
Save jasonwiener/89dde448a209bc7ecca80cbe6dbc90fe to your computer and use it in GitHub Desktop.
BBEdit wrap selection (or create empty) HTML tag with CSS class using AppleScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- tag input options: | |
-- div -> <div>{SELECTED TEXT IF ANY}</div> | |
-- div.myclass -> <div class="myclass">{SELECTED TEXT IF ANY}</div> | |
on splitText(theText, theDelimiter) | |
set AppleScript's text item delimiters to theDelimiter | |
set theTextItems to every text item of theText | |
set AppleScript's text item delimiters to "" | |
return theTextItems | |
end splitText | |
tell application "BBEdit" | |
tell text of front text document | |
set theText to contents of selection | |
end tell | |
end tell | |
set tagName to text returned of (display dialog "tag: " buttons {"Cancel", "Set"} with title "OK" cancel button "Cancel" default button "Set" default answer "") | |
set theClass to "" | |
set theOffset to offset of "." in tagName | |
if theOffset > 0 then | |
set theSplit to splitText(tagName, ".") | |
set tagName to item 1 of theSplit | |
set className to item 2 of theSplit | |
set theClass to " class=\"" & className & "\"" | |
end if | |
set theText to "<" & tagName & theClass & ">" & theText & "</" & tagName & ">" | |
tell application "BBEdit" | |
tell text of front text document | |
set text of selection to theText | |
end tell | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment