Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- This applescript converts clipboard input into a format suited for pasting into an ASC
- reply. I observed that my copies into an ASC reply were not formated that well.
- I observed that copies from a web browser were formated much better. I went about
- adjusting the clipboard copy to the format expected by a web browser for best results.
- This applescript accepts the clipboard in either
- -- 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.
- -- HTML source code identified by text containing HTML markup.
- Caveat emptor.
- to use:
- 1) copy command + c what data you want to convert
- 2) run this applascript by double clicking on the app.
- 3) paste command + V into an ASC reply
- 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.
- Save as an Application Bundle. Don't check any of the boxes.
- Should you experience a problem, run in the Script Editor.
- Shows how to debug via on run path. Shows items added to folder. Shows log statement.
- 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.
- For testing, run in the Script Editor.
- 1) Click on the Event Log tab to see the output from the log statement
- 2) Click on Run
- change log
- may 1, 2019 -- skip 403 forbidding title
- may 2, 2019 -- convert \" to ". the \" mysteriously appears in HTML source code input. Probably some TextEdit artifact.
- copy to TextEdit copy out of TextEdit.
- enhancements:
- -- get pdf title
- Author: rccharles
- Copyright 2019 rccharles
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- example text document:
- set the clipboard to "\"Effective defenses 111 threats\" by John Galt
- https://discussions.apple.com/docs/DOC-8841
- \"Avoid phishing emails 222 and other scams\"
- https://support.apple.com/en-ca/HT204759
- blank lines
- also,see:http://www.google.com/ seeing again:http://www.google.com"
- *)
- -- Gets invoked here when you run in AppleScript editor or double click on the app icon.
- on run
- global debug
- set debug to 2
- set theList to clipboard info
- printClipboardInfo(theList)
- try
- set clipboardData to (the clipboard as text)
- if debug ≥ 2 then
- log "class clipboardData is " & class of clipboardData
- log "calling printHeader."
- end if
- printHeader("clipboardData", clipboardData)
- on error errStr number errorNumber
- log "===> We didn't find data on the clipboard. errStr is " & errStr & " errorNumber is " & errorNumber
- 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
- return 1
- end try
- set returnedData to common(clipboardData)
- postToCLipboard(returnedData)
- -- return code
- return 0
- end run
- -- Folder actions.
- -- Gets invoked here when something is dropped on the folder that this script is monitoring.
- -- Right click on the folder to be monitored. services > Folder Action Settup...
- on adding folder items to this_folder after receiving added_items
- -- TBD
- end adding folder items to
- -- Gets invoked here when something is dropped on this AppleScript icon
- on open dropped_items
- global debug
- set debug to 2
- (*
- -- Debug code.
- set fileName to choose file with prompt "get file"
- set dropped_items to {fileName}
- *)
- log "class of dropped_items is " & class of dropped_items
- display dialog "You dropped " & (count of dropped_items) & " item or items." & return & " Caveat emptor. You have been warned." giving up after 6
- set totalFileData to ""
- repeat with droppedItem in dropped_items
- log "The droppedItem is "
- -- display dialog "processing file " & (droppedItem as string) giving up after 3
- log droppedItem
- log "class = " & class of droppedItem
- set extIs to findExtension(droppedItem)
- set extIsU to makeCaseUpper(extIs)
- if extIsU is "HTML" or extIsU is "HTM" or extIsU is "TEXT" or extIsU is "TXT" then
- try
- set theFile to droppedItem as string
- set theFile to open for access file theFile
- set allOfFile to read theFile
- close access theFile
- printHeader("read from file ( allOfFile )", allOfFile)
- set totalFileData to totalFileData & common(allOfFile)
- on error theErrorMessage number theErrorNumber
- log theErrorMessage & "error number " & theErrorNumber
- close access theFile
- end try
- else
- -- we do not support this extension
- 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
- end if
- end repeat
- postToCLipboard(totalFileData)
- -- return code
- return 0
- end open
- -- ------------------------------------------------------
- on common(clipboardData)
- global debug
- set lf to character id 10
- -- Write a message into the event log.
- log " --- Starting on " & ((current date) as string) & " --- "
- -- don't let Windoze confuse us. convert Return LineFeed to lf
- set clipboardData to alterString(clipboardData, return & lf, lf)
- -- might as will convert classic macOS return to lf. We will have to look for so many things.
- set clipboardData to alterString(clipboardData, return, lf)
- -- figure out what type of data we have: plan text or html text.
- set paraCount to count of textToList(clipboardData, "<p")
- set endparaCount to count of textToList(clipboardData, "</p>")
- set titleCount to count of textToList(clipboardData, "<title")
- set endTitleCount to count of textToList(clipboardData, "</title>")
- set aLinkCount to count of textToList(clipboardData, "href=\"http")
- -- mangled href="http
- set mangledLinkCount to count of textToList(clipboardData, "href=\\\"http")
- set brCount to count of textToList(clipboardData, "<br>")
- if debug ≥ 1 then
- log "Values used to distinguis HTML source code from plan text."
- log "paraCount is " & paraCount
- log "endparaCount is " & endparaCount
- log "titleCount is " & titleCount
- log "endTitleCount is " & endTitleCount
- log "aLinkCount is " & aLinkCount
- log "brCount is " & brCount
- log "mangledLinkCount is " & mangledLinkCount
- end if
- --set endHttpCount to count of textToList(clipboardData, "http://")
- --set endHttpsCount to count of textToList(clipboardData, "https://")
- -- note, textToList returns a count one greater than the actual because item one is the data before the first found entry.
- if paraCount ≥ 4 and endparaCount ≥ 3 or brCount ≥ 4 or ((titleCount is endTitleCount) and titleCount ≥ 2) or aLinkCount ≥ 3 or mangledLinkCount ≥ 3 then
- log "found HTML input"
- -- strange \" are appearing in input text. Probably the result of using TextEdit along the way.
- -- quick hack.
- set alteredClipboardData to alterString(clipboardData, "\\\"", "\"")
- set readyData to typeHTML(alteredClipboardData)
- else
- log "found Text input"
- set readyData to typeText(clipboardData)
- end if
- return readyData
- end common
- -- ------------------------------------------------------
- (*
- alterString
- thisText is the input string to change
- delim is what string to change. It doesn't have to be a single character.
- replacement is the new string
- returns the changed string.
- *)
- on alterString(thisText, delim, replacement)
- set resultList to {}
- set {tid, my text item delimiters} to {my text item delimiters, delim}
- try
- set resultList to every text item of thisText
- set text item delimiters to replacement
- set resultString to resultList as string
- set my text item delimiters to tid
- on error
- set my text item delimiters to tid
- end try
- return resultString
- end alterString
- -- ------------------------------------------------------
- (*
- Return the text to the right of theToken.
- *)
- on answerAndChomp(theString, theToken)
- set debugging to false
- set theOffset to offset of theToken in theString
- if debugging then log "theOffset is " & theOffset
- set theLength to length of theString
- if theOffset > 0 then
- set beginningPart to text 1 thru (theOffset - 1) of theString
- if debugging then log "beginningPart is " & beginningPart
- set chompped to text theOffset thru theLength of theString
- if debugging then log "chompped is " & chompped
- return {chompped, beginningPart}
- else
- set beginningPart to ""
- return {theString, beginningPart}
- end if
- end answerAndChomp
- -- ------------------------------------------------------
- (*
- Delete the leading part of the string until and including theToken.
- *)
- on chompLeftAndTag(theString, theToken)
- set debugging to false
- --log text 1 thru ((offset of "my" in s) - 1) of s
- --set rightString to offset of theToken in theString thru count of theString of theString
- set theOffset to offset of theToken in theString
- if debugging then log "theOffset is " & theOffset
- set theLength to length of theString
- if debugging then log "theLength is " & theLength
- if theOffset > 0 then
- set chompped to text (theOffset + (length of theToken)) thru theLength of theString
- if debugging then log "chompped is " & chompped
- return chompped
- else
- return ""
- end if
- end chompLeftAndTag
- -- ------------------------------------------------------
- (*
- Yvan Koenig
- https://macscripter.net/viewtopic.php?id=43133
- *)
- on findExtension(inputFileName)
- set fileName to inputFileName as string
- set saveTID to AppleScript's text item delimiters
- set AppleScript's text item delimiters to {"."}
- set theExt to last text item of fileName
- set AppleScript's text item delimiters to saveTID
- --log "theExt is " & theExt
- if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
- --log "theExt is " & theExt
- return theExt
- end findExtension
- -- ------------------------------------------------------
- (*
- http://krypted.com/mac-os-x/to-hex-and-back/
- *)
- on hexToString(hex)
- log "in hexToString"
- log "hex string is " & hex
- set toUnix to "echo " & hex & " | xxd -r -p "
- log "toUnix is " & toUnix
- try
- set fromUnix to do shell script toUnix
- log "fromUnix is " & fromUnix
- on error errMsg number n
- log "convert hex string to string failed. " & errMsg & " with number " & n
- end try
- end hexToString
- -- ------------------------------------------------------
- (*
- https://stackoverflow.com/questions/55838252/minimum-value-that-not-zero
- set m to get minimumPositiveNumber from {10, 2, 0, 2, 4}
- log "m is " & m
- set m to minimumPositiveNumber from {0, 0, 0}
- log "m is " & m
- *)
- on minimumPositiveNumber from L
- local L
- if L = {} then return null
- set |ξ| to 0
- repeat with x in L
- set x to x's contents
- if (x < |ξ| and x ≠ 0) ¬
- or |ξ| = 0 then ¬
- set |ξ| to x
- end repeat
- |ξ|
- end minimumPositiveNumber
- -- ------------------------------------------------------
- (*
- makeCaseUpper("Now is the time, perhaps, for all good men")
- *)
- on makeCaseUpper(theString)
- set UC to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- set LC to "abcdefghijklmnopqrstuvwxyz"
- set C to characters of theString
- repeat with ch in C
- if ch is in LC then set contents of ch to item (offset of ch in LC) of UC
- end repeat
- return C as string
- end makeCaseUpper
- -- ------------------------------------------------------
- on postToCLipboard(pleasePost)
- try
- -- osascript -e "set the clipboard to «data HTML${hex}»"
- set toUnixSet to "osascript -e \"set the clipboard to «data HTML" & pleasePost & "»\""
- log "toUnixSet is " & printHeader("toUnixSet", toUnixSet)
- set fromUnixSet to do shell script toUnixSet
- log "fromUnixSet is " & fromUnixSet
- on error errMsg number n
- log "==> We tried to send back HTML data, but failed. " & errMsg & " with number " & n
- end try
- -- see what ended up on the clipboard
- set theList2 to clipboard info
- printClipboardInfo(theList2)
- end postToCLipboard
- -- ------------------------------------------------------
- on printClipboardInfo(theList)
- log (clipboard info)
- log class of theList
- log "Data types on the clipboard ... "
- printList("theList", theList)
- log "... "
- end printClipboardInfo
- -- ------------------------------------------------------
- (* Pump out the beginning of theString *)
- on printHeader(theName, theString)
- global debug
- if debug ≥ 3 then
- log "in printHeader"
- log theString
- log length of theString
- end if
- if length of theString ≤ 0 then
- log "==> no string to print"
- else
- log theName & " is " & text 1 thru (minimumPositiveNumber from {400, length of theString}) of theString & "<+++++++++"
- end if
- end printHeader
- -- ------------------------------------------------------
- (*
- print out the items in a list
- *)
- on printList(theName, splits)
- set theCount to 1
- repeat with theEntry in splits
- log "------- " & theName & theCount & " is " & return & theEntry
- set theCount to theCount + 1
- end repeat
- end printList
- -- ------------------------------------------------------
- (*
- splitTextToList seems to be what you are trying to do
- thisText is the input string
- delim is what to split on
- results returned in a list
- Total hack. We know splitTextToList strips of delim so add it back.
- *)
- on splitTextToList(thisText, delim)
- set returnedList to textToList(thisText, delim)
- set resultArray to {}
- copy item 1 of returnedList to the end of the resultArray
- repeat with i from 2 to (count of returnedList) in returnedList
- set newElement to delim & item i of returnedList
- copy newElement to the end of the resultArray
- end repeat
- return resultArray
- end splitTextToList
- -- ------------------------------------------------------
- (*
- Retrieved data between "begin" and "end" tag. Whatever is between the strings.
- *)
- on tagContent(theString, startTag, endTag)
- try
- log "in tabContent. " & " startTag is " & startTag & " endTag is " & endTag
- set beginningOfTag to chompLeftAndTag(theString, startTag)
- if length of beginningOfTag ≤ 0 then
- set middleText to ""
- else
- printHeader("beginningOfTag", beginningOfTag)
- set endingOffset to (offset of endTag in beginningOfTag)
- if endingOffset ≤ (length of endTag) then
- set middleText to ""
- else
- set middleText to text 1 thru (endingOffset - 1) of beginningOfTag
- log "middleText is " & printHeader("middleText", middleText)
- end if
- end if
- on error errMsg number n
- log "finding contained text failed. " & errMsg & " with number " & n
- set middleText to ""
- end try
- return middleText
- end tagContent
- (*
- textToList seems to be what you are trying to do
- thisText is the input string
- delim is what to split on
- returns a list of strings.
- - textToList was found here:
- - http://macscripter.net/viewtopic.php?id=15423
- *)
- on textToList(thisText, delim)
- set resultList to {}
- set {tid, my text item delimiters} to {my text item delimiters, delim}
- try
- set resultList to every text item of thisText
- set my text item delimiters to tid
- on error
- set my text item delimiters to tid
- end try
- return resultList
- end textToList
- -- ------------------------------------------------------
- on typeHTML(theData)
- global debug
- log "in typeHTML" & return & " Try to send back HTML."
- try
- set clipboardDataQuoted to quoted form of theData
- log "quoted form is " & printHeader("clipboardDataQuoted", clipboardDataQuoted)
- -- make hex string as required for HTML data on the clipboard
- set toUnix to "/bin/echo -n " & clipboardDataQuoted & " | hexdump -ve '1/1 \"%.2x\"'"
- log "toUnix is " & printHeader("toUnix", toUnix)
- set fromUnix to do shell script toUnix
- log "fromUnix is " & printHeader("fromUnix", fromUnix)
- if debug ≥ 2 then
- log "displaying original string --- so we can tell if it converted successfully. "
- hexToString(fromUnix)
- end if
- on error errMsg number n
- log "==> convert to hex string failed. " & errMsg & " with number " & n
- set fromUnix to ""
- end try
- return fromUnix
- end typeHTML
- -- ------------------------------------------------------
- on typeText(theData)
- (*
- Unix-like systems LF 0A \n
- (Linux, macOS)
- Microsoft Windows CRLF 0D 0A \r\n
- classic Mac OS CR 0D \r Applescript return
- *)
- global debug
- set lf to character id 10
- log "in typeText"
- printHeader("the input ( theData )", theData)
- -- Example: -- https://discussions.apple.com/docs/DOC-8841
- -- locate links
- set theOutputBuffer to theData
- set countOf to 1
- -- file is mostly for testing, but should be ok for production too.
- set linkId to {"https://", "http://"}
- repeat with lookForLink in linkId
- set splitOnHTTPorHTTPS to splitTextToList(theOutputBuffer, lookForLink)
- log "display splitOnHTTPorHTTPS.."
- -- debug info
- if debug ≥ 2 then
- repeat with theCurrentHTTPorHTTPS in splitOnHTTPorHTTPS
- printHeader("#" & countOf & " theCurrentHTTPorHTTPS ", theCurrentHTTPorHTTPS)
- set countOf to countOf + 1
- end repeat
- end if
- set buildHTML to beginning of splitOnHTTPorHTTPS
- log "buildHTML is " & buildHTML
- -- delete the first item text
- set splitOnHTTPorHTTPS to rest of splitOnHTTPorHTTPS
- log splitOnHTTPorHTTPS
- set counti to 1
- repeat with theCurrentHTTPorHTTPS in splitOnHTTPorHTTPS
- -- example: converted url. no title found
- -- <a href="https://discussions.apple.com/docs/DOC-8841" target="_blank">https://discussions.apple.com/docs/DOC-8841</a>
- if debug ≥ 1 then
- set toUnix to "/bin/echo -n " & quoted form of theCurrentHTTPorHTTPS & " | hexdump -C"
- set fromUnix to do shell script toUnix
- log "fromUnix is " & return & fromUnix
- end if
- -- find the end of the HTML URL by splitting on blank or return
- -- unsafe characters includes the blank/empty space and " < > # % { } | \ ^ ~ [ ] `
- -- https://perishablepress.com/stop-using-unsafe-characters-in-urls/
- -- the end of the clipboard string my end after the url, hence no " ", LF or CR
- -- Rember, CRLF was converted to LF above
- set endsWhere to {}
- copy (offset of " " in theCurrentHTTPorHTTPS) to the end of the endsWhere
- copy (offset of lf in theCurrentHTTPorHTTPS) to the end of the endsWhere
- log endsWhere
- set endOfURL to (minimumPositiveNumber from endsWhere) - 1
- if endOfURL = -1 then
- -- We have reached the end of the input
- set theURL to theCurrentHTTPorHTTPS
- else
- set theURL to text 1 thru endOfURL of theCurrentHTTPorHTTPS
- end if
- log "--------------------------- " & theURL & "--------------------------- "
- -- "curl --silent --location --max-time 15 " & theURL
- set toUnix to "curl --silent --location --max-time 10 " & quoted form of theURL
- log "toUnix is " & toUnix
- try
- log "reading link file to get title"
- set fromUnix to do shell script toUnix
- log "fromUnix"
- printHeader("fromUnix", fromUnix)
- -- may not be working with an HTLM document, so thefound title may be to long or confused.
- log "how far?"
- set actualTagData to tagContent(fromUnix, "<title", "</title>")
- log "actualTagData is " & printHeader("actualTagData", actualTagData)
- if actualTagData is "" then
- set actualTagData to theURL
- else if length of actualTagData > 140 then
- log "length of actualTagData is " & length of actualTagData
- set actualTagData to theURL
- -- curl https://appleid.apple.com returns <title>403 Forbidden</title>
- else if actualTagData contains "403" and actualTagData contains "Forbidden" then
- set actualTagData to theURL
- else
- -- there could be some attributes within the tag
- -- an attribute could have a > in it. ignoring that for now.
- set actualTagData to text ((offset of ">" in actualTagData) + 1) thru (length of actualTagData) of actualTagData
- -- found line-end in title. caused confustion.
- set actualTagData to alterString(actualTagData, return & lf, " ")
- set actualTagData to alterString(actualTagData, return, " ")
- set actualTagData to alterString(actualTagData, lf, " ")
- end if
- on error errMsg number n
- log "==> Error occured when looking for title. " & errMsg & " with number " & n
- set actualTagData to theURL
- end try
- set assembled to "<a href=\"" & theURL & "\" target=\"_blank\">" & actualTagData & "</a>"
- log "assembled is " & assembled
- if endOfURL = -1 then
- -- We have reached the end of the input
- set buildHTML to buildHTML & assembled
- else
- set buildHTML to buildHTML & assembled & text from (endOfURL + 1) to (length of theCurrentHTTPorHTTPS) of theCurrentHTTPorHTTPS
- end if
- -- wrap up
- set theOutputBuffer to buildHTML
- log "transformed text from buildHTML is " & return & buildHTML
- log "#" & counti & " transformed text from buildHTML is " & return & buildHTML
- -- number of links found
- set counti to counti + 1
- end repeat -- looking for all links of the same type in document
- end repeat -- scanning for https and http links
- (* add paragraphs *)
- -- start the theOutputBuffer with a paragraph tag. We are taking a simple approach at this time.
- set theOutputBuffer to "<p>" & theOutputBuffer
- -- LF
- -- Remember CRLF was changed to LF above and CR was chanaged to LF above.
- -- we don't want no Windoze problems
- set theOutputBuffer to alterString(theOutputBuffer, lf & lf, "</p><p> </p><p>")
- -- Does the string end with a dangling paragraph?
- if debug ≥ 3 then
- log "length of theOutputBuffer is " & length of theOutputBuffer
- log "((length of theOutputBuffer) - 2) is " & ((length of theOutputBuffer) - 2)
- log "(length of theOutputBuffer) is " & (length of theOutputBuffer)
- log "((length of theOutputBuffer) - 3) is " & ((length of theOutputBuffer) - 3)
- end if
- if text ((length of theOutputBuffer) - 2) thru (length of theOutputBuffer) of theOutputBuffer is "<p>" then
- set theOutputBuffer to text 1 thru ((length of theOutputBuffer) - 3) of theOutputBuffer
- else if text ((length of theOutputBuffer) - 2) thru (length of theOutputBuffer) of theOutputBuffer is not "</p>" then
- set theOutputBuffer to theOutputBuffer & "</p>"
- end if
- log "theOutputBuffer is " & return & theOutputBuffer
- --convert to html clipboard format
- return typeHTML(theOutputBuffer)
- end typeText
- (*
- https://www.oreilly.com/library/view/applescript-the-definitive/0596102119/re89.html
- https://stackoverflow.com/questions/11085654/apple-script-how-can-i-copy-html-content-to-the-clipboard
- -- user has copied a file's icon in the Finder
- clipboard info
- -- {{string, 20}, {«class ut16», 44}, {«class hfs », 80}, {«class
- utf8», 20}, {Unicode text, 42}, {picture, 2616}, {«class icns», 43336},
- {«class furl», 62}}
- textutil -convert html foo.rtf
- if ((clipboard info) as string) contains "«class furl»" then
- log "the clipboard contains a file named " & (the clipboard as string)
- else
- log "the clipboard does not contain a file"
- end if
- the clipboard required
- as class optional
- tell application "Script Editor"
- activate
- end tell
- textutil has a simplistic text to html conversion
- set clipboardDataQuoted to quoted form of theData
- log "quoted form is " & clipboardDataQuoted
- set toUnix to "/bin/echo -n " & clipboardDataQuoted
- set toUnix to toUnix & " | textutil -convert html -noload -nostore -stdin -stdout "
- log "toUnix is " & toUnix
- set fromUnix to do shell script toUnix
- log "fromUnix is " & fromUnix
- set s to "Today is my birthday"
- log text 1 thru ((offset of "my" in s) - 1) of s
- --> "Today is "
- -- text 1 thru ((offset of "my" in s) - 1) of s
- -- -1 since offset return the first character "m" position count
- log "beginningOfTag is " & text 1 thru (minimumPositiveNumber from {200, length of beginningOfTag}) of beginningOfTag & "<+++++++++++++++++++++++"
- https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html
- *)
- --mac $ hex=`echo -n "<p>your html code here</>" | hexdump -ve '1/1 "%.2x"'`
- --mac $ echo $hex
- --3c703e796f75722068746d6c20636f646520686572653c2f3e
- --mac $ osascript -e "set the clipboard to «data HTML${hex}»"
- --mac $
- (*
- A sub-routine for encoding ASCII characters.
- encode_char("$")
- --> returns: "%24"
- based on:
- https://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
- *)
- (*
- Lowest Numeric Value in a List
- 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:
- lowest_number({-3.25, 23, 2345, "sid", 3, 67})
- --> returns: -3.25
- lowest_number({-3.25, 23, {-22, 78695, "bob"}, 2345, true, "sid", 3, 67})
- --> returns: -22
- If there is no numeric data in the passed list, the sub-routine will return a null string ("")
- lowest_number({"this", "list", "contains", "only", "text"})
- --> returns: ""
- https://macosxautomation.com/applescript/sbrt/sbrt-03.html
- Here's the sub-routine:
- *)
- (*
- on lowestNumber(values_list)
- set the low_amount to ""
- repeat with i from 1 to the count of the values_list
- set this_item to item i of the values_list
- set the item_class to the class of this_item
- if the item_class is in {integer, real} then
- if the low_amount is "" then
- set the low_amount to this_item
- else if this_item is less than the low_amount then
- set the low_amount to item i of the values_list
- end if
- else if the item_class is list then
- set the low_value to lowest_number(this_item)
- if the the low_value is less than the low_amount then ¬
- set the low_amount to the low_value
- end if
- end repeat
- return the low_amount
- end lowestNumber
- https://lists.apple.com/archives/applescript-users/2010/Sep/msg00139.html
- set list_of_values to {10, 20, 30, 40, 50, 60, 2000, 9, 3000, 4}
- set minimum to 9.9999999999E+12
- set maximum to 0
- repeat with ref_to_value in list_of_values
- set the_value to contents of ref_to_value
- if the_value > maximum then set maximum to the_value
- if the_value < minimum then set minimum to the_value
- end repeat
- {minimum, maximum}
- may do the trick.
- Yvan KOENIG (VALLAURIS, France) lundi 13 septembre 2010 22:32:41
- *)
- (* https://lists.apple.com/archives/applescript-users/2010/Sep/msg00139.html
- set list_of_values to {10, 20, 30, 40, 50, 60, 2000, 9, 3000, 4}
- set minimum to 9.9999999999E+12
- assume it's limited to positive values
- on maxValue(list_of_values)
- global debug
- if debug ≥ 3 then log "in maxValue " & return & list_of_values
- set maximum to 0
- repeat with ref_to_value in list_of_values
- set the_value to contents of ref_to_value
- if the_value > maximum then set maximum to the_value
- end repeat
- if debug ≥ 3 then log maximum
- return maximum
- end maxValue
- *)
- -- ------------------------------------------------------
- (*
- http://harvey.nu/applescript_url_encode_routine.html
- on urlencode(theText)
- set theTextEnc to ""
- repeat with eachChar in characters of theText
- set useChar to eachChar
- set eachCharNum to ASCII number of eachChar
- if eachCharNum = 32 then
- set useChar to "+"
- 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
- set firstDig to round (eachCharNum / 16) rounding down
- set secondDig to eachCharNum mod 16
- if firstDig > 9 then
- set aNum to firstDig + 55
- set firstDig to ASCII character aNum
- end if
- if secondDig > 9 then
- set aNum to secondDig + 55
- set secondDig to ASCII character aNum
- end if
- set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
- set useChar to numHex
- end if
- set theTextEnc to theTextEnc & useChar as string
- end repeat
- return theTextEnc
- end urlencode
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement