Advertisement
rccharles

ASC adjust clipboard May 10, 2019

May 10th, 2019
1,228
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. may 3, 2019 -- regressed may 2nd update.  Applescript was inserting \ into output.
  34. may 8, 2019 -- special processing for html class on clipboard          
  35.  
  36. enhancements:
  37.   -- get pdf title
  38.  
  39.  
  40. Author: rccharles
  41.  
  42.  Copyright 2019 rccharles  
  43.      
  44.        Permission is hereby granted, free of charge, to any person obtaining a copy  
  45.        of this software and associated documentation files (the "Software"), to deal  
  46.        in the Software without restriction, including without limitation the rights  
  47.        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  48.        copies of the Software, and to permit persons to whom the Software is  
  49.        furnished to do so, subject to the following conditions:  
  50.        
  51.        The above copyright notice and this permission notice shall be included in all  
  52.        copies or substantial portions of the Software.  
  53.        
  54.        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  55.        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  56.        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  57.        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  58.        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  59.        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
  60.        SOFTWARE.  
  61.  
  62.  
  63.     example text document: remember to edit out the returns.
  64.     set the clipboard to «data HTML3C68746D6C3E3C686561643E3C6D65746120687474702D65717569763D22636
  65.     F6E74656E742D747970652220636F6E74656
  66.     E743D22746578742F68746D6C3B206368617273657
  67.     43D7574662D38223E3C2F686561643E3C626F64793E3C62723E0A202020203C62207374
  68.     796C653D22636F6C6F723A677265656E3B2
  69.     23E506172616C6C656C733C2F623E3A3C62723E0A2
  70.     0202020467265652076657273696F6E206F6620506172616C6C656C7320666F7220696E6
  71.     46976696475616C207573653A3C62723E0A
  72.     68747470733A2F2F6974756E65732E6170706C652E
  73.     636F6D2F75732F6170702F706172616C6C656C732D6465736B746F702D6C6974652F696
  74.     4313038353131343730393F6D743D31323C
  75.     62723E0A2020202046756C6C2076657273696F6E3C6
  76.     2723E0A202020203C6120687265663D22687474703A2F2F7777772E706172616C6C656C
  77.     732E636F6D2F656E2F70726F64756374732F
  78.     6465736B746F702F223E687474703A2F2F7777772E7
  79.     06172616C6C656C732E636F6D2F656E2F70726F64756374732F6465736B746F702F3C2F6
  80.     13E3C62723E0A202020203C62723E0A2020
  81.     20203C623E564D7761726520467573696F6E3C2F62
  82.     3E3C62723E0A202020205769746820564D7761726520467573696F6E2C2072756E20746
  83.     865206D6F73742064656D616E64696E6720
  84.     4D616320616E642057696E646F77730A20202020617
  85.     0706C69636174696F6E7320730A6964652D62792D73696465206174206D6178696D756
  86.     D2073706565647320776974686F7574207265626F6F74696E673C62723E0A20202020687
  87.     474703A
  88.     2F2F7777772E766D776172652E636F6D2F70726F64756374732F667573696F6E2F3C2F62
  89.     6F64793E3C2F68746D6C3E»
  90.    
  91.     Translated text is:
  92.     Full version<br>
  93.     <a href="http://www.parallels.com/en/products/desktop/">http://www.parallels.com/en/products/desktop/</a><br>
  94.     <br>
  95.     <b>VMware Fusion</b><br>
  96.    
  97.     set the clipboard to «data HTML2020202046756C6C2076657273696F6E3C62723E0A202020203C612068726566
  98.     3D22687474703A2F2F7777772E706172616
  99.     C6C656C732E636F6D2F656E2F70726F64756374732F6465736B746F702F223E687474703
  100.     A2F2F7777772E706172616C6C656C732E63
  101.     6F6D2F656E2F70726F64756374732F6465736B746F702F3C2F613E3C62723E0A20202020
  102.     3C62723E0A202020203C623E564D7761726
  103.     520467573696F6E3C2F623E3C62723E0A»
  104.    
  105. set the clipboard to "Saturday, September 7, 2019
  106. Live streamed
  107. https://www.omf.ngo/community-symposium-2/"
  108.  
  109. set the clipboard to "\"Effective defenses 111 threats\" by John Galt
  110. https://discussions.apple.com/docs/DOC-8841
  111. \"Avoid phishing emails 222 and other scams\""
  112.  
  113. https://support.apple.com/en-ca/HT204759
  114.  
  115. blank lines
  116. also,see:http://www.google.com/ seeing again:http://www.google.com"
  117.  
  118.  *)
  119.  
  120. (* For whatever reason, this segment doesn't work when moved above.
  121.     set the clipboard to "<p>Simple put, Apple attempts to provide all the malware detection and removal you need in Mac OS X.</p>
  122. <p>\"Effective defenses against malware and other threats\" by John Galt
  123. <a href=\"https://discussions.apple.com/docs/DOC-8841\" target=\"_blank\">Effective
  124. defenses against malware and ot… - Apple Community</a>
  125. </p><p> </p>"
  126.    *)
  127. (*
  128. set the clipboard to "<p>Simple put, Apple attempts to provide all the malware detection and removal you need in Mac OS X.</p>
  129. <p>\"Effective defenses against malware and other threats\" by John Galt
  130. <a href=\"https://discussions.apple.com/docs/DOC-8841\" target=\"_blank\">Effective
  131. defenses against malware and to… - Apple Community</a>
  132. </p><p> </p>"
  133. *)
  134. (*
  135.     set the clipboard to "Saturday, September 7, 2019
  136. Live streamed
  137. https://www.omf.ngo/community-symposium-2/"
  138. *)
  139.  
  140.  
  141. -- Gets invoked here when you run in AppleScript editor or double click on the app icon.
  142. on run
  143.     global debug
  144.     set debug to 2
  145.    
  146.     set theList to clipboard info
  147.     printClipboardInfo(theList)
  148.    
  149.     set cbInfo to get (clipboard info) as string
  150.    
  151.     -- Most likely, if we have HTML data in the clipboard it will be from a web browser or Word.
  152.     if cbInfo contains "HTML" then
  153.        
  154.         log "Working with HTML Class data from clipboard."
  155.         set theBoard to the clipboard as «class HTML»
  156.         --log "Print out inputted HTML data on the clipboard..." -- it's just going to be a hex string. waste.
  157.         --log theBoard
  158.        
  159.         set normalHtml to do shell script "osascript -e 'try' -e 'get the clipboard as «class HTML»' -e 'end try' | awk '{sub(/«data HTML/, \"\") sub(/»/, \"\")} {print}' | xxd -r -p "
  160.         log "...Print out plan text version of inputed HTML data from the clipboard..." & return & normalHtml
  161.         log "printed in hex"
  162.         hexDumpFormat("normalHtml", normalHtml)
  163.        
  164.         set returnedData to adjustBrowserHTML(normalHtml)
  165.         log "...Print out plan text version of adjusted HTML data ..." & return & returnedData
  166.         log "...just printed plan text version"
  167.         log "printed in hex"
  168.         hexDumpFormat("returnedData", returnedData)
  169.        
  170.         set returnedData to convertToHTML(returnedData)
  171.         try
  172.             log "returnedData is " & returnedData
  173.         on error errStr number errorNumber
  174.             log "===> We didn't find HTML data.   errStr is " & errStr & " errorNumber is " & errorNumber
  175.             return
  176.         end try
  177.     else
  178.         -- will work with a plan html or plan text.
  179.         try
  180.             log "Working with plan html or plan text"
  181.             set clipboardData to (the clipboard as text)
  182.             if debug ≥ 2 then
  183.                 log "class clipboardData is " & class of clipboardData
  184.                 log "calling printHeader."
  185.             end if
  186.             log "continueing plan html or plan text"
  187.             printHeader("clipboardData", clipboardData)
  188.         on error errStr number errorNumber
  189.             log "===> We didn't find data on the clipboard.   errStr is " & errStr & " errorNumber is " & errorNumber
  190.             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
  191.             return 1
  192.         end try
  193.         log "calling common"
  194.         set returnedData to common(clipboardData)
  195.     end if
  196.     log "place on the clipboard returnedData is " & returnedData
  197.     postToCLipboard(returnedData)
  198.     -- return code
  199.     return 0
  200.    
  201.    
  202. end run
  203.  
  204. -- Folder actions.
  205. -- Gets invoked here when something is dropped on the folder that this script is monitoring.
  206. -- Right click on the folder to be monitored. services > Folder Action Settup...
  207. on adding folder items to this_folder after receiving added_items
  208.     -- TBD
  209.    
  210. end adding folder items to
  211.  
  212.  
  213.  
  214. -- Gets invoked here when something is dropped on this AppleScript icon
  215. on open dropped_items
  216.     global debug
  217.     set debug to 2
  218.    
  219.     (*
  220.     -- Debug code.
  221.       set fileName to choose file with prompt "get file"
  222.       set dropped_items to {fileName}
  223.     *)
  224.     log "class of dropped_items is " & class of dropped_items
  225.     display dialog "You dropped " & (count of dropped_items) & " item or items." & return & "  Caveat emptor. You have been warned." giving up after 6
  226.    
  227.     set totalFileData to ""
  228.     repeat with droppedItem in dropped_items
  229.         log "The droppedItem is "
  230.         -- display dialog "processing file " & (droppedItem as string) giving up after 3
  231.         log droppedItem
  232.         log "class = " & class of droppedItem
  233.         set extIs to findExtension(droppedItem)
  234.         set extIsU to makeCaseUpper(extIs)
  235.         if extIsU is "HTML" or extIsU is "HTM" or extIsU is "TEXT" or extIsU is "TXT" then
  236.             try
  237.                 set theFile to droppedItem as string
  238.                 set theFile to open for access file theFile
  239.                 set allOfFile to read theFile
  240.                 close access theFile
  241.                 printHeader("read from file ( allOfFile )", allOfFile)
  242.                 set totalFileData to totalFileData & common(allOfFile)
  243.             on error theErrorMessage number theErrorNumber
  244.                 log theErrorMessage & "error number " & theErrorNumber
  245.                 close access theFile
  246.             end try
  247.            
  248.         else
  249.             -- we do not support this extension
  250.             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
  251.            
  252.         end if
  253.     end repeat
  254.    
  255.     postToCLipboard(totalFileData)
  256.     -- return code
  257.     return 0
  258.    
  259. end open
  260.  
  261.  
  262. -- ------------------------------------------------------
  263. on common(clipboardData)
  264.     global debug
  265.     set lf to character id 10
  266.     -- Write a message into the event log.
  267.     log "  --- Starting on " & ((current date) as string) & " --- "
  268.     set cbInfo to get (clipboard info) as string
  269.    
  270.    
  271.     -- don't let Windoze confuse us. convert Return LineFeed to lf
  272.     set clipboardData to alterString(clipboardData, return & lf, lf)
  273.     -- might as will convert classic macOS return to lf. We will have to look for less things.
  274.     set clipboardData to alterString(clipboardData, return, lf)
  275.    
  276.     -- figure out what type of data we have: plan text or html source code text.
  277.     set paraCount to count of textToList(clipboardData, "<p")
  278.     set endparaCount to count of textToList(clipboardData, "</p>")
  279.     set titleCount to count of textToList(clipboardData, "<title")
  280.     set endTitleCount to count of textToList(clipboardData, "</title>")
  281.     set aLinkCount to count of textToList(clipboardData, "href=\"http")
  282.     -- mangled href="http
  283.     set mangledLinkCount to count of textToList(clipboardData, "href=\\\"http")
  284.     set brCount to count of textToList(clipboardData, "<br>")
  285.     if debug ≥ 1 then
  286.         log "Values used to distinguis HTML source code from plan text."
  287.         log "paraCount  is " & paraCount
  288.         log "endparaCount is " & endparaCount
  289.         log "titleCount is " & titleCount
  290.         log "endTitleCount is " & endTitleCount
  291.         log "aLinkCount is " & aLinkCount
  292.         log "brCount is " & brCount
  293.         log "mangledLinkCount is " & mangledLinkCount
  294.     end if
  295.     --set endHttpCount to count of textToList(clipboardData, "http://")
  296.     --set endHttpsCount to count of textToList(clipboardData, "https://")
  297.     -- note, textToList returns a count one greater than the actual because item one is the data before the first found entry.
  298.     if paraCount ≥ 4 and endparaCount ≥ 3 or brCount ≥ 4 or ((titleCount is endTitleCount) and titleCount ≥ 2) or aLinkCount ≥ 3 or mangledLinkCount ≥ 3 then
  299.         log "... found HTML input ... (in plan text format )."
  300.        
  301.         set clipboardData to adjustURLs(clipboardData)
  302.         set clipboardData to adjustAscHTML(clipboardData)
  303.         set readyData to convertToHTML(clipboardData)
  304.        
  305.     else
  306.         log "... found plan Text input ..."
  307.         set readyData to typeText(clipboardData)
  308.         set readyData to convertToHTML(readyData)
  309.        
  310.     end if
  311.     return readyData
  312. end common
  313.  
  314. -- ------------------------------------------------------  
  315. (* add paragraphs *)
  316. on addParagraphs(theOutputBuffer)
  317.     global debug
  318.     set lf to character id 10
  319.    
  320.     -- start the theOutputBuffer with a paragraph tag.  We are taking a simple approach at this time.
  321.     set theOutputBuffer to "<p>" & theOutputBuffer
  322.     --  LF
  323.     -- Remember CRLF was changed to LF above and CR was chanaged to LF above.
  324.     -- we don't want no Windoze problems
  325.     set theOutputBuffer to alterString(theOutputBuffer, lf & lf, "</p><p> </p><p>")
  326.    
  327.     -- Does the string end with a dangling paragraph?  
  328.     if debug ≥ 3 then
  329.         log "length of theOutputBuffer is " & length of theOutputBuffer
  330.         log "((length of theOutputBuffer) - 2) is " & ((length of theOutputBuffer) - 2)
  331.         log "(length of theOutputBuffer)  is " & (length of theOutputBuffer)
  332.         log "((length of theOutputBuffer) - 3) is " & ((length of theOutputBuffer) - 3)
  333.     end if
  334.     if text ((length of theOutputBuffer) - 2) thru (length of theOutputBuffer) of theOutputBuffer is "<p>" then
  335.         set theOutputBuffer to text 1 thru ((length of theOutputBuffer) - 3) of theOutputBuffer
  336.     else if text ((length of theOutputBuffer) - 2) thru (length of theOutputBuffer) of theOutputBuffer is not "</p>" then
  337.         set theOutputBuffer to theOutputBuffer & "</p>"
  338.     end if
  339.     return theOutputBuffer
  340. end addParagraphs
  341.  
  342. -- ------------------------------------------------------
  343. (*
  344.   We received HTML class data on the clipboard.  This is the manager.
  345.  *)
  346. on adjustBrowserHTML(normalHtml)
  347.     set lf to character id 10
  348.     -- don't let Windoze confuse us. convert Return LineFeed to lf
  349.     set normalHtml to alterString(normalHtml, return & lf, lf)
  350.     -- might as will convert classic macOS return to lf. We will have to look for less things.
  351.     set normalHtml to alterString(normalHtml, return, lf)
  352.     hexDumpFormat("normalHtml", normalHtml)
  353.    
  354.     set alteredHTML to adjustURLs(normalHtml)
  355.     set alteredHTML to adjustAscHTML(alteredHTML)
  356.     return alteredHTML
  357. end adjustBrowserHTML
  358.  
  359. -- ------------------------------------------------------
  360. (* ASC likes to insert lots of white space into a page.
  361.   This routing attempt to fix up the html to avoid
  362.   all the extra white-space.
  363.    Minimize the amount of white space inserted.
  364.  *)
  365. on adjustAscHTML(AscHtml)
  366.     -- surprisingly ASC converts <p> </p> to <p><br></p>, that is a
  367.     -- space only paragraph to a paragraph with a <br> in it.
  368.     -- get rid of the space to avoid this conversion.  
  369.     set AscHtml to alterString(AscHtml, "<p> </p>", "<p></p>")
  370.     return AscHtml
  371. end adjustAscHTML
  372. -- ------------------------------------------------------
  373. (*
  374. example:
  375.   Free version of Parallels for individual use:</p><p><br></p>
  376.   <p>https://itunes.apple.com/us/app/parallels-desktop-lite/id1085114709?mt=12</p>
  377.   <p><br></p>
  378.   <p>Full version</p><p><a href="http://www.parallels.com/en/products/desktop/" target="_blank">
  379.      http://www.parallels.com/en/products/desktop/</a>
  380.      
  381. If asc find a URL outside of an a tag, it will place blank lines around the URL. No, it will not go the
  382. full nine yards and place an a tag around the url.
  383.  
  384. *)
  385. on adjustURLs(theOriginalInputBuffer)
  386.     global debug
  387.     set alteredBuffer to false
  388.     set lf to character id 10
  389.     set theInputBuffer to theOriginalInputBuffer
  390.     hexDumpFormat("theInputBuffer", theInputBuffer)
  391.    
  392.     -- we end up in a lot of grief when the buffer ends without
  393.     -- a line-end
  394.     if text (length of theInputBuffer) thru (length of theInputBuffer) of theInputBuffer is not lf then
  395.         set alteredBuffer to true
  396.         set theInputBuffer to theInputBuffer & lf
  397.         hexDumpFormat("theInputBuffer", theInputBuffer)
  398.     end if
  399.     set buildHTML to ""
  400.     if debug ≥ 3 then log "buildHTML [ should be empty string ] is " & buildHTML
  401.     set countI to 1 -- variable is used for debuging.
  402.     -- do until we have processed theInputBuffer
  403.     repeat until theInputBuffer is ""
  404.         log "at the top of theInputBuffer ........."
  405.        
  406.         set foundWhere to {}
  407.         repeat with lookCharacters in {"https://", "http://", "<a "}
  408.             copy (offset of lookCharacters in theInputBuffer) to the end of the foundWhere
  409.             try
  410.                 set tempLoc to (offset of lookCharacters in theInputBuffer)
  411.                 log "searching for " & lookCharacters & " found at offset  " & tempLoc & " contains " & text tempLoc thru (tempLoc + ((length of lookCharacters) - 1)) of theInputBuffer
  412.             end try
  413.         end repeat
  414.         log foundWhere
  415.         set foundMarkerOffset to (minimumPositiveNumber from foundWhere)
  416.         -- figure out what type of marker we got?
  417.        
  418.         -- None.  Reached the end of the data without finding one.
  419.         if foundMarkerOffset ≤ 0 then
  420.             -- we are done
  421.             log "Found all links."
  422.             set buildHTML to buildHTML & theInputBuffer
  423.             printHeader("buildHTML", buildHTML)
  424.             set theInputBuffer to ""
  425.             exit repeat -- ------ done processing theInputBuffer ------>
  426.         end if
  427.        
  428.         -- find which of three markers we found.
  429.         if (text foundMarkerOffset thru (foundMarkerOffset + 2) of theInputBuffer) is "<a " then
  430.             set actualMarker to "<a "
  431.         else if text foundMarkerOffset thru (foundMarkerOffset + 6) of theInputBuffer is "http://" then
  432.             set actualMarker to "http://"
  433.         else
  434.             -- just assume it's the remaining "https://" since we looked for just three.
  435.             set actualMarker to "https://"
  436.         end if
  437.         set actualMarkerOffsetLength to ((length of actualMarker) - 1)
  438.         log "actualMarker is " & actualMarker & " actualMarkerOffsetLength is " & actualMarkerOffsetLength
  439.        
  440.         log "foundMarkerOffset is " & foundMarkerOffset & "  verify marker text is " & text foundMarkerOffset thru (foundMarkerOffset + actualMarkerOffsetLength) of theInputBuffer
  441.        
  442.        
  443.        
  444.         if foundMarkerOffset ≥ 2 then
  445.             -- collect and strip off characters that are before the marker.
  446.             log "buildHTML is " & buildHTML & " length is " & length of buildHTML
  447.             hexDumpFormat("theInputBuffer", theInputBuffer)
  448.             log " (foundMarkerOffset - 1) is " & (foundMarkerOffset - 1)
  449.             -- get the proceding text
  450.             set buildHTML to buildHTML & text 1 thru (foundMarkerOffset - 1) of theInputBuffer
  451.             log "buildHTML is " & buildHTML
  452.             --printHeader("buildHTML", buildHTML)
  453.             hexDumpFormat("buildHTML", buildHTML)
  454.            
  455.             -- https://apple.stackexchange.com/a/20135/44531
  456.            
  457.             set theInputBuffer to text foundMarkerOffset thru -1 of theInputBuffer --trim off character before what we found
  458.             printHeader("theInputBuffer", theInputBuffer)
  459.             hexDumpFormat("theInputBuffer", theInputBuffer)
  460.         else
  461.             log "no proceeding data."
  462.         end if
  463.        
  464.         repeat 1 times -- interate loop
  465.            
  466.             -- example" the url is also the display text
  467.             -- <a href="https://discussions.apple.com/docs/DOC-8841" target="_blank">https://discussions.apple.com/docs/DOC-8841</a>
  468.             hexDumpFormat("theInputBuffer", theInputBuffer)
  469.            
  470.             -- check for the <a> tag
  471.             if text 1 thru (length of "<a ") of theInputBuffer is "<a " then
  472.                 -- found <a> tag
  473.                 log "processing <a> tag"
  474.                 -- ASC consider a line-end as a <br> when when firefox considers it a blank
  475.                 -- change a possible line-end before an <a> tag to a " "
  476.                 if debug ≥ 1 then hexDumpFormat("before lf check buildHTML", buildHTML)
  477.                 if text (length of buildHTML) thru (length of buildHTML) of buildHTML is lf then
  478.                     log "we need to delete a line-end before the <a> tag"
  479.                     set buildHTML to text 1 thru ((length of buildHTML) - 1) of buildHTML
  480.                     set buildHTML to buildHTML & " "
  481.                     if debug ≥ 1 then hexDumpFormat("after lf deletion buildHTML", buildHTML)
  482.                 end if
  483.                 -- find ending </a> tag
  484.                 set whereEnds to offset of "</a>" in theInputBuffer
  485.                 if whereEnds ≤ 0 then
  486.                     log "==> found an error in the HTML.  no ending </a>"
  487.                     set buildHTML to buildHTML & theInputBuffer
  488.                     printHeader("buildHTML", buildHTML)
  489.                     set theInputBuffer to ""
  490.                     exit repeat -- ------ next ------>
  491.                 end if
  492.                 set lastOffsetLength to ((length of "</a>") - 1)
  493.                 log "lastOffsetLength is " & lastOffsetLength
  494.                 set lastCharacterOffset to whereEnds + lastOffsetLength
  495.                 log "lastCharacterOffset is " & lastCharacterOffset
  496.                 -- needs to copy the ending ">"
  497.                 set anchorString to text 1 thru lastCharacterOffset of theInputBuffer
  498.                 -- don't let Windoze confuse us. convert Return LineFeed to lf
  499.                 -- Correct absure ASC bug where there is a line-end in the <a> text.
  500.                 hexDumpFormat("before adjusting anchorString", anchorString)
  501.                 set anchorString to alterString(anchorString, lf, " ")
  502.                 hexDumpFormat("anchorString", anchorString)
  503.                 set buildHTML to buildHTML & anchorString
  504.                 hexDumpFormat("buildHTML", buildHTML)
  505.                 -- https://apple.stackexchange.com/a/20135/44531
  506.                 -- We want first character of the "next" portion of theInputBuffer so add one
  507.                 set theInputBuffer to text (lastCharacterOffset + 1) thru -1 of theInputBuffer --trim out <a>
  508.                 hexDumpFormat("theInputBuffer", theInputBuffer)
  509.                 -- Web Browsers like Firefox convert a line-end in text to a space.
  510.                 if text 1 thru 1 of theInputBuffer is lf then
  511.                     if (length of theInputBuffer) is 1 then
  512.                         set theInputBuffer to " "
  513.                     else
  514.                         set theInputBuffer to " " & (text 2 thru (length of theInputBuffer) of theInputBuffer)
  515.                         if debug ≥ 1 then hexDumpFormat("after lf deletion; theInputBuffer", theInputBuffer)
  516.                     end if
  517.                 end if
  518.                 exit repeat -- ------ next ------>
  519.             end if
  520.            
  521.             -- find the end of the HTML URL by splitting on blank or return
  522.             -- unsafe characters  <blank> " < > # % { } | \ ^ ~ [ ] `
  523.             -- and line-end
  524.             -- https://perishablepress.com/stop-using-unsafe-characters-in-urls/
  525.             -- the end of the clipboard string my end after the url, hence no " ", LF or CR
  526.             -- Rember, CRLF was converted to LF above
  527.             set endsWhere to {}
  528.             -- the end of the url ends with one of the not allowed characters + line-end
  529.             repeat with unsafeCharacter in {" ", "\"", lf, "<", ">", "#", "%", "{", "}", "|", "\\", "^", "~", "[", "]"}
  530.                 copy (offset of unsafeCharacter in theInputBuffer) to the end of the endsWhere
  531.             end repeat
  532.             log endsWhere
  533.             set endOfURL to (minimumPositiveNumber from endsWhere) - 1
  534.            
  535.             log "endOfURL is " & endOfURL
  536.            
  537.             if endOfURL ≤ 0 then
  538.                 -- We have reached the end of the input
  539.                 set theURL to theInputBuffer
  540.                 set theInputBuffer to ""
  541.             else
  542.                 set theURL to text 1 thru endOfURL of theInputBuffer
  543.                 log "from middle theURL is " & theURL
  544.                
  545.                 set theInputBuffer to text (endOfURL + 1) thru -1 of theInputBuffer -- trim off url in front.
  546.             end if
  547.             printHeader("printHeader", theInputBuffer)
  548.             log "----------------------- " & theURL & " -----------------------"
  549.             (*
  550.             retrieve the file pointed to by the URL so we can
  551.             get the title. Note: <title> can have attributes.  Example:
  552.                
  553.             <title data-test-page-title="Parallels Desktop Lite on the Mac App Store"
  554.             >‎Parallels Desktop Lite on the Mac App Store</title>
  555.            
  556.             *)
  557.            
  558.             -- Example:
  559.             -- curl --silent --location --max-time 10 <URL>
  560.             set toUnix to "curl --silent --location --max-time 10 " & quoted form of theURL
  561.             log "what we will use to retrieve the Url. toUnix  is " & return & "  " & toUnix
  562.             try
  563.                 log "reading link file to get title"
  564.                 set fromUnix to do shell script toUnix
  565.                 --log "fromUnix:"
  566.                 printHeader("fromUnix", fromUnix)
  567.                 -- may not be working with an HTLM document, so thefound title may be too long or confused.
  568.                 log "how far?..."
  569.                 -- there could be some bagage with the <title
  570.                 set actualTagData to tagContent(fromUnix, "<title", "</title>")
  571.                 -- Find what we will actually display in the title.
  572.                 -- Fix up gotchas.             
  573.                 log "actualTagData  is " & printHeader("actualTagData", actualTagData)
  574.                 if actualTagData is "" then
  575.                     set actualTagData to theURL
  576.                 else if length of actualTagData > 140 then
  577.                     log "length of actualTagData is " & length of actualTagData & "which is too long.  Truncated."
  578.                     set actualTagData to theURL
  579.                     -- curl https://appleid.apple.com returns <title>403 Forbidden</title>
  580.                     -- which is misleading.
  581.                 else if actualTagData contains "403" and actualTagData contains "Forbidden" then
  582.                     set actualTagData to theURL
  583.                 else
  584.                     -- there could be some attributes within the <title> tag.
  585.                     -- or there could not be
  586.                     -- an attribute could have a > in it. ignoring that for now.
  587.                     try
  588.                         -- find where <title ends
  589.                         set whereToEnd to (offset of ">" in actualTagData)
  590.                         log "whereToEnd is " & whereToEnd
  591.                         set whereToBegin to whereToEnd + (length of ">")
  592.                         log "whereToBegin is " & whereToBegin
  593.                         hexDumpFormat("actualTagData", actualTagData)
  594.                         set actualTagData to text whereToBegin thru (length of actualTagData) of actualTagData
  595.                         log "actualTagData is " & actualTagData
  596.                     on error theErrorMessage number theErrorNumber
  597.                         log "==>No ending greater than (>) for title. Badly contructed html." & return & "message is " & theErrorMessage & "error number " & theErrorNumber
  598.                         set actualTagData to actualTagData
  599.                         -- no need to repair.  It's not our page.
  600.                     end try
  601.                    
  602.                     -- found line-end in title.  caused confustion.
  603.                     -- note: this is new data and the multiple line-ends have not been
  604.                     -- filtered out.
  605.                     -- some joker had a line-end in the title!
  606.                     set actualTagData to alterString(actualTagData, return & lf, "  ")
  607.                     set actualTagData to alterString(actualTagData, return, " ")
  608.                     set actualTagData to alterString(actualTagData, lf, "  ")
  609.                     log "actualTagData has been chanaged which is  " & actualTagData
  610.                     hexDumpFormat("actualTagData", actualTagData)
  611.                 end if
  612.             on error errMsg number n
  613.                 log "==> Error occured when looking for title. " & errMsg & " with number " & n
  614.                 set actualTagData to theURL
  615.             end try
  616.             -- why the _blank in the <a>?
  617.             set assembled to "<a href=\"" & theURL & "\" target=\"_blank\">" & actualTagData & "</a>"
  618.             log "assembled  is " & assembled
  619.            
  620.             if (length of theInputBuffer)0 then
  621.                 -- We have reached the end of the input
  622.                 log "we have reached the end of the input."
  623.                 set buildHTML to buildHTML & assembled
  624.             else
  625.                 log "more input to process"
  626.                 set buildHTML to buildHTML & assembled
  627.             end if
  628.            
  629.             -- wrap up
  630.             --log "transformed text from buildHTML is  " & return & buildHTML
  631.             log "#" & countI & " transformed text from buildHTML is  " & return & buildHTML
  632.             -- number of links found
  633.             set countI to countI + 1
  634.            
  635.         end repeat -- used to interate
  636.     end repeat -- processing links in the input text
  637.     if alteredBuffer is true then
  638.         -- chop off the lf we added above.
  639.         set buildHTML to text 1 thru ((length of buildHTML) - 1) of buildHTML
  640.         set alteredBuffer to false -- somewhat redundant
  641.     end if
  642.     return the buildHTML
  643.    
  644. end adjustURLs
  645.  
  646. -- ------------------------------------------------------
  647. (*
  648. alterString
  649.   thisText is the input string to change
  650.   delim is what string to change.  It doesn't have to be a single character.
  651.   replacement is the new string
  652.  
  653.   returns the changed string.
  654. *)
  655.  
  656. on alterString(thisText, delim, replacement)
  657.     set resultList to {}
  658.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  659.     try
  660.         set resultList to every text item of thisText
  661.         set text item delimiters to replacement
  662.         set resultString to resultList as string
  663.         set my text item delimiters to tid
  664.     on error
  665.         set my text item delimiters to tid
  666.     end try
  667.     return resultString
  668. end alterString
  669.  
  670. -- ------------------------------------------------------
  671. (*
  672.   Return the text to the right of theToken.
  673. *)
  674. on answerAndChomp(theString, theToken)
  675.     set debugging to false
  676.     set theOffset to offset of theToken in theString
  677.     if debugging then log "theOffset is " & theOffset
  678.     set theLength to length of theString
  679.     if theOffset > 0 then
  680.         set beginningPart to text 1 thru (theOffset - 1) of theString
  681.         if debugging then log "beginningPart is " & beginningPart
  682.        
  683.         set chompped to text theOffset thru theLength of theString
  684.         if debugging then log "chompped is " & chompped
  685.         return {chompped, beginningPart}
  686.     else
  687.         set beginningPart to ""
  688.         return {theString, beginningPart}
  689.     end if
  690.    
  691. end answerAndChomp
  692.  
  693. -- ------------------------------------------------------
  694. (*
  695.   Delete the leading part of the string until and including theToken.
  696. *)
  697. on chompLeftAndTag(theString, theToken)
  698.     set debugging to false
  699.     --log text 1 thru ((offset of "my" in s) - 1) of s
  700.     --set rightString to offset of theToken in theString thru count of theString of theString
  701.     set theOffset to offset of theToken in theString
  702.     if debugging then log "theOffset is " & theOffset
  703.     set theLength to length of theString
  704.     if debugging then log "theLength is " & theLength
  705.     if theOffset > 0 then
  706.         set chompped to text (theOffset + (length of theToken)) thru theLength of theString
  707.         if debugging then log "chompped is " & chompped
  708.         return chompped
  709.     else
  710.         return ""
  711.     end if
  712. end chompLeftAndTag
  713.  
  714. -- ------------------------------------------------------  
  715. (*
  716. Yvan Koenig
  717. https://macscripter.net/viewtopic.php?id=43133
  718. *)
  719. on findExtension(inputFileName)
  720.     set fileName to inputFileName as string
  721.     set saveTID to AppleScript's text item delimiters
  722.     set AppleScript's text item delimiters to {"."}
  723.     set theExt to last text item of fileName
  724.     set AppleScript's text item delimiters to saveTID
  725.     --log "theExt is " & theExt
  726.     if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
  727.     --log "theExt is " & theExt
  728.     return theExt
  729. end findExtension
  730.  
  731. -- ------------------------------------------------------
  732. (*
  733.   http://krypted.com/mac-os-x/to-hex-and-back/
  734.   0000000: 3c68 746d 6c3e 3c68 6561 643e 3c6d 6574  <html><head><met
  735. "         0    2    4    6    8    a    c    e     0 2 4 6 8 a c e"
  736.  
  737.  
  738.   *)
  739. on hexDumpFormat(textMessage, hex)
  740.     global debug
  741.     if debug ≥ 3 then log "in hexDumpFormat"
  742.     if debug ≥ 3 then log "hex string is " & hex
  743.     -- -r -p
  744.     set toUnix to "/bin/echo -n " & (quoted form of hex) & " | xxd  "
  745.     if debug ≥ 3 then log "toUnix is " & toUnix
  746.     try
  747.         set fromUnix to do shell script toUnix
  748.         log "variable " & textMessage & " in hex is " & return & "         0    2    4    6    8    a    c    e     0 2 4 6 8 a c e" & return & fromUnix
  749.     on error errMsg number n
  750.         log "==> convert hex string to string failed. " & errMsg & " with number " & n
  751.     end try
  752. end hexDumpFormat
  753.  
  754.  
  755. -- ------------------------------------------------------
  756. (*
  757.  
  758. https://stackoverflow.com/questions/55838252/minimum-value-that-not-zero
  759.        set m to get minimumPositiveNumber from {10, 2, 0, 2, 4}
  760.     log "m is " & m
  761.     set m to minimumPositiveNumber from {0, 0, 0}
  762.     log "m is " & m
  763. *)
  764. on minimumPositiveNumber from L
  765.     local L
  766.    
  767.     if L = {} then return null
  768.    
  769.     set |ξ| to 0
  770.    
  771.     repeat with x in L
  772.         set x to x's contents
  773.         if (x < |ξ| and x ≠ 0) ¬
  774.             or |ξ| = 0 then ¬
  775.             set |ξ| to x
  776.     end repeat
  777.    
  778.     |ξ|
  779. end minimumPositiveNumber
  780.  
  781. -- ------------------------------------------------------
  782. (*
  783.   makeCaseUpper("Now is the time, perhaps, for all good men")
  784. *)
  785. on makeCaseUpper(theString)
  786.     set UC to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  787.     set LC to "abcdefghijklmnopqrstuvwxyz"
  788.     set C to characters of theString
  789.     repeat with ch in C
  790.         if ch is in LC then set contents of ch to item (offset of ch in LC) of UC
  791.     end repeat
  792.     return C as string
  793. end makeCaseUpper
  794.  
  795. -- ------------------------------------------------------
  796. on postToCLipboard(pleasePost)
  797.     try
  798.         -- osascript -e "set the clipboard to «data HTML${hex}»"     
  799.         set toUnixSet to "osascript -e \"set the clipboard to «data HTML" & pleasePost & \""
  800.         log "toUnixSet is " & printHeader("toUnixSet", toUnixSet)
  801.        
  802.         set fromUnixSet to do shell script toUnixSet
  803.         log "fromUnixSet is " & fromUnixSet
  804.        
  805.     on error errMsg number n
  806.         log "==> We tried to send back HTML data, but failed. " & errMsg & " with number " & n
  807.     end try
  808.     -- see what ended up on the clipboard
  809.     set theList2 to clipboard info
  810.     printClipboardInfo(theList2)
  811. end postToCLipboard
  812.  
  813. -- ------------------------------------------------------
  814. on printClipboardInfo(theList)
  815.     log (clipboard info)
  816.     log class of theList
  817.     log "Data types on the clipboard ... "
  818.     printList("", theList)
  819.     log "... "
  820. end printClipboardInfo
  821.  
  822. -- ------------------------------------------------------
  823. (* Pump out the beginning of theString *)
  824. on printHeader(theName, theString)
  825.     global debug
  826.     if debug ≥ 3 then
  827.         log "in printHeader"
  828.         log theString
  829.         log length of theString
  830.     end if
  831.     if length of theString ≤ 0 then
  832.         log "==> no string to print"
  833.     else
  834.         log theName & " is " & text 1 thru (minimumPositiveNumber from {400, length of theString}) of theString & "<+++++++++"
  835.     end if
  836. end printHeader
  837.  
  838. -- ------------------------------------------------------
  839. (*
  840. print out the items in a list
  841.  
  842. *)
  843.  
  844. on printList(theName, splits)
  845.     try
  846.         set theCount to 1
  847.         repeat with theEntry in splits
  848.             --log "class of theEntry is " & class of theEntry
  849.             set classDisplay to class of theEntry as text
  850.             --log "classDisplay is " & classDisplay as text
  851.             --log "class of classDisplay is " & class of classDisplay
  852.             if classDisplay is "list" then
  853.                 log "    " & theName & theCount & " is " & item 1 of theEntry & "; " & item 2 of theEntry
  854.             else
  855.                 log "    " & theName & theCount & " is " & theEntry
  856.             end if
  857.             set theCount to theCount + 1
  858.         end repeat
  859.     on error errMsg number n
  860.         log "==> No go in printList. " & errMsg & " with number " & n
  861.     end try
  862. end printList
  863.  
  864. -- ------------------------------------------------------
  865. (*
  866. splitTextToList seems to be what you are trying to do
  867.   thisText is the input string
  868.   delim is what to split on
  869.  
  870.   results returned in a list
  871.  
  872.   Total hack. We know splitTextToList strips of delim so add it back.
  873. *)
  874.  
  875. on splitTextToList(thisText, delim)
  876.    
  877.     set returnedList to textToList(thisText, delim)
  878.     set resultArray to {}
  879.     copy item 1 of returnedList to the end of the resultArray
  880.    
  881.     repeat with i from 2 to (count of returnedList) in returnedList
  882.         set newElement to delim & item i of returnedList
  883.         copy newElement to the end of the resultArray
  884.     end repeat
  885.    
  886.     return resultArray
  887. end splitTextToList
  888.  
  889. -- ------------------------------------------------------
  890. (*
  891.   Retrieved data between "begin" and "end" tag. Whatever is between the strings.
  892. *)
  893. on tagContent(theString, startTag, endTag)
  894.     global debug
  895.     try
  896.         log "in tabContent. " & return & "    startTag is " & startTag & " endTag is " & endTag
  897.         set beginningOfTag to chompLeftAndTag(theString, startTag)
  898.         if length of beginningOfTag ≤ 0 then
  899.             set middleText to ""
  900.         else
  901.             printHeader("beginningOfTag", beginningOfTag)
  902.             set endingOffset to (offset of endTag in beginningOfTag)
  903.            
  904.             if endingOffset ≤ (length of endTag) then
  905.                 set middleText to ""
  906.             else
  907.                 set middleText to text 1 thru (endingOffset - 1) of beginningOfTag
  908.                 printHeader("middleText is ", middleText)
  909.             end if
  910.         end if
  911.     on error errMsg number n
  912.         log "finding contained text failed. " & errMsg & " with number " & n
  913.         set middleText to ""
  914.     end try
  915.     if debug ≥ 2 then log "returning with middleText is " & middleText
  916.     return middleText
  917. end tagContent
  918.  
  919. (*
  920. textToList seems to be what you are trying to do
  921.   thisText is the input string
  922.   delim is what to split on
  923.  
  924.   returns a list of strings.  
  925.  
  926. - textToList was found here:
  927. - http://macscripter.net/viewtopic.php?id=15423
  928.  
  929. *)
  930.  
  931. on textToList(thisText, delim)
  932.     set resultList to {}
  933.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  934.    
  935.     try
  936.         set resultList to every text item of thisText
  937.         set my text item delimiters to tid
  938.     on error
  939.         set my text item delimiters to tid
  940.     end try
  941.     return resultList
  942. end textToList
  943.  
  944. -- ------------------------------------------------------
  945. on convertToHTML(theData)
  946.     global debug
  947.     log "in convertToHTML" & return & "  Try to send back HTML."
  948.     try
  949.         set clipboardDataQuoted to quoted form of theData
  950.         printHeader("clipboardDataQuoted", clipboardDataQuoted)
  951.         hexDumpFormat("clipboardDataQuoted", clipboardDataQuoted)
  952.         -- make hex string as required for HTML data on the clipboard
  953.         set toUnix to "/bin/echo -n " & clipboardDataQuoted & " | hexdump -ve '1/1 \"%.2x\"'"
  954.         printHeader("toUnix to convert to hex", toUnix)
  955.        
  956.         set fromUnix to do shell script toUnix
  957.        
  958.         printHeader("fromUnix", fromUnix)
  959.        
  960.         if debug ≥ 2 then
  961.             log "displaying original string --- so we can tell if it converted successfully. "
  962.             --hexDumpFormat("fromUnix", fromUnix)
  963.         end if
  964.     on error errMsg number n
  965.         log "==> convert to hex string failed. " & errMsg & " with number " & n
  966.         set fromUnix to ""
  967.     end try
  968.     return fromUnix
  969. end convertToHTML
  970.  
  971. -- ------------------------------------------------------
  972. on typeText(theData)
  973.     (*
  974.          Unix-like systems      LF      0A      \n
  975.             (Linux, macOS)
  976.                Microsoft Windows    CRLF    0D 0A   \r\n
  977.                classic Mac OS       CR      0D          \r   Applescript return
  978.          *)
  979.     global debug
  980.     set lf to character id 10
  981.     log "in typeText"
  982.     printHeader("the input  ( theData )", theData)
  983.     -- Example: -- https://discussions.apple.com/docs/DOC-8841
  984.     -- locate links
  985.    
  986.     set theOutputBuffer to adjustURLs(theData)
  987.    
  988.     -- add paragraphs
  989.     set theOutputBuffer to addParagraphs(theOutputBuffer)
  990.    
  991.     log "theOutputBuffer is " & return & theOutputBuffer
  992.    
  993.     return theOutputBuffer
  994. end typeText
  995.  
  996.  
  997.  
  998. (*
  999. https://www.oreilly.com/library/view/applescript-the-definitive/0596102119/re89.html
  1000.  
  1001. https://stackoverflow.com/questions/11085654/apple-script-how-can-i-copy-html-content-to-the-clipboard
  1002.  
  1003. -- user has copied a file's icon in the Finder
  1004. clipboard info
  1005. -- {{string, 20}, {«class ut16», 44}, {«class hfs », 80}, {«class
  1006.  utf8», 20}, {Unicode text, 42}, {picture, 2616}, {«class icns», 43336},
  1007. {«class furl», 62}}
  1008.  
  1009. textutil -convert html foo.rtf
  1010.  
  1011. if ((clipboard info) as string) contains "«class furl»" then
  1012.         log "the clipboard contains a file named " & (the clipboard as string)
  1013.     else
  1014.         log "the clipboard does not contain a file"
  1015.     end if
  1016.    
  1017. the clipboard       required
  1018. as  class   optional
  1019.  
  1020. tell application "Script Editor"
  1021.         activate
  1022.     end tell
  1023.    
  1024. textutil has a simplistic text to html conversion
  1025.     set clipboardDataQuoted to quoted form of theData
  1026.     log "quoted form is " & clipboardDataQuoted
  1027.    
  1028.     set toUnix to "/bin/echo -n " & clipboardDataQuoted
  1029.     set toUnix to toUnix & " | textutil -convert html -noload -nostore -stdin -stdout "
  1030.     log "toUnix is " & toUnix
  1031.     set fromUnix to do shell script toUnix
  1032.     log "fromUnix  is " & fromUnix
  1033.    
  1034.    
  1035. set s to "Today is my birthday"
  1036. log text 1 thru ((offset of "my" in s) - 1) of s
  1037. --> "Today is "
  1038.             -- text 1 thru ((offset of "my" in s) - 1) of s
  1039.             -- -1 since offset return the first character "m" position count
  1040.            
  1041. log "beginningOfTag is " & text 1 thru (minimumPositiveNumber from {200, length of beginningOfTag}) of beginningOfTag & "<+++++++++++++++++++++++"
  1042.  
  1043. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html
  1044.  
  1045. *)
  1046.  
  1047. --mac $ hex=`echo -n "<p>your html code here</>" | hexdump -ve '1/1 "%.2x"'`
  1048. --mac $ echo $hex
  1049. --3c703e796f75722068746d6c20636f646520686572653c2f3e
  1050. --mac $ osascript -e "set the clipboard to «data HTML${hex}»"
  1051. --mac $
  1052. (*  
  1053. A sub-routine for encoding ASCII characters.  
  1054.  
  1055. encode_char("$")  
  1056. --> returns: "%24"  
  1057.  
  1058. based on:  
  1059. https://www.macosxautomation.com/applescript/sbrt/sbrt-08.html  
  1060.  
  1061. *)
  1062. (*
  1063. Lowest Numeric Value in a List
  1064.  
  1065. 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:
  1066.  
  1067. lowest_number({-3.25, 23, 2345, "sid", 3, 67})
  1068. --> returns: -3.25
  1069. lowest_number({-3.25, 23, {-22, 78695, "bob"}, 2345, true, "sid", 3, 67})
  1070. --> returns: -22
  1071.  
  1072. If there is no numeric data in the passed list, the sub-routine will return a null string ("")
  1073.  
  1074. lowest_number({"this", "list", "contains", "only", "text"})
  1075. --> returns: ""
  1076.  
  1077. https://macosxautomation.com/applescript/sbrt/sbrt-03.html
  1078.  
  1079. Here's the sub-routine:
  1080.  
  1081. *)
  1082. (*
  1083. on lowestNumber(values_list)
  1084.     set the low_amount to ""
  1085.     repeat with i from 1 to the count of the values_list
  1086.         set this_item to item i of the values_list
  1087.         set the item_class to the class of this_item
  1088.         if the item_class is in {integer, real} then
  1089.             if the low_amount is "" then
  1090.                 set the low_amount to this_item
  1091.             else if this_item is less than the low_amount then
  1092.                 set the low_amount to item i of the values_list
  1093.             end if
  1094.         else if the item_class is list then
  1095.             set the low_value to lowest_number(this_item)
  1096.             if the the low_value is less than the low_amount then ¬
  1097.                 set the low_amount to the low_value
  1098.         end if
  1099.     end repeat
  1100.     return the low_amount
  1101. end lowestNumber
  1102.  
  1103. https://lists.apple.com/archives/applescript-users/2010/Sep/msg00139.html
  1104. set list_of_values to {10, 20, 30, 40, 50, 60, 2000, 9, 3000, 4}
  1105.  
  1106. set minimum to 9.9999999999E+12
  1107. set maximum to 0
  1108. repeat with ref_to_value in list_of_values
  1109.     set the_value to contents of ref_to_value
  1110.     if the_value > maximum then set maximum to the_value
  1111.     if the_value < minimum then set minimum to the_value
  1112. end repeat
  1113.  
  1114. {minimum, maximum}
  1115.  
  1116. may do the trick.
  1117.  
  1118. Yvan KOENIG (VALLAURIS, France) lundi 13 septembre 2010 22:32:41
  1119. *)
  1120. (* https://lists.apple.com/archives/applescript-users/2010/Sep/msg00139.html
  1121. set list_of_values to {10, 20, 30, 40, 50, 60, 2000, 9, 3000, 4}
  1122.  
  1123. set minimum to 9.9999999999E+12
  1124.  
  1125. assume it's limited to positive values
  1126.  
  1127.  
  1128. on maxValue(list_of_values)
  1129.     global debug
  1130.     if debug ≥ 3 then log "in maxValue " & return & list_of_values
  1131.     set maximum to 0
  1132.     repeat with ref_to_value in list_of_values
  1133.         set the_value to contents of ref_to_value
  1134.         if the_value > maximum then set maximum to the_value
  1135.     end repeat
  1136.     if debug ≥ 3 then log maximum
  1137.     return maximum
  1138. end maxValue
  1139. *)
  1140. -- ------------------------------------------------------
  1141. (*
  1142. http://harvey.nu/applescript_url_encode_routine.html
  1143.  
  1144. on urlencode(theText)
  1145.     set theTextEnc to ""
  1146.     repeat with eachChar in characters of theText
  1147.         set useChar to eachChar
  1148.         set eachCharNum to ASCII number of eachChar
  1149.         if eachCharNum = 32 then
  1150.             set useChar to "+"
  1151.         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
  1152.             set firstDig to round (eachCharNum / 16) rounding down
  1153.             set secondDig to eachCharNum mod 16
  1154.             if firstDig > 9 then
  1155.                 set aNum to firstDig + 55
  1156.                 set firstDig to ASCII character aNum
  1157.             end if
  1158.             if secondDig > 9 then
  1159.                 set aNum to secondDig + 55
  1160.                 set secondDig to ASCII character aNum
  1161.             end if
  1162.             set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
  1163.             set useChar to numHex
  1164.         end if
  1165.         set theTextEnc to theTextEnc & useChar as string
  1166.     end repeat
  1167.     return theTextEnc
  1168. end urlencode
  1169.  
  1170. Clipboard classes after a copy from the application.
  1171. from waterfox
  1172. (*«class HTML», 13876, «class utf8», 505, «class ut16», 1012, string, 505, Unicode text, 1010*)
  1173.  
  1174. from chrome
  1175. (*«class HTML», 748, «class utf8», 204, «class ut16», 410, string, 204, Unicode text, 408*)
  1176.  
  1177. from safari
  1178. (*«class weba», 120785, «class RTF », 70255, «class HTML», 122811, «class utf8», 3370, «class ut16», 6772, uniform styles, 47132, string, 3385, scrap styles, 8122, Unicode text, 6732, uniform styles, 47132, scrap styles, 8122*)
  1179.  
  1180. iCab
  1181. (*«class weba», 1665, «class RTF », 763, «class utf8», 121, «class ut16», 244, uniform styles, 376, string, 121, scrap styles, 62, Unicode text, 242, uniform styles, 376, scrap styles, 62*)
  1182.  
  1183. Opera
  1184. (*«class HTML», 5767, «class utf8», 150, «class ut16», 302, string, 150, Unicode text, 300*)
  1185.  
  1186. Textedit
  1187. (*«class RTF », 1136, «class utf8», 138, «class ut16», 278, uniform styles, 148, string, 138, scrap styles, 22, Unicode text, 276, uniform styles, 148, scrap styles, 22*)
  1188.  
  1189. Word
  1190. (*«class DSIG», 4, «class DOBJ», 56, «class OBJD», 244, «class RTF », 30573, «class HTML», 21160, scrap styles, 22, uniform styles, 136, string, 210, Unicode text, 420, «class PDF », 13197, picture, 154058, «class EMBS», 33280, «class LNKS», 909, «class LKSD», 244, «class OJLK», 93, «class HLNK», 1387, «class OFSC», 232, «class ut16», 422, «class DSIG», 4, «class DOBJ», 56, «class OBJD», 244, scrap styles, 22, uniform styles, 136, «class EMBS», 33280, «class LNKS», 909, «class LKSD», 244, «class OJLK», 93, «class HLNK», 1387, «class OFSC», 232*)
  1191.  
  1192. TextWrangler
  1193. (*«class utf8», 185, «class BBLM», 4, «class ut16», 372, string, 185, Unicode text, 370, «class BBLM», 4*)
  1194.  
  1195. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement