Advertisement
rccharles

asc clipboard fixer may 2nd

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