Enlarge Text Area.

Last update for tonight: <a class='button' onclick='javascript:document.getElementById("the_id").rows+=10;'> Make Text Box Bigger </a> And similar for Make Text Boxes Smaller. It’s working on my comment box as we speak. Try it out. (Thanks to Dunstan).

Posted... (human time display)

Following on from the previous post, I’ve also shamelessly poached another idea from Dunstan: human-readable timestamps. Just pop this into your post.html file, where you want the ‘time’ to appear.

    {capture name=hour}{the_time d="G"}{/capture}
    {assign var=hour value=$smarty.capture.hour}
    Posted
    {if $hour eq "00" or $hour eq "01" or $hour eq "02"}
        in the wee hours,
    {elseif $hour eq "03" or $hour eq "04" or $hour eq "05" or $hour eq "06"}
        terribly early in the morning,
    {elseif $hour eq "07" or $hour eq "08" or $hour eq "09"}
        early in the morning,
    {elseif $hour eq "10"}
        mid-morning,
    {elseif $hour eq "11"}
        late morning,
    {elseif $hour eq "12" or $hour eq "13"}
        mid-morning,
    {elseif $hour eq "14"}
        early afternoon,
    {elseif $hour eq "15" or $hour eq "16"}
        mid-afternoon,
    {elseif $hour eq "17"}
        late afternoon,
    {elseif $hour eq "18" or $hour eq "19"}
        early evening,
    {elseif $hour eq "20" or $hour eq "21"}
        evening,
    {elseif $hour eq "22"}
        late evening,
    {elseif $hour eq "23"}
        late at night,
    {/if}

Time Since (...after the fact).

I had noticed lots of blogs had a nice little Posted x hours, y minutes after the fact tag attached to Comments. I thought this was pretty cool, and had a very short attempt at this some time ago. Then, over on Binary Bonsai, Michael mentioned how the plugin was broken, and how it needs fixing. Well, I didn’t fix the PHP version, but I did write a pure Smarty version!

    {* 
    
    "Time Since" Smarty Code 
    
    Author: Matt Schinckel <matt@schinckel.net>
        http://schinckel.net
    
    Based on a WordPress Plugin "Time Since" 
        http://binarybonsai.com/wordpress/timesince
    
    Notes: Insert this whole text where you want the
    text to appear.  If keen, you could move the first
    pair of lines to a place outside of the comment loop,
    but still inside the post.  This might save some
    precious server seconds.
    
    The accuracy of the 'time since' decreases over time:
    there aren't 30.4 days in every month, for instance.
    
    *}
    
    {capture name=post_time}{the_time d="U"}{/capture}
    {assign var=post_time value=$smarty.capture.post_time}
    
    {capture name=comment_time}{comment_date d='U'}{/capture}
    {assign var=comment_time value=$smarty.capture.comment_time}
    
    
    {assign var=since value=$comment_time-$post_time}
    
    
    {if $since < 3600}
        {assign var=name1 value="minute"}{assign var=sec1 value=60}
        {assign var=name2 value="second"}{assign var=sec2 value=1}
    {elseif $since < 86400}
        {assign var=name1 value="hour"}{assign var=sec1 value=3600}
        {assign var=name2 value="minute"}{assign var=sec2 value=60}
    {elseif $since < 604800}
        {assign var=name1 value="day"}{assign var=sec1 value=86400}
        {assign var=name2 value="hour"}{assign var=sec2 value=3600}
    {elseif $since < 2626560}
        {assign var=name1 value="week"}{assign var=sec1 value=604800}
        {assign var=name2 value="day"}{assign var=sec2 value=86400}
    {elseif $since < 31536000}
        {assign var=name1 value="month"}{assign var=sec1 value=2626560}
        {assign var=name2 value="week"}{assign var=sec2 value=604800}
    {else}
        {assign var=name1 value="year"}{assign var=sec1 value=31536000}
        {assign var=name2 value="month"}{assign var=sec2 value=2626560}
    {/if}
    {math equation='floor(a/b)' a=$since b=$sec1 assign=count1}
    {math equation='floor((a-floor(a/b)*b)/c) a=$since b=$sec1 c=$sec2 assign=count2}
    Posted {$count1} {$name1}{if $count1 != 1}s{/if},
           {$count2} {$name2}{if $count2 != 1}s{/if} after the fact.

Another Gravatar Implementation

I recalled at some stage there was a Smarty Modifier called escape. I think it was when I was looking for one called rot13. Anyways, you can use this and the JavaScript function unescape() to obfuscate the email address. You’ll need this code early in your template: I stick it at the start just after <html>

1     <script type="text/javascript" 
2         src="http://schinckel.net/images/md5.jpg">
3     </script>

This fragment will insert the Gravatar Image:

    {capture name=reader}{comment_author_email}{/capture}
    <script type="text/javascript">
    document.write('<div class="right">');
    document.write('<img src="http://www.gravatar.com/avatar.php?gravatar_id='); 
    document.write(hex_md5(unescape("{$smarty.capture.reader|escape:"hex"}"))); 
    document.write('&size=40" alt="" />');
    document.write('</div>');
    </script>

This seems to more reliably display the Gravatars, so I’d suggest you use it instead. Either that, or Gravatar just fixed up their servers…

Show/Hide JavaScript Code

There’s a little one-liner I’ve just learned for showing or hiding the contents of a particular <div> tag. (May also work for other types of tag). The stuff you want to show or hide needs to have an id attribute. I use it to hide some stuff if there are no Comments or Trackbacks, since I’ve seperated them, and this was the best way to hide the title.

1     <div id="the_id">
2         Text/Data To Hide Goes Gere
3     </div>

Then, you can use this code to hide it (in an {if} block, for instance):

1     <script type="text/javascript">
2         document.getElementById("the_id").style.display = "none";
3     </script>

And this to show it:

1     <script type="text/Javascript">
2         document.getElementById("the_id").style.display = "block";
3     </script>

I haven’t tried, but it’s also probably possible to have show/hide links:

1     <div id="the_id">
2         Text/Data to Hide Goes Here.
3     </div>
4     <a onclick="javascript:document.getElementById('the_id').style.display = 'none'">
5         Hide Text
6     </a>
7     <a onclick="javascript:document.getElementById('the_id').style.display = 'block'">
8         Show Text
9     </a>

Of course, if you want to hide text in-place (without rearranging the flow of the remainder of the text), you’ll need:

1     <div id="the_id">
2         Text/Data to Hide Goes Here.
3     </div>
4     <a onclick="javascript:document.getElementById('the_id').style.visibility = 'hidden'">
5         Hide Text
6     </a>
7     <a onclick="javascript:document.getElementById('the_id').style.visibility = 'visible'">
8         Show Text
9     </a>

Script to XHTML

I wrote my own script to convert the selection (or whole front document) in Script Editor to XHTML. It uses the same CSS tags as Jonathon’s program, but does not add the style data in, unless a property is set. I’ve also got the Source Code for his program, so it will be interesting to see similarities.

  1     (*
  2     Script to XHTML v1.0
  3     ©2005 Matthew Schinckel
  4     http://schinckel.net/
  5     
  6     Converts selection (or whole document) into XHTML code.
  7     
  8     Insert the StyleSheet into your CSS if you want.  Else set externalCSS to false.
  9     
 10     Put it in your Scripts Menu, and it will work a treat for you.  
 11     Even copies the data to the clipboard.
 12     
 13     Bugs/Issues:
 14     
 15     • Script Editor reports the start of comments [--] as being black, not grey.
 16     • Doesn't handle references.
 17     • Operators and values are treated all as values.
 18     
 19     *)
 20     
 21     property StyleSheet : "
 22     .AppleScript { background-color:#ffffff; border: solid black 1px; padding:0.5em 1em 1em 1em; text-align:left; font-family: Verdana,Sans-Serif; overflow:auto; font-size:0.9em; white-space:pre; line-height:1.2em; margin-bottom:9px;}
 23     .as_new_text  { font-family:Courier; color: purple; }
 24     .as_operators  { color: black; }
 25     .as_language  { font-weight: bold; color: blue; }
 26     .as_application  { color: blue; }
 27     .as_comments, .as_comment  { font-style:italic; color: gray; }
 28     .as_values  { color: black; }
 29     .as_variables ,.as_variable { color: green; }
 30     .as_references  { color: purple; }
 31     "
 32     property externalCSS : true
 33     
 34     -- The HTML code either side of the block.
 35     property htmlStart : "<pre class='AppleScript'>" & return
 36     property htmlEnd : return & "</pre>" & return
 37     
 38     property tagStart : "<span class='"
 39     property tagMiddle : "'>"
 40     property tagEnd : "</span>"
 41     
 42     -- Apparently, this one is different to 'return'
 43     property enter : "
 44     "
 45     -- For testing, set this to 2, for use via Script Menu, set to 1.
 46     property win : 1
 47     
 48     if externalCSS is not true then set htmlStart to htmlStart & "<style>" & return & StyleSheet & return & "</style>" & return
 49     
 50     tell application "Script Editor"
 51         set theData to contents of selection of document of window win
 52         if (theData is "") then
 53             -- No selection, let's do the whole document.
 54             set textList to attribute run of contents of document of window win
 55             set fontList to font of attribute run of contents of document of window win
 56             set colorList to color of attribute run of contents of document of window win
 57         else
 58             set textList to attribute run of contents of selection of document of window win
 59             set fontList to font of attribute run of contents of selection of document of window win
 60             set colorList to color of attribute run of contents of selection of document of window win
 61         end if
 62     end tell
 63     
 64     set html to ""
 65     
 66     repeat with i from 1 to the count of textList
 67         set theClass to whichClass(item i of fontList, item i of colorList)
 68         set theText to my HTMLify(item i of textList)
 69         set html to html & tagStart & theClass & tagMiddle & theText & tagEnd
 70     end repeat
 71     
 72     set the clipboard to htmlStart & html & htmlEnd
 73     beep
 74     
 75     -- Utility Functions
 76     
 77     on whichClass(theFont, theColor)
 78         -- Set the class according to the font or colour.
 79         if theFont is "Verdana-Bold" then return "as_language"
 80         if theColor is {16384, 32768, 0} then return "as_variables"
 81         if theColor is {19660, 19960, 19960} then return "as_comments"
 82         if theColor is {0, 0, 65535} then return "as_application"
 83         if theColor is {32768, 0, 32768} then return "as_new_text"
 84         return "as_values"
 85     end whichClass
 86     
 87     on replace(theText, find, replace)
 88         -- Nice replace function
 89         set OldDelims to AppleScript's text item delimiters
 90         set AppleScript's text item delimiters to find
 91         set newText to text items of theText
 92         set AppleScript's text item delimiters to replace
 93         set theResult to newText as text
 94         set AppleScript's text item delimiters to OldDelims
 95         return theResult
 96     end replace
 97     
 98     on HTMLify(someText)
 99         -- This might need some more entries.
100         -- Perhaps a better way of doing it...?
101         set someText to replace(someText, "&", "&amp;")
102         set someText to replace(someText, "\"", "&quot;")
103         set someText to replace(someText, "<", "&lt;")
104         set someText to replace(someText, ">", "&gt;")
105         --set someText to replace(someText, tab, "&nbsp;&nbsp;&nbsp;&nbsp;")
106         --set someText to replace(someText, enter, "<br />" & enter)
107         --set someText to replace(someText, return, "<br />" & return)
108         return someText
109     end HTMLify

I will not tickle you!

Got this one from Boing Boing:

'Fixed' XHTML Export

Since I need to replace all “ in the generated XHTML with ‘, I use the replace function from the previous post.

 1     (* 
 2     Bugs:
 3     
 4     Does not like no selection: no real way to get the selection from SEE anyway.
 5     Sometimes does not execute if called from Script Menu.  Intermittant.
 6     *)
 7     
 8     -- If called from Script Menu, need to do this.
 9     
10     tell application "SubEthaEdit"
11      activate
12      tell application "System Events" to keystroke "C" using {command down}
13     end tell
14     
15     set theStart to (the clipboard)
16     
17     set the clipboard to (my replace(theStart, "\"", "'"))
18     
19     beep
20     
21     -- Another way of the Replace Function being called:
22     
23     -- set the clipboard to (replaceText from theStart to "'" instead of "\"")
24     
25     on replace(theText, find, replace)
26      set OldDelims to AppleScript's text item delimiters
27      set AppleScript's text item delimiters to find
28      set newText to text items of theText
29      set AppleScript's text item delimiters to replace
30      set theResult to newText as text
31      set AppleScript's text item delimiters to OldDelims
32      return theResult
33     end replace
34     
35     -- Alternate version of replace()
36     
37     to replaceText from theText to replace instead of find
38      set OldDelims to AppleScript's text item delimiters
39      set AppleScript's text item delimiters to find
40      set theText to text items of theText
41      set AppleScript's text item delimiters to replace
42      set theText to theText as text
43      set AppleScript's text item delimiters to OldDelims
44      theText
45     end replaceText

Code Markup

I like to present styled code for my readers, but I’m not totally happy with my methods of getting it: SubEthaEdit’s Export as XHTML is very cool, but I still have to tweak it so it looks nice. And I’ve got a great program for getting the XHTML version of the code Script Editor (AppleScript) is currently displaying. But, I’d like more flexibility. I want the code to be tagged with classes, like it’s possible to do with the Script Editor add-on, rather than using inline styles. I’ve already done this for the Script Editor code, but I just need to remember to remove the inline stylesheet from the start of the generated code. Unfortunately the Script has been saved as a Run Only. I’d love to know a way to decompile one of these Scripts…apparently there isn’t. I’ll have to write my own. Or see if Jon is nice enough to give the Source Code. As for the SubEthaEdit part: I should be able to do it. I’ve learned a couple of tricks that will help.

Insert iTunes Data into Post

I used to use ecto to paste all of my entries on my blog, but with Blogsome’s XMLRPC issue, I have to use a browser. But it would be nice to automatically get the name and information about my currently playing iTunes track, just like ecto used to do. I wrote an AppleScript that does this, and, inserts the data in at the insertion point for you. Because it’s intended to be run from the Script Menu, and everything run from there runs as “System Events”, I had to hard code in the browser name. If you use another browser, just replace the second line with whatever your browser is called.

 1 property star : «data utxt2605» as Unicode text
 2 property browser : "Camino"
 3 on run {}
 4     
 5     tell application "System Events"
 6         try
 7             get process "iTunes"
 8         on error
 9             return "No Track"
10         end try
11     end tell
12     
13     tell application "iTunes"
14         set theTrack to current track
15         set theArtist to artist of theTrack
16         set theAlbum to album of theTrack
17         set theRating to rating of theTrack
18         set theTrack to name of theTrack
19     end tell
20     
21     set theString to "<p class=’itunes’> " & theTrack & " • <a href='http://www.google.com/search?q=%22" & theArtist & "%22>" & theArtist & "</a> • <a href=’http://www.google.com/search?q=%22" & theAlbum & "%22>" & theAlbum & "</a> " & myRating(theRating)
22     set the clipboard to theString
23     
24     tell application browser
25         activate
26         tell application "System Events" to keystroke "v" using {command down}
27     end tell
28 end run
29 
30 on myRating(theRating)
31     set theResult to ""
32     set theTimes to (theRating - 9) / 20 as integer
33     repeat theTimes times
34         set theResult to theResult & star
35     end repeat
36     return theResult
37 end myRating