Created
April 5, 2013 09:22
-
-
Save robjwells/5317919 to your computer and use it in GitHub Desktop.
Prefixr AppleScript with formatting
This file contains hidden or 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
tell application "BBEdit" | |
-- Get selection and call the Prefixr API | |
set theSel to the contents of the selection of text 1 of text window 1 | |
set prefixed to (do shell script "curl -sSd css=\"" & theSel & "\" http://prefixr.com/api/index.php") | |
-- Calculate current indent | |
set initialSpaces to "" | |
repeat | |
set spaceOffset to the offset of " " in theSel | |
if spaceOffset is not 1 then | |
exit repeat | |
else | |
set initialSpaces to initialSpaces & " " | |
set theSel to (characters 2 through -1 of theSel) as text | |
end if | |
end repeat | |
-- Break prefixed text by line | |
set AppleScript's text item delimiters to return | |
set theLines to the text items of prefixed | |
set AppleScript's text item delimiters to "" | |
-- Ensure -ms- appears before -o- | |
repeat with aLine in theLines | |
if aLine starts with "-o-" then | |
set operaLine to contents of aLine | |
set operaPos to my lpos(operaLine, theLines) | |
else if aLine starts with "-ms-" then | |
set msLine to contents of aLine | |
set msPos to my lpos(msLine, theLines) | |
end if | |
end repeat | |
if operaPos < msPos then | |
set item operaPos of theLines to msLine | |
set item msPos of theLines to operaLine | |
end if | |
-- Line up each line by colon position | |
repeat with aLine in theLines | |
set theOffset to the offset of ":" in aLine | |
if aLine starts with "-webkit-" then | |
set webkitOffset to the theOffset | |
set bbText to initialSpaces & aLine | |
else | |
set extraSpaces to "" | |
repeat (webkitOffset - theOffset) times | |
set extraSpaces to (" " & extraSpaces) | |
end repeat | |
set bbText to (bbText & return & initialSpaces & extraSpaces & aLine) | |
end if | |
end repeat | |
-- Return text to BBEdit | |
set the contents of the selection of text 1 of text window 1 to bbText | |
end tell | |
-- List position handler | |
on lpos(this_item, this_list) | |
repeat with i from 1 to count of this_list | |
if item i of this_list is this_item then return i | |
end repeat | |
end lpos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment