Advertisement
rccharles

ASC pretty up post May 1, 2019

Apr 30th, 2019
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2.   This applescript converts clipboard input into a format suited for pasting into an ASC
  3.   reply.  I observed that my copies into an ASC reply were not formated that well.  
  4.   I observed that copies from a web browser were formated much better.  I went about
  5.    adjusting the clipboard copy to the format expected by a web browser for best results.
  6.  
  7.  This applescript accepts the clipboard in either
  8.  -- plan text upon which the text is converted to HTML.  Conversion is limitted to inserting paragraph tags for blank lines and inserting links where http or https text appears. The page title is substituted for the link.  
  9.  -- HTML which text identified as containing HTML markup.  The detection of HTML is done by finding four less thans < and four greater thans >.
  10.      Caveat emptor.  
  11.  
  12.  to use:
  13.  1) copy command + c what data you want to convert
  14.  2) run this applascript by double clicking on the app.
  15.  3) paste command + V into an ASC reply
  16.  
  17.  I have tested in Waterfox 56.2.9 in Yosemite.  I assume the process will work with other web browsers and other versions of macOS.
  18.  
  19.  Save as an Application Bundle.  Don't check anything.
  20.  
  21.  Hints on Debugging...
  22.    Shows how to debug via on run path. Shows items added to folder. Shows log statement.
  23.    It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.  
  24.    For testing, run in the Script Editor.
  25.          1) Click on the Event Log tab to see the output from the log statement
  26.       2) Click on Run
  27.    
  28. change log
  29. may 1, 2019 -- skip 403 forbidding titles
  30.  
  31. enhancements:
  32.   -- get pdf title
  33.  
  34.  
  35. Author: rccharles
  36.  
  37.  Copyright 2019 rccharles  
  38.      
  39.        Permission is hereby granted, free of charge, to any person obtaining a copy  
  40.        of this software and associated documentation files (the "Software"), to deal  
  41.        in the Software without restriction, including without limitation the rights  
  42.        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  43.        copies of the Software, and to permit persons to whom the Software is  
  44.        furnished to do so, subject to the following conditions:  
  45.        
  46.        The above copyright notice and this permission notice shall be included in all  
  47.        copies or substantial portions of the Software.  
  48.        
  49.        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  50.        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  51.        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  52.        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  53.        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  54.        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
  55.        SOFTWARE.  
  56.  
  57.  
  58.     example text document:
  59. set the clipboard to "\"Effective defenses 111 threats\" by John Galt
  60. https://discussions.apple.com/docs/DOC-8841
  61. \"Avoid phishing emails 222 and other scams\"
  62.  
  63. https://support.apple.com/en-ca/HT204759
  64.  
  65.  
  66.  
  67. blank lines
  68. also,see:http://www.google.com/ seeing again:http://www.google.com"
  69.  
  70.  *)
  71.  
  72.  
  73. -- Gets invoked here when you run in AppleScript editor or double click on the app icon.
  74. on run
  75.     global debug
  76.     set debug to 2
  77.    
  78.     set theList to clipboard info
  79.     printClipboardInfo(theList)
  80.    
  81.     try
  82.         set clipboardData to (the clipboard as text)
  83.         if debug ≥ 3 then
  84.             log "class clipboardData is " & class of clipboardData
  85.             log "calling printHeader."
  86.         end if
  87.         printHeader("clipboardData", clipboardData)
  88.     on error errStr number errorNumber
  89.         log "===> We didn't find data.   errStr is " & errStr & " errorNumber is " & errorNumber
  90.         return
  91.     end try
  92.     set returnedData to common(clipboardData)
  93.     postToCLipboard(returnedData)
  94.     -- return code
  95.     return 0
  96.    
  97.    
  98. end run
  99.  
  100. -- Folder actions.
  101. -- Gets invoked here when something is dropped on the folder that this script is monitoring.
  102. -- Right click on the folder to be monitored. services > Folder Action Settup...
  103. on adding folder items to this_folder after receiving added_items
  104.     -- TBD
  105.    
  106. end adding folder items to
  107.  
  108.  
  109.  
  110. -- Gets invoked here when something is dropped on this AppleScript icon
  111. on open dropped_items
  112.    
  113.     global debug
  114.     set debug to 2
  115.     (*
  116.     -- Debug code.
  117.       set fileName to choose file with prompt "get file"
  118.       set dropped_items to {fileName}
  119.     *)
  120.     log "class of dropped_items is " & class of dropped_items
  121.     display dialog "You dropped " & (count of dropped_items) & " item or items." & return & "  Caveat emptor. You have been warned." giving up after 6
  122.    
  123.     set totalFileData to ""
  124.     repeat with droppedItem in dropped_items
  125.         log "The droppedItem is "
  126.         -- display dialog "processing file " & (droppedItem as string) giving up after 3
  127.         log droppedItem
  128.         log "class = " & class of droppedItem
  129.         set extIs to findExtension(droppedItem)
  130.         set extIsU to makeCaseUpper(extIs)
  131.         if extIsU is "HTML" or extIsU is "HTM" or extIsU is "TEXT" or extIsU is "TXT" then
  132.             try
  133.                 set theFile to droppedItem as string
  134.                 set theFile to open for access file theFile
  135.                 set allOfFile to read theFile
  136.                 close access theFile
  137.                 printHeader("read from file ( allOfFile )", allOfFile)
  138.                 set totalFileData to totalFileData & common(allOfFile)
  139.             on error theErrorMessage number theErrorNumber
  140.                 log theErrorMessage & "error number " & theErrorNumber
  141.                 close access theFile
  142.             end try
  143.            
  144.         else
  145.             -- we do not support this extension
  146.             display dialog "We only support files with extenstion of html, htm, text or txt in either case. Your file had a " & extIs & " extention. Skipping" giving up after 10
  147.            
  148.         end if
  149.     end repeat
  150.    
  151.     postToCLipboard(totalFileData)
  152.     -- return code
  153.     return 0
  154.    
  155. end open
  156.  
  157.  
  158. -- ------------------------------------------------------
  159. on common(clipboardData)
  160.     global debug
  161.     set lf to character id 10
  162.     -- Write a message into the event log.
  163.     log "  --- Starting on " & ((current date) as string) & " --- "
  164.    
  165.     -- don't let Windoze confuse us. convert Return LineFeed to lf
  166.     set clipboardData to alterString(clipboardData, return & lf, lf)
  167.     -- might as will convert classic macOS return to lf. We will have to look for so many things.
  168.     set clipboardData to alterString(clipboardData, return, lf)
  169.    
  170.     -- figure out what type of data we have: plan text or html text.
  171.     set paraCount to count of textToList(clipboardData, "<p")
  172.     set endparaCount to count of textToList(clipboardData, "</p>")
  173.     set titleCount to count of textToList(clipboardData, "<title")
  174.     set endTitleCount to count of textToList(clipboardData, "</title>")
  175.     set aLinkCount to count of textToList(clipboardData, "href=\"http")
  176.     if debug ≥ 1 then
  177.         log "paraCount  is " & paraCount
  178.         log "endparaCount is " & endparaCount
  179.         log "titleCount is " & titleCount
  180.         log "endTitleCount is " & endTitleCount
  181.         log "aLinkCount is " & aLinkCount
  182.     end if
  183.     --set endHttpCount to count of textToList(clipboardData, "http://")
  184.     --set endHttpsCount to count of textToList(clipboardData, "https://")
  185.     -- note, textToList returns a count of one when string wasn't found becuase it really returns slit strings.
  186.     if paraCount ≥ 4 and endparaCount ≥ 3 or ((titleCount is endTitleCount) and titleCount ≥ 2) or aLinkCount ≥ 2 then
  187.         log "found HTML input"
  188.         set readyData to typeHTML(clipboardData)
  189.     else
  190.         log "found Text input"
  191.         set readyData to typeText(clipboardData)
  192.        
  193.     end if
  194.     return readyData
  195. end common
  196.  
  197. -- ------------------------------------------------------
  198. (*
  199. alterString
  200.   thisText is the input string to change
  201.   delim is what string to change.  It doesn't have to be a single character.
  202.   replacement is the new string
  203.  
  204.   returns the changed string.
  205. *)
  206.  
  207. on alterString(thisText, delim, replacement)
  208.     set resultList to {}
  209.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  210.     try
  211.         set resultList to every text item of thisText
  212.         set text item delimiters to replacement
  213.         set resultString to resultList as string
  214.         set my text item delimiters to tid
  215.     on error
  216.         set my text item delimiters to tid
  217.     end try
  218.     return resultString
  219. end alterString
  220.  
  221. -- ------------------------------------------------------
  222. (*
  223.   Return the text to the right of theToken.
  224. *)
  225. on answerAndChomp(theString, theToken)
  226.     set debugging to false
  227.     set theOffset to offset of theToken in theString
  228.     if debugging then log "theOffset is " & theOffset
  229.     set theLength to length of theString
  230.     if theOffset > 0 then
  231.         set beginningPart to text 1 thru (theOffset - 1) of theString
  232.         if debugging then log "beginningPart is " & beginningPart
  233.        
  234.         set chompped to text theOffset thru theLength of theString
  235.         if debugging then log "chompped is " & chompped
  236.         return {chompped, beginningPart}
  237.     else
  238.         set beginningPart to ""
  239.         return {theString, beginningPart}
  240.     end if
  241.    
  242. end answerAndChomp
  243.  
  244. -- ------------------------------------------------------
  245. (*
  246.   Delete the leading part of the string until and including theToken.
  247. *)
  248. on chompLeftAndTag(theString, theToken)
  249.     set debugging to false
  250.     --log text 1 thru ((offset of "my" in s) - 1) of s
  251.     --set rightString to offset of theToken in theString thru count of theString of theString
  252.     set theOffset to offset of theToken in theString
  253.     if debugging then log "theOffset is " & theOffset
  254.     set theLength to length of theString
  255.     if debugging then log "theLength is " & theLength
  256.     if theOffset > 0 then
  257.         set chompped to text (theOffset + (length of theToken)) thru theLength of theString
  258.         if debugging then log "chompped is " & chompped
  259.         return chompped
  260.     else
  261.         return ""
  262.     end if
  263. end chompLeftAndTag
  264.  
  265. -- ------------------------------------------------------  
  266. (*
  267. Yvan Koenig
  268. https://macscripter.net/viewtopic.php?id=43133
  269. *)
  270. on findExtension(inputFileName)
  271.     set fileName to inputFileName as string
  272.     set saveTID to AppleScript's text item delimiters
  273.     set AppleScript's text item delimiters to {"."}
  274.     set theExt to last text item of fileName
  275.     set AppleScript's text item delimiters to saveTID
  276.     --log "theExt is " & theExt
  277.     if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
  278.     --log "theExt is " & theExt
  279.     return theExt
  280. end findExtension
  281.  
  282. -- ------------------------------------------------------
  283. (*
  284.   http://krypted.com/mac-os-x/to-hex-and-back/
  285. *)
  286. on hexToString(hex)
  287.     log "in hexToString"
  288.     log "hex string is " & hex
  289.     set toUnix to "echo " & hex & " | xxd -r -p "
  290.     log "toUnix is " & toUnix
  291.     try
  292.         set fromUnix to do shell script toUnix
  293.         log "fromUnix is " & fromUnix
  294.     on error errMsg number n
  295.         log "convert hex string to string failed. " & errMsg & " with number " & n
  296.     end try
  297. end hexToString
  298.  
  299.  
  300. -- ------------------------------------------------------
  301. (*
  302.  
  303. https://stackoverflow.com/questions/55838252/minimum-value-that-not-zero
  304.        set m to get minimumPositiveNumber from {10, 2, 0, 2, 4}
  305.     log "m is " & m
  306.     set m to minimumPositiveNumber from {0, 0, 0}
  307.     log "m is " & m
  308.  
  309.  
  310. *)
  311. on minimumPositiveNumber from L
  312.     local L
  313.    
  314.     if L = {} then return null
  315.    
  316.     set |ξ| to 0
  317.    
  318.     repeat with x in L
  319.         set x to x's contents
  320.         if (x < |ξ| and x ≠ 0) ¬
  321.             or |ξ| = 0 then ¬
  322.             set |ξ| to x
  323.     end repeat
  324.    
  325.     |ξ|
  326. end minimumPositiveNumber
  327.  
  328. -- ------------------------------------------------------
  329. (*
  330.   makeCaseUpper("Now is the time, perhaps, for all good men")
  331. *)
  332. on makeCaseUpper(theString)
  333.     set UC to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  334.     set LC to "abcdefghijklmnopqrstuvwxyz"
  335.     set C to characters of theString
  336.     repeat with ch in C
  337.         if ch is in LC then set contents of ch to item (offset of ch in LC) of UC
  338.     end repeat
  339.     return C as string
  340. end makeCaseUpper
  341.  
  342. -- ------------------------------------------------------
  343. on postToCLipboard(pleasePost)
  344.     try
  345.         -- osascript -e "set the clipboard to «data HTML${hex}»"     
  346.         set toUnixSet to "osascript -e \"set the clipboard to «data HTML" & pleasePost & \""
  347.         log "toUnixSet is " & printHeader("toUnixSet", toUnixSet)
  348.        
  349.         set fromUnixSet to do shell script toUnixSet
  350.         log "fromUnixSet is " & fromUnixSet
  351.        
  352.     on error errMsg number n
  353.         log "==> We tried to send back HTML data, but failed. " & errMsg & " with number " & n
  354.     end try
  355.     -- see what ended up on the clipboard
  356.     set theList2 to clipboard info
  357.     printClipboardInfo(theList2)
  358. end postToCLipboard
  359.  
  360. -- ------------------------------------------------------
  361. on printClipboardInfo(theList)
  362.     log (clipboard info)
  363.     log class of theList
  364.     log "Data types on the clipboard ... "
  365.     printList("theList", theList)
  366.     log "... "
  367. end printClipboardInfo
  368.  
  369. -- ------------------------------------------------------
  370. (* Pump out the beginning of theString *)
  371. on printHeader(theName, theString)
  372.     global debug
  373.     if debug ≥ 3 then
  374.         log "in printHeader"
  375.         log theString
  376.         log length of theString
  377.     end if
  378.     if length of theString ≤ 0 then
  379.         log "==> no string to print"
  380.     else
  381.         log theName & " is " & text 1 thru (minimumPositiveNumber from {200, length of theString}) of theString & "<+++++++++++"
  382.     end if
  383. end printHeader
  384.  
  385. -- ------------------------------------------------------
  386. (*
  387. print out the items in a list
  388.  
  389. *)
  390.  
  391. on printList(theName, splits)
  392.     set theCount to 1
  393.     repeat with theEntry in splits
  394.         log "------- " & theName & theCount & " is " & return & theEntry
  395.         set theCount to theCount + 1
  396.     end repeat
  397. end printList
  398.  
  399. -- ------------------------------------------------------
  400. (*
  401. splitTextToList seems to be what you are trying to do
  402.   thisText is the input string
  403.   delim is what to split on
  404.  
  405.   results returned in a list
  406.  
  407.   Total hack. We know splitTextToList strips of delim so add it back.
  408. *)
  409.  
  410. on splitTextToList(thisText, delim)
  411.    
  412.     set returnedList to textToList(thisText, delim)
  413.     set resultArray to {}
  414.     copy item 1 of returnedList to the end of the resultArray
  415.    
  416.     repeat with i from 2 to (count of returnedList) in returnedList
  417.         set newElement to delim & item i of returnedList
  418.         copy newElement to the end of the resultArray
  419.     end repeat
  420.    
  421.     return resultArray
  422. end splitTextToList
  423.  
  424. -- ------------------------------------------------------
  425. (*
  426.   Retrieved data between "begin" and "end" tag. Whatever is between the strings.
  427. *)
  428. on tagContent(theString, startTag, endTag)
  429.     try
  430.         log "in tabContent. " & " startTag is " & startTag & " endTag is " & endTag
  431.         set beginningOfTag to chompLeftAndTag(theString, startTag)
  432.         if length of beginningOfTag ≤ 0 then
  433.             set middleText to ""
  434.         else
  435.             printHeader("beginningOfTag", beginningOfTag)
  436.             set endingOffset to (offset of endTag in beginningOfTag)
  437.             if endingOffset ≤ (length of endTag) then
  438.                 set middleText to ""
  439.             else
  440.                 set middleText to text 1 thru (endingOffset - 1) of beginningOfTag
  441.                 log "middleText is " & printHeader("middleText", middleText)
  442.             end if
  443.         end if
  444.     on error errMsg number n
  445.         log "finding contained text failed. " & errMsg & " with number " & n
  446.         set middleText to ""
  447.     end try
  448.     return middleText
  449. end tagContent
  450.  
  451. (*
  452. textToList seems to be what you are trying to do
  453.   thisText is the input string
  454.   delim is what to split on
  455.  
  456.   returns a list of strings.  
  457.  
  458. - textToList was found here:
  459. - http://macscripter.net/viewtopic.php?id=15423
  460.  
  461. *)
  462.  
  463. on textToList(thisText, delim)
  464.     set resultList to {}
  465.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  466.    
  467.     try
  468.         set resultList to every text item of thisText
  469.         set my text item delimiters to tid
  470.     on error
  471.         set my text item delimiters to tid
  472.     end try
  473.     return resultList
  474. end textToList
  475.  
  476. -- ------------------------------------------------------
  477. on typeHTML(theData)
  478.     global debug
  479.     log "in typeHTML" & return & "  Try to send back HTML."
  480.     try
  481.         set clipboardDataQuoted to quoted form of theData
  482.         log "quoted form is " & printHeader("clipboardDataQuoted", clipboardDataQuoted)
  483.         -- make hex string as required for HTML data on the clipboard
  484.         set toUnix to "/bin/echo -n " & clipboardDataQuoted & " | hexdump -ve '1/1 \"%.2x\"'"
  485.         log "toUnix is " & printHeader("toUnix", toUnix)
  486.        
  487.         set fromUnix to do shell script toUnix
  488.        
  489.         log "fromUnix is " & printHeader("fromUnix", fromUnix)
  490.         if debug ≥ 2 then
  491.             log "displaying original string --- so we can tell if it converted successfully. "
  492.             hexToString(fromUnix)
  493.         end if
  494.     on error errMsg number n
  495.         log "==> convert to hex string failed. " & errMsg & " with number " & n
  496.         set fromUnix to ""
  497.     end try
  498.     return fromUnix
  499. end typeHTML
  500.  
  501. -- ------------------------------------------------------
  502. on typeText(theData)
  503.     (*
  504.          Unix-like systems      LF      0A      \n
  505.             (Linux, macOS)
  506.                Microsoft Windows    CRLF    0D 0A   \r\n
  507.                classic Mac OS       CR      0D          \r   Applescript return
  508.          *)
  509.     global debug
  510.     set lf to character id 10
  511.     log "in typeText"
  512.     printHeader("the input  ( theData )", theData)
  513.     -- Example: -- https://discussions.apple.com/docs/DOC-8841
  514.     -- locate links
  515.    
  516.     set theOutputBuffer to theData
  517.     set countOf to 1
  518.     -- file is mostly for testing, but should be ok for production too.
  519.     set linkId to {"https://", "http://"}
  520.     repeat with lookForLink in linkId
  521.        
  522.         set splitOnHTTPorHTTPS to splitTextToList(theOutputBuffer, lookForLink)
  523.         log "display splitOnHTTPorHTTPS.."
  524.        
  525.         -- debug info
  526.         if debug ≥ 2 then
  527.             repeat with theCurrentHTTPorHTTPS in splitOnHTTPorHTTPS
  528.                 printHeader("#" & countOf & " theCurrentHTTPorHTTPS ", theCurrentHTTPorHTTPS)
  529.                 set countOf to countOf + 1
  530.             end repeat
  531.         end if
  532.        
  533.         set buildHTML to beginning of splitOnHTTPorHTTPS
  534.         log "buildHTML is " & buildHTML
  535.         -- delete the first item text
  536.         set splitOnHTTPorHTTPS to rest of splitOnHTTPorHTTPS
  537.         log splitOnHTTPorHTTPS
  538.         set counti to 1
  539.         repeat with theCurrentHTTPorHTTPS in splitOnHTTPorHTTPS
  540.             -- example: converted url. no title found
  541.             -- <a href="https://discussions.apple.com/docs/DOC-8841" target="_blank">https://discussions.apple.com/docs/DOC-8841</a>       
  542.            
  543.             if debug ≥ 1 then
  544.                 set toUnix to "/bin/echo -n " & quoted form of theCurrentHTTPorHTTPS & " | hexdump -C"
  545.                 set fromUnix to do shell script toUnix
  546.                 log "fromUnix is " & return & fromUnix
  547.             end if
  548.            
  549.             -- find the end of the HTML URL by splitting on blank or return
  550.             -- unsafe characters includes the blank/empty space and " < > # % { } | \ ^ ~ [ ] `
  551.             -- https://perishablepress.com/stop-using-unsafe-characters-in-urls/
  552.             -- the end of the clipboard string my end after the url, hence no " ", LF or CR
  553.             -- Rember, CRLF was converted to LF above
  554.             set endsWhere to {}
  555.             copy (offset of " " in theCurrentHTTPorHTTPS) to the end of the endsWhere
  556.             copy (offset of lf in theCurrentHTTPorHTTPS) to the end of the endsWhere
  557.            
  558.             log endsWhere
  559.             set endOfURL to (minimumPositiveNumber from endsWhere) - 1
  560.            
  561.             if endOfURL = -1 then
  562.                 -- We have reached the end of the input
  563.                 set theURL to theCurrentHTTPorHTTPS
  564.             else
  565.                 set theURL to text 1 thru endOfURL of theCurrentHTTPorHTTPS
  566.             end if
  567.             log "--------------------------- " & theURL & "--------------------------- "
  568.             -- "curl --silent --location --max-time 15 " & theURL
  569.             set toUnix to "curl --silent --location --max-time 10 " & quoted form of theURL
  570.             log "toUnix  is " & toUnix
  571.             try
  572.                 log "reading link file to get title"
  573.                 set fromUnix to do shell script toUnix
  574.                 log "fromUnix"
  575.                 printHeader("fromUnix", fromUnix)
  576.                 -- may not be working with an HTLM document, so thefound title may be to long or confused.
  577.                 log "how far?"
  578.                 set actualTagData to tagContent(fromUnix, "<title", "</title>")
  579.                 log "actualTagData  is " & printHeader("actualTagData", actualTagData)
  580.                 if actualTagData is "" then
  581.                     set actualTagData to theURL
  582.                 else if length of actualTagData > 140 then
  583.                     log "length of actualTagData is " & length of actualTagData
  584.                     set actualTagData to theURL
  585.                     -- curl https://appleid.apple.com returns <title>403 Forbidden</title>
  586.                 else if actualTagData contains "403" and actualTagData contains "Forbidden" then
  587.                     set actualTagData to theURL
  588.                 else
  589.                     -- there could be some attributes within the tag
  590.                     -- an attribute could have a > in it. ignoring that for now.
  591.                     set actualTagData to text ((offset of ">" in actualTagData) + 1) thru (length of actualTagData) of actualTagData
  592.                     -- found line-end in title.  caused confustion.
  593.                     set actualTagData to alterString(actualTagData, return & lf, "  ")
  594.                     set actualTagData to alterString(actualTagData, return, " ")
  595.                     set actualTagData to alterString(actualTagData, lf, "  ")
  596.                 end if
  597.             on error errMsg number n
  598.                 log "==> Error occured when looking for title. " & errMsg & " with number " & n
  599.                 set actualTagData to theURL
  600.             end try
  601.             set assembled to "<a href=\"" & theURL & "\" target=\"_blank\">" & actualTagData & "</a>"
  602.             log "assembled  is " & assembled
  603.             if endOfURL = -1 then
  604.                 -- We have reached the end of the input
  605.                 set buildHTML to buildHTML & assembled
  606.             else
  607.                
  608.                 set buildHTML to buildHTML & assembled & text from (endOfURL + 1) to (length of theCurrentHTTPorHTTPS) of theCurrentHTTPorHTTPS
  609.             end if
  610.             -- wrap up
  611.             set theOutputBuffer to buildHTML
  612.             log "transformed text from buildHTML is  " & return & buildHTML
  613.             log "#" & counti & " transformed text from buildHTML is  " & return & buildHTML
  614.             -- number of links found
  615.             set counti to counti + 1
  616.            
  617.         end repeat -- looking for all links of the same type in document
  618.     end repeat -- scanning for https and http links
  619.    
  620.     (* add paragraphs *)
  621.    
  622.     -- start the theOutputBuffer with a paragraph tag.  We are taking a simple approach at this time.
  623.     set theOutputBuffer to "<p>" & theOutputBuffer
  624.     --  LF
  625.     -- Remember CRLF was changed to LF above and CR was chanaged to LF above.
  626.     -- we don't want no Windoze problems
  627.     set theOutputBuffer to alterString(theOutputBuffer, lf & lf, "</p><p> </p><p>")
  628.    
  629.     -- Does the string end with a dangling paragraph?  
  630.     if debug ≥ 3 then
  631.         log "length of theOutputBuffer is " & length of theOutputBuffer
  632.         log "((length of theOutputBuffer) - 2) is " & ((length of theOutputBuffer) - 2)
  633.         log "(length of theOutputBuffer)  is " & (length of theOutputBuffer)
  634.         log "((length of theOutputBuffer) - 3) is " & ((length of theOutputBuffer) - 3)
  635.     end if
  636.     if text ((length of theOutputBuffer) - 2) thru (length of theOutputBuffer) of theOutputBuffer is "<p>" then
  637.         set theOutputBuffer to text 1 thru ((length of theOutputBuffer) - 3) of theOutputBuffer
  638.     else if text ((length of theOutputBuffer) - 2) thru (length of theOutputBuffer) of theOutputBuffer is not "</p>" then
  639.         set theOutputBuffer to theOutputBuffer & "</p>"
  640.     end if
  641.    
  642.     log "theOutputBuffer is " & return & theOutputBuffer
  643.    
  644.     --convert to html clipboard format
  645.     return typeHTML(theOutputBuffer)
  646.    
  647. end typeText
  648.  
  649.  
  650.  
  651. (*
  652. https://www.oreilly.com/library/view/applescript-the-definitive/0596102119/re89.html
  653.  
  654. https://stackoverflow.com/questions/11085654/apple-script-how-can-i-copy-html-content-to-the-clipboard
  655.  
  656. -- user has copied a file's icon in the Finder
  657. clipboard info
  658. -- {{string, 20}, {«class ut16», 44}, {«class hfs », 80}, {«class
  659.  utf8», 20}, {Unicode text, 42}, {picture, 2616}, {«class icns», 43336},
  660. {«class furl», 62}}
  661.  
  662. textutil -convert html foo.rtf
  663.  
  664. if ((clipboard info) as string) contains "«class furl»" then
  665.         log "the clipboard contains a file named " & (the clipboard as string)
  666.     else
  667.         log "the clipboard does not contain a file"
  668.     end if
  669.    
  670. the clipboard       required
  671. as  class   optional
  672.  
  673. tell application "Script Editor"
  674.         activate
  675.     end tell
  676.    
  677. textutil has a simplistic text to html conversion
  678.     set clipboardDataQuoted to quoted form of theData
  679.     log "quoted form is " & clipboardDataQuoted
  680.    
  681.     set toUnix to "/bin/echo -n " & clipboardDataQuoted
  682.     set toUnix to toUnix & " | textutil -convert html -noload -nostore -stdin -stdout "
  683.     log "toUnix is " & toUnix
  684.     set fromUnix to do shell script toUnix
  685.     log "fromUnix  is " & fromUnix
  686.    
  687.    
  688. set s to "Today is my birthday"
  689. log text 1 thru ((offset of "my" in s) - 1) of s
  690. --> "Today is "
  691.             -- text 1 thru ((offset of "my" in s) - 1) of s
  692.             -- -1 since offset return the first character "m" position count
  693.            
  694. log "beginningOfTag is " & text 1 thru (minimumPositiveNumber from {200, length of beginningOfTag}) of beginningOfTag & "<+++++++++++++++++++++++"
  695.  
  696. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html
  697.  
  698. *)
  699.  
  700. --mac $ hex=`echo -n "<p>your html code here</>" | hexdump -ve '1/1 "%.2x"'`
  701. --mac $ echo $hex
  702. --3c703e796f75722068746d6c20636f646520686572653c2f3e
  703. --mac $ osascript -e "set the clipboard to «data HTML${hex}»"
  704. --mac $
  705. (*  
  706. A sub-routine for encoding ASCII characters.  
  707.  
  708. encode_char("$")  
  709. --> returns: "%24"  
  710.  
  711. based on:  
  712. https://www.macosxautomation.com/applescript/sbrt/sbrt-08.html  
  713.  
  714. *)
  715. (*
  716. Lowest Numeric Value in a List
  717.  
  718. This sub-routine will return the lowest numeric value in a list of items. The passed list can contain non-numeric data as well as lists within lists. For example:
  719.  
  720. lowest_number({-3.25, 23, 2345, "sid", 3, 67})
  721. --> returns: -3.25
  722. lowest_number({-3.25, 23, {-22, 78695, "bob"}, 2345, true, "sid", 3, 67})
  723. --> returns: -22
  724.  
  725. If there is no numeric data in the passed list, the sub-routine will return a null string ("")
  726.  
  727. lowest_number({"this", "list", "contains", "only", "text"})
  728. --> returns: ""
  729.  
  730. https://macosxautomation.com/applescript/sbrt/sbrt-03.html
  731.  
  732. Here's the sub-routine:
  733.  
  734. *)
  735. (*
  736. on lowestNumber(values_list)
  737.     set the low_amount to ""
  738.     repeat with i from 1 to the count of the values_list
  739.         set this_item to item i of the values_list
  740.         set the item_class to the class of this_item
  741.         if the item_class is in {integer, real} then
  742.             if the low_amount is "" then
  743.                 set the low_amount to this_item
  744.             else if this_item is less than the low_amount then
  745.                 set the low_amount to item i of the values_list
  746.             end if
  747.         else if the item_class is list then
  748.             set the low_value to lowest_number(this_item)
  749.             if the the low_value is less than the low_amount then ¬
  750.                 set the low_amount to the low_value
  751.         end if
  752.     end repeat
  753.     return the low_amount
  754. end lowestNumber
  755.  
  756. https://lists.apple.com/archives/applescript-users/2010/Sep/msg00139.html
  757. set list_of_values to {10, 20, 30, 40, 50, 60, 2000, 9, 3000, 4}
  758.  
  759. set minimum to 9.9999999999E+12
  760. set maximum to 0
  761. repeat with ref_to_value in list_of_values
  762.     set the_value to contents of ref_to_value
  763.     if the_value > maximum then set maximum to the_value
  764.     if the_value < minimum then set minimum to the_value
  765. end repeat
  766.  
  767. {minimum, maximum}
  768.  
  769. may do the trick.
  770.  
  771. Yvan KOENIG (VALLAURIS, France) lundi 13 septembre 2010 22:32:41
  772. *)
  773. (* https://lists.apple.com/archives/applescript-users/2010/Sep/msg00139.html
  774. set list_of_values to {10, 20, 30, 40, 50, 60, 2000, 9, 3000, 4}
  775.  
  776. set minimum to 9.9999999999E+12
  777.  
  778. assume it's limited to positive values
  779.  
  780.  
  781. on maxValue(list_of_values)
  782.     global debug
  783.     if debug ≥ 3 then log "in maxValue " & return & list_of_values
  784.     set maximum to 0
  785.     repeat with ref_to_value in list_of_values
  786.         set the_value to contents of ref_to_value
  787.         if the_value > maximum then set maximum to the_value
  788.     end repeat
  789.     if debug ≥ 3 then log maximum
  790.     return maximum
  791. end maxValue
  792. *)
  793. -- ------------------------------------------------------
  794. (*
  795. http://harvey.nu/applescript_url_encode_routine.html
  796.  
  797. on urlencode(theText)
  798.     set theTextEnc to ""
  799.     repeat with eachChar in characters of theText
  800.         set useChar to eachChar
  801.         set eachCharNum to ASCII number of eachChar
  802.         if eachCharNum = 32 then
  803.             set useChar to "+"
  804.         else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
  805.             set firstDig to round (eachCharNum / 16) rounding down
  806.             set secondDig to eachCharNum mod 16
  807.             if firstDig > 9 then
  808.                 set aNum to firstDig + 55
  809.                 set firstDig to ASCII character aNum
  810.             end if
  811.             if secondDig > 9 then
  812.                 set aNum to secondDig + 55
  813.                 set secondDig to ASCII character aNum
  814.             end if
  815.             set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
  816.             set useChar to numHex
  817.         end if
  818.         set theTextEnc to theTextEnc & useChar as string
  819.     end repeat
  820.     return theTextEnc
  821. end urlencode
  822. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement