Format a link and an Applescipt for posting

Version 1
Last Modified: Feb 22, 2011 7:58 PM
Disclaimer: Apple does not necessarily endorse any suggestions, solutions, or third-party software products that may be mentioned in the topic below. Apple encourages you to first seek a solution at Apple Support. The following links are provided as is, with no guarantee of the effectiveness or reliability of the information. Apple does not guarantee that these links will be maintained or functional at any given time. Use the information below at your own discretion.



*This AppleScript code has two functions:*

1. Converts a post link to the format needed by these forums for posting that link.

2. Reformats a post containing format codes so the symbols are preserved rather than being used to format the post. For example, a word surrounded by asterisks does not get bolded and the asterisks stay in the post. Leading white space is also preserved (two or more spaces are preserved and tabs become 3 spaces). This is especially handy for posting outlines, code, Terminal output or Applescripts.


For example,  the asterisks will be preserved:
  mac $ ls *oup*
  Youpi Key Editor.plist  Youpi Key.app alias  

And the indentation will be preserved:
   on adding folder items to this_folder after receiving dropped_items
    repeat with dropped_item_ref in dropped_items
      display dialog "dropped files is " & dropped_item_ref
    end repeat
  end adding folder items to



The first thing that you need to do is to make the text into an AppleScript program.

Start the AppleScript Editor
/Applications/AppleScript/Script Editor.app
In Snow Leopard it's at: /Applications/Utilities/AppleScript Editor


Copy the script text to the Applescript editor.


Save the text to a file as an application and do not check any of the boxes below.


!http://farm4.static.flickr.com/3544/3390737677_645a847e28.jpg?v=0!


If you want access to the script from your Script Menu, move the script (the saved script application file) to your
~/Library/Scripts folder. You can also drag it to your Dock or make an alias for it on the Desktop.

Usage:
1. To convert a post link to a link that can be posted.
  a) Find the particular post you wish to reference.
  b) Click on the reply icon.
  c) Select and copy the Web address to the clipboard (command+c).
  d) Cancel this reply (assuming you just want the link to this post).
  e) Run this AppleScript to convert the Web address.
  f) Paste (command+v) the converted Web address into your post.

2. If you have text that has spaces or symbols you would like to preserve in your post.
  a) Copy the text, outline, code, terminal commands, etc to the clipboard (command+c)
  b) Run the AppleScript to convert the clipboard contents.
  c) Paste the converted clipboard into your post (command+v)

Robert

-----------------
(*
 Name: alter-clipboard
 
 Input: Clipboard
 
 Output: Clipboard
 
 This AppleScript program is designed to convert a forum-reply web address  to a link to a forum post, to convert special characters to their HTML symbols, and to convert programming language code to a  form that does not get mangled.
  
1)   When you wish to reference another Apple discussion forum post, I found the process convoluted.  The Apple form software does not provide for the display of a form post link.  Instead, you must  construct the link from a reply post.  This AppleScript simplifies the process. 
 
   When the clipboard contains a link to a forum reply page, do the conversion. 
   
   Here is that way:
   a) Find the particular post you wish to reference.  
   b) Click on the reply icon.  
   c) Copy  the Web address to the clipboard  (command+c).  
   d) Run this AppleScript to convert the Web address.  
   e) Paste (command+v) the converted Web address into your post. 
   
    For example, you:  
    • Want to reference a post on how to improve the performance of Tiger.  You find a post by Texas Mac Man with a list of possibilities.  
    • Click on the Reply option and highlight the Web address because you know the address contains a reference to this post.
    • Type command+c to copy the address to the clipboard.  The clipboard now contains:
       http://discussions.apple.com/post!reply.jspa?messageID=9124252
    •  Run this AppleScript to convert the Web address. The clipboard now contains:   
            http://discussions.apple.com/message.jspa?messageID=9124252#9124252
    • Type command+v to paste the address   into your new post.
     
2)  Assume that the clipboard contains  special symbols or formated programming language code to convert.  
      
   The simpleFormatting property switch in the code selects teletype text ( tt ) when true and selets preformated text ( pre ) when false.      
      
   This AppleScript will converted special symbols used in these forums to a form not used by the forum software in order to preserve the original characters.
   
   For the symbols, see:
      See "Other formating tags are available as shown below" in 
      http://discussions.apple.com/help.jspa#format 
   
      An alternative to using these symbols is document in this post: 
      http://discussions.apple.com/thread.jspa?messageID=607563
   
      Plain Text Markup Help
      http://www.jivesoftware.com/jivespace/markuphelpfull.jspa
   
   Also, this AppleScript preserves indentation used in programs or Terminal output. 
   
   a) Copy  the text to the clipboard .  
   b) Run this AppleScript to convert the text
   c) Paste the converted text into your post. 
 
    For example,  the asterisks will be preserved:
      mac $ ls *oup*
      Youpi Key Editor.plist  Youpi Key.app alias  
 
   For example,  the indentation will be preserved:
      on adding folder items to this_folder after receiving dropped_items
         repeat with dropped_item_ref in dropped_items
            display dialog "dropped files is " & dropped_item_ref
         end repeat
      end adding folder items to
 

   Author: rccharles
   
   with input from:
      red_menace
      Nubz N.
   
   Copyright 2009 rccharles
   GNU General Public License 
   
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation,  version 3
   
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.
 
   *)
 
property simpleFormatting : true -- when true use tt tag to enclose; when false use pre tab to enclose
 

on run
   -- Write a message into the event log.
   log "  --- Starting on " & ((current date) as string) & " --- "
   
   set theClip to the clipboard
   log length of theClip
   
   (* added Nov 7, 2009 *)
   if length of theClip = 0 then
      display dialog "Please copy something to the clip board." giving up after 20
      return
      
   else if length of theClip > 33 and characters 1 thru 33 of theClip as string = "http://discussions.apple.com/post" then
      
      (* 
       for example:
          http://discussions.apple.com/post!reply.jspa?messageID=9003196
        converts to:
           http://discussions.apple.com/message.jspa?messageID=9003196#9003196
            *)
      
      display dialog "converting link syntax..." giving up after 2
      
      set theClip to alterString(theClip, "post!reply", "message")
      set {frontPart, postNumber} to textToList(theClip, "=")
      set theClip to theClip & "#" & postNumber
   else
      display dialog "converting text..." giving up after 2
      
      --  see http://www.asciitable.com/ for the ASCII translations
      
      -- must be the first change so as not to cause problems. 
      set theClip to alterString(theClip, "#", "&#035;")
      
      --  You need to use either the numeric form of the HTML symbol or 
      --  translate HTML character enties as seen below.
      set theClip to alterString(theClip, "&amp;", "&amp;amp;")
      set theClip to alterString(theClip, " ", "&amp;nbsp;")
      
      -- Preserve single blanks, makes it a little easier to read translated code
      
      -- converts two spaces to a space and non-breaking space
      set theClip to alterString(theClip, "  ", "  ")
      
      -- A leading blank will get deleted, so avoid a leading blank -- a blank after a return.
      
      (* ... supports three file formats: 
            unix (lines end with LF),
              dos (lines end with CR LF), and 
              mac (lines end with CR).
         http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix *)
      
      -- Handle Dos return & lf.  Perhaps the clipboard was copied from a web page. 
      -- Reduce to just return since both return & lf act as returns.
      -- I've seen this & didn't know what was going on at the time.
      set lf to ASCII character 10
      set theClip to alterString(theClip, return & lf, return)
      -- Handle mac return. And catch a leading blank if any.
      set forceLeadingReturn to return & theClip
      set forceLeadingReturn to alterString(forceLeadingReturn, return & " ", return & " ")
      set theClip to text 2 thru -1 of forceLeadingReturn -- delete prepended return
      -- Handle Unix return. 
      set theClip to alterString(theClip, lf & " ", lf & " ")
      -- Change trailing blank return
      set theClip to alterString(theClip, " " & return, " " & return) -- mac
      set theClip to alterString(theClip, " " & lf, " " & lf) -- unix
      
      -- Avoids multiple returns being converted to a single return / lf. 
      set theClip to alterString(theClip, return & return, return & " " & return) -- mac
      set theClip to alterString(theClip, lf & lf, lf & " " & lf) -- unix
      
      
      -- I don't think these are order dependent.
      set theClip to alterString(theClip, "!", "&#033;")
      set theClip to alterString(theClip, "[", "&#091;")
      set theClip to alterString(theClip, "]", "&#093;")
      set theClip to alterString(theClip, "<", "&#060;")
      set theClip to alterString(theClip, "*", "&#042;")
      set theClip to alterString(theClip, "+", "&#043;")
      set theClip to alterString(theClip, "_", "&#095;")
      
      (* altered Nov 7, 2009 *)
      set theClip to alterString(theClip, "-", "&#045;")
      
      set theClip to alterString(theClip, tab, "   ") -- Pick three for less spacing.
      set theClip to alterString(theClip, "|", "&#124;") -- Gets toasted if in column one
      set theClip to alterString(theClip, ">", "&#062;") -- just in case
      
      (* added Nov 7, 2009 *)
      set theClip to alterString(theClip, "\\", "&#092;") -- backslash is applescript escape
      
      (* added May 24, 2010 *)
      set theClip to alterString(theClip, ".", "&#046;")
      set theClip to alterString(theClip, ":", "&#058;") -- colon
      set theClip to alterString(theClip, "{", "&#123;")
      set theClip to alterString(theClip, "}", "&#125;")
      
      
      
      
      
      -- suggested by red_menace from the Mac forums; altered May 24, 2010
      set theClip to alterString(theClip, "^", "&#94;")
      set theClip to alterString(theClip, "~", "&#126;")
      
      if simpleFormatting then
         set theClip to "<tt>" & theClip & "</tt>"
      else
         set theClip to addWrapper(theClip)
      end if
      
      
   end if
   
   set the clipboard to theClip
   
end run
 
on alterString(thisText, delim, replacement)
   set resultList to {}
   set {tid, my text item delimiters} to {my text item delimiters, delim}
   try
      set resultList to every text item of thisText
      set text item delimiters to replacement
      set resultString to resultList as string
      set my text item delimiters to tid
   on error
      set my text item delimiters to tid
   end try
   return resultString
end alterString
 
-- textToList was found here:
-- http://macscripter.net/viewtopic.php?id=15423
 
on textToList(thisText, delim)
   set resultList to {}
   set {tid, my text item delimiters} to {my text item delimiters, delim}
   try
      set resultList to every text item of thisText
      set my text item delimiters to tid
   on error
      set my text item delimiters to tid
   end try
   return resultList
end textToList
 
-- ----------------------------------------------------------------------
-- suggested by red_menace from the Mac forums
 
-- some constants and switches
property TextColor : "#000000" -- black  (see http://www.w3schools.com/tags/ref_colornames.asp)
property BackgroundColor : "#FFEE80" -- medium/canary yellow -- "#FFDDFF" -- a light plum/purple
property BorderColor : "#000000" -- black
property TheWidth : "width: 720px; " -- a width attribute
property UseWidth : true -- use the width attribute?
property LineCount : 25 -- the number of lines before including the height attribute
property TheHeight : "height: " & ((LineCount * 13.6) as integer) & "px; " -- a (rough) maximum height for the formatted box
 

on addWrapper(SomeText)
   (*
   fix up some formatting and add a tag wrapper  (see http://www.w3schools.com/tags/default.asp)
      parameters -      SomeText  text to add the wrapper to
      global items -    LineCount, TheHeight, UseWidth, TheWidth,  KeepFormatting,                          
                  BorderColor, TextColor, BackgroundColor
      returns [text]:      the wrapped text
   *)
   set {Height, Width} to {"", ""}
   if (count paragraphs of SomeText) is greater than LineCount then set Height to TheHeight
   
   if UseWidth then set Width to TheWidth
   -- hack...
   -- seems like the apple forum software doubles the returns in a pre tag
   set SomeText to alterString(SomeText, return, "<br>")
   
   -- change the style properties as needed
   set SomeText to "<pre style=\"
      font-family: Monaco, 'Courier New', Courier, monospace;
      font-size: 10px;
      font-weight: normal;
      margin: 0px;
      padding: 5px;
      border: 1px solid " & BorderColor & ";
      " & Width & Height & "
      color: " & TextColor & ";
      background-color: " & BackgroundColor & ";
      overflow: auto;\"
      title=\"\">" & SomeText & "</pre>
"
   return SomeText
end addWrapper

-----

This is the 3rd version of this tip. It was submitted on July 14, 2010 by rccharles.

Do you want to provide feedback on this User Contributed Tip or contribute your own? If you have achieved Level 2 status, visit the User Tips Library Contributions forum for more information.