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
- adjust a 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 adding paragraphs and links.
- -- HTML which which is marked at HTML. The detection of HTML is done by finding four less thans < and four greater thans >.
- Caveat emptor.
- to use:
- 1) copy command + c what data you want to convert
- 2) run this applascript
- 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 anything.
- Debugging...
- 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
- 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.
- *)
- -- Gets invoked here when you run in AppleScript editor.
- on run
- global debug
- set debug to 2
- (*
- 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"
- *)
- set theList to clipboard info
- printClipboardInfo(theList)
- try
- set clipboardData to (the clipboard as text)
- if debug ≥ 3 then
- log "class clipboardData is " & class of clipboardData
- log "calling displayHeader."
- end if
- displayHeader("clipboardData", clipboardData)
- on error errStr number errorNumber
- log "===> We didn't find data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- common(clipboardData)
- 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
- --common(added_items)
- 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
- log "class of dropped_items is " & class of dropped_items
- set totalFileData to ""
- repeat with droppedItem in dropped_items
- log "The droppedItem is "
- log droppedItem
- log "class = " & class of droppedItem
- try
- set theFile to droppedItem as string
- set theFile to open for access file theFile
- set allOfFile to read theFile
- close access theFile
- on error theErrorMessage number theErrorNumber
- log theErrorMessage & "error number " & theErrorNumber
- close access theFile
- end try
- displayHeader("read from file ( allOfFile )", allOfFile)
- set totalFileData to totalFileData & allOfFile
- end repeat
- common(totalFileData)
- end open
- -- ------------------------------------------------------
- on common(clipboardData)
- global debug
- set lf to character id 10
- -- don't let Windoze confuse us. convert Return LineFeed to lf
- set clipboardData to alterString(clipboardData, return & lf, lf)
- -- Write a message into the event log.
- log " --- Starting on " & ((current date) as string) & " --- "
- -- figure out what type of data we have: plan text or html text.
- set paraCount to textToList(clipboardData, "<")
- set endparaCount to textToList(clipboardData, ">")
- if (count of paraCount) ≥ 4 and (count of endparaCount) ≥ 4 then
- log "found HTML input"
- typeHTML(clipboardData)
- else
- log "found Text input"
- typeText(clipboardData)
- end if
- -- return code
- return 0
- 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 theString
- end if
- end chompLeftAndTag
- -- ------------------------------------------------------
- (* Pump out the beginning of theString *)
- on displayHeader(theName, theString)
- global debug
- if debug ≥ 3 then
- log "in displayHeader"
- log theString
- log length of theString
- end if
- log theName & " is " & text 1 thru (minimumPositiveNumber from {200, length of theString}) of theString & "<+++++++++++"
- end displayHeader
- -- ------------------------------------------------------
- (*
- 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
- -- ------------------------------------------------------
- on printClipboardInfo(theList)
- log (clipboard info)
- log class of theList
- log "Data types on the clipboard ... "
- printList("theList", theList)
- log "... "
- end printClipboardInfo
- -- ------------------------------------------------------
- (*
- print out the items in a list
- *)
- on printList(name, splits)
- set theCount to 1
- repeat with theEntry in splits
- log "------- " & name & 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)
- log "in tabContent. " & " startTag is " & startTag & " endTag is " & endTag
- set beginningOfTag to chompLeftAndTag(theString, startTag)
- displayHeader("beginningOfTag", beginningOfTag)
- set middleText to text 1 thru ((offset of endTag in beginningOfTag) - 1) of beginningOfTag
- log "middleText is " & middleText
- 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)
- log "in typeHTML" & return & "Try to send back utf8."
- set clipboardDataQuoted to quoted form of theData
- log "quoted form is " & clipboardDataQuoted
- set toUnix to "/bin/echo -n " & clipboardDataQuoted & " | hexdump -ve '1/1 \"%.2x\"'"
- log "toUnix is " & toUnix
- try
- set fromUnix to do shell script toUnix
- log "fromUnix is " & fromUnix
- log "displaying original string ---"
- hexToString(fromUnix)
- on error errMsg number n
- log "convert to hex string failed. " & errMsg & " with number " & n
- end try
- try
- -- osascript -e "set the clipboard to «data HTML${hex}»"
- --set toUnixSet to "osascript -e \"set the clipboard to «data HTML" & fromUnix & "»"
- --set cbData to the clipboard as «class utf8»
- set toUnixSet to "osascript -e \"set the clipboard to «data HTML" & fromUnix & "»\""
- log "toUnixSet is " & 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
- set theList2 to clipboard info
- printClipboardInfo(theList2)
- end typeHTML
- -- ------------------------------------------------------
- on typeText(theData)
- (*
- Unix-like systems LF 0A \n
- (Linux, macOS)
- Microsoft Windows CR LF 0D 0A \r\n
- classic Mac OS CR 0D \r
- *)
- set lf to character id 10
- log "in typeText"
- displayHeader("the input ( theData )", theData)
- -- Example: -- https://discussions.apple.com/docs/DOC-8841
- -- locate links
- set theOutputBuffer to theData
- set linkId to {"https://", "http://"}
- repeat with lookForLink in linkId
- set splitOnHTTPS to splitTextToList(theOutputBuffer, lookForLink)
- log "display splitOnHTTPS..."
- set countOf to 1
- repeat with theCurrentHTTPS in splitOnHTTPS
- displayHeader("#" & countOf & " theCurrentHTTPS ", theCurrentHTTPS)
- set countOf to countOf + 1
- end repeat
- set buildHTML to beginning of splitOnHTTPS
- log "buildHTML is " & buildHTML
- -- delete the first item text
- set splitOnHTTPS to rest of splitOnHTTPS
- log splitOnHTTPS
- set counti to 1
- repeat with theCurrentHTTPS in splitOnHTTPS
- -- find the end of the HTML URL aby splitting on blank or return
- -- <a href="https://discussions.apple.com/docs/DOC-8841" target="_blank">https://discussions.apple.com/docs/DOC-8841</a>
- -- text 1 thru ((offset of "my" in s) - 1) of s
- -- -1 since offset return the first character "m" position count
- set toUnix to "/bin/echo -n " & quoted form of theCurrentHTTPS & " | hexdump -C"
- set fromUnix to do shell script toUnix
- log "fromUnix is " & return & fromUnix
- -- the end of the clipboard string my end after the url, hence no " ", LF or CRLF.
- -- ---- What about just return? ----
- set endsWhere to {}
- copy (offset of " " in theCurrentHTTPS) to the end of the endsWhere
- copy (offset of lf in theCurrentHTTPS) to the end of the endsWhere
- copy (offset of return in theCurrentHTTPS) to the end of the endsWhere
- -- in case two links are adjacent
- -- not sure what the standard might be, but I don't think there is a conflict
- -- confuses thing. http & https were still in search string.
- --copy (offset of "https://" in theCurrentHTTPS) to the end of the endsWhere
- --copy (offset of "http://" in theCurrentHTTPS) 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 theCurrentHTTPS
- end if
- set theURL to text 1 thru endOfURL of theCurrentHTTPS
- log "--------------------------- " & theURL & "--------------------------- "
- -- "curl --silent --location --max-time 15 " & theURL
- set toUnix to "curl --silent --location --max-time 15 " & theURL
- log "toUnix is " & toUnix
- set fromUnix to do shell script toUnix
- displayHeader("fromUnix", fromUnix)
- try
- set actualTagData to tagContent(fromUnix, "<title", "</title>")
- log "actualTagData is " & actualTagData
- if actualTagData is "" then
- set actualTagData to theURL
- end if
- -- 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
- 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 theCurrentHTTPS) of theCurrentHTTPS
- 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
- -- CRLF, CR or LF
- set theOutputBuffer to alterString(theOutputBuffer, return & lf & return & lf, "</p><p> </p><p>")
- set theOutputBuffer to alterString(theOutputBuffer, return & return, "</p><p> </p><p>")
- set theOutputBuffer to alterString(theOutputBuffer, lf & lf, "</p><p> </p><p>")
- -- Does the string end with a dangling paragraph?
- 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)
- 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
- 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 "
- 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
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement