[TxMt] Easy insert image? (HTML)

Allan Odgaard throw-away-1 at macromates.com
Mon May 22 03:35:47 UTC 2006


On 22/5/2006, at 4:53, Sean Schertell wrote:

> Hmm... that's a bit of a disappointment that the only way to get  
> auto height/width is with the mouse.  Is it possible to add this  
> functionality with a command?

Sure it is -- it is already done with a drag command, you just need  
to make it a regular command that e.g. uses AS to prompt for the  
filename.

Here’s a quick proof-of-concept, set input: none, output: insert as  
snippet:

img=$(osascript -e 'tell app "SystemUIServer"' -e activate -e 'return  
POSIX path of (choose file with prompt "Pick an image:")' -e 'end tell')

err=$?
osascript -e 'tell app "TextMate" to activate' &>/dev/null &

[[ $err == 0 ]] || exit_discard

src=${img#$(dirname "${TM_FILEPATH:-}")/}
echo -n "<img src=\"$src\" "

sips -g pixelWidth -g pixelHeight "$img" \
|awk '/pixelWidth/  { printf("width=\"%d\" ",  $2) }
       /pixelHeight/ { printf("height=\"%d\" ", $2) }'

base=${img##*/}
alt=$(tr <<<${base%.*} '[_-]' ' '|perl -pe 's/(\w+)/\u$1/g')
echo -n "alt=\"\${1:$alt}\" />"


Or starting with next (cutting edge) build, just click this file:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Insert Image.tmCommand
Type: application/octet-stream
Size: 1187 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20060522/7b753b53/attachment.tmCommand>


More information about the textmate mailing list