Last active
November 12, 2021 17:10
-
-
Save mixio/fd3919d33f80d4f6797662b8aca44fbb to your computer and use it in GitHub Desktop.
This BBEdit applescript will try to expand css abbreviations using Emmet.
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
## | |
# README | |
# | |
# This script will try to expand Emmet css abbreviations. | |
# If nothing is selected in the frontmost document, | |
# it will use the expression at the left of the insertion point | |
# otherwise it will use the selected expression. | |
# | |
# See: https://github.com/emmetio/emmet | |
# | |
# INSTALL | |
# | |
# Install Emmet as a global node module with npm: | |
# | |
# % npm -g install emmet | |
# | |
# To check where is the npm global node_modules parent directory: | |
# | |
# % npm root --quiet -g | |
# | |
# To list installed modules. | |
# | |
# % npm list -g --depth=0 | |
# | |
## | |
try | |
set vNodeBinPath to "/usr/local/bin/node" -- CHECK: should be the absolute path to node's executable. | |
set vNodeModulesPath to "/usr/local/lib/node_modules" -- CHECK: should be the absolute path to the global node_modules directory. | |
tell application "BBEdit" | |
tell first document of first window | |
if class of its window's selection is insertion point then | |
-- Select abbreviation to the left of the insertion point. | |
set vMatch to find "(\\S+)" searching in it options {backwards:true, search mode:grep} with selecting match | |
select insertion point before selection of its window | |
set vMatch to find "(\\S+)" searching in it options {backwards:false, search mode:grep, extend selection:true} with selecting match | |
if length of selection of its window = 0 then | |
-- Couldn't match. Exit. | |
tell current application to beep | |
return | |
end if | |
else if class of its window's selection is character then | |
-- User knows best! Use the selection as the abbreviation. | |
else | |
-- Discontinuous selection. Exit. | |
tell current application to beep | |
return | |
end if | |
set vAbbreviation to (its window's selection) as string | |
set vCode to "" | |
set vCode to vCode & "const emmet = require('emmet');" | |
set vCode to vCode & "console.log(emmet.default(" & (the quoted form of vAbbreviation) & ", { type: 'stylesheet' }));" | |
set vScript to "" | |
set vScript to vScript & "NODE_PATH=" & (the quoted form of vNodeModulesPath) & space | |
set vScript to vScript & vNodeBinPath & space | |
set vScript to vScript & "--eval" & space | |
set vScript to vScript & the quoted form of vCode | |
-- log vScript | |
set vResult to do shell script (vScript) | |
-- log vResult | |
set (its window's selection) to vResult | |
-- Place the insertion point after the expansion. | |
select insertion point after its window's selection | |
end tell | |
end tell | |
on error aMessage | |
display alert "CSS abbreviation: " & the quoted form of vAbbreviation & " could not be expanded by Emmet." as critical | |
end try |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment