I am writing an AppleScript .scpt file, triggered system-wide by key combination assigned in FastScripts.app, that adds parentheses around the selected editable text.
If the selected text happens to already be wrapped in parentheses, then I want my script to effectively delete the parentheses from the selection. This is where I need assistance. I do not want to strip any of the formatting from formatted text.
My script works if the selection-with-parentheses is plain text data, but not if it is RTF or HTML data.
Here is my full code:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
(*
Get the selected text into an AppleScript, while preserving the original clipboard:
From: http://apple.stackexchange.com/questions/271161/how-to-get-the-selected-text-into-an-applescript-without-copying-the-text-to-th/
*)
-- Back up the original clipboard contents:
set savedClipboard to my fetchStorableClipboard()
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theCount to thePasteboard's changeCount()
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
-- Check for changed clipboard:
repeat 20 times
if thePasteboard's changeCount() is not theCount then exit repeat
delay 0.1
end repeat
set firstCharacter to (character 1 of (the clipboard))
set lastCharacter to (character (length of (the clipboard))) of (the clipboard)
-- Remove the parentheses from the selection, if the selection is wrapped in parentheses:
if (firstCharacter is "(") and (lastCharacter is ")") then
-- The selection already has parentheses.
-- I must discern what class types are available for the clipboard content:
tell current application
set cbInfo to get (clipboard info) as string
if cbInfo contains "RTF" then
-- I need help here.
-- Remove the first and last characters of the rich text, while retaining the rich text formatting.
else if cbInfo contains "HTML" then
-- I need help here.
-- Remove the first and last characters of the HTML, while retaining formatting data.
else
-- The clipboard contains plain text.
-- Remove the first and last character of a plain text string:
set theSelectionWithoutParentheses to (text 2 thru -2 of (the clipboard))
set the clipboard to theSelectionWithoutParentheses
tell application "System Events" to keystroke "v" using {command down}
end if
end tell
else
-- The selection needs parentheses.
tell application "System Events" to keystroke "("
delay 0.1
tell application "System Events" to keystroke "v" using {command down}
delay 0.1
tell application "System Events" to keystroke ")"
end if
delay 1 -- Without this delay, may restore clipboard before pasting.
-- Restore clipboard:
my putOnClipboard:savedClipboard
on fetchStorableClipboard()
set aMutableArray to current application's NSMutableArray's array() -- used to store contents
-- get the pasteboard and then its pasteboard items
set thePasteboard to current application's NSPasteboard's generalPasteboard()
-- loop through pasteboard items
repeat with anItem in thePasteboard's pasteboardItems()
-- make a new pasteboard item to store existing item's stuff
set newPBItem to current application's NSPasteboardItem's alloc()'s init()
-- get the types of data stored on the pasteboard item
set theTypes to anItem's types()
-- for each type, get the corresponding data and store it all in the new pasteboard item
repeat with aType in theTypes
set theData to (anItem's dataForType:aType)'s mutableCopy()
if theData is not missing value then
(newPBItem's setData:theData forType:aType)
end if
end repeat
-- add new pasteboard item to array
(aMutableArray's addObject:newPBItem)
end repeat
return aMutableArray
end fetchStorableClipboard
on putOnClipboard:theArray
-- get pasteboard
set thePasteboard to current application's NSPasteboard's generalPasteboard()
-- clear it, then write new contents
thePasteboard's clearContents()
thePasteboard's writeObjects:theArray
end putOnClipboard:
do shell script "tr -d '()'"
work? I didn't test this in Applescript but from the terminal this worked on artf
file. – I0_ol May 16 '17 at 13:25do shell script "pbpaste | tr -d '()' | pbcopy"
and for HTML data:do shell script "osascript -e 'try' -e 'get the clipboard as «class HTML»' -e 'end try' | awk '{sub(/«data HTML/, \"3C68746D6C3E\") sub(/»/, \"3C2F68746D6C3E\")} {print}' | xxd -r -p | textutil -convert rtf -stdin -stdout | tr -d '()' | pbcopy"
. But, I couldn't figure out how to get it to work properly on RTF data. – rubik's sphere May 16 '17 at 22:48.rtf
file by copying an internet page and pasting it to a document in TextEdit. I opened Terminal and rangrep '(' txt.rtf
and it showed me several lines with parentheses. Then I ran grep'(' txt.rtf | tr -d '()'
and it stripped the parentheses from the output. I rancat txt.rtf | tr -d '()' | grep '('
just to be certain. So it seems to work in Terminal. – I0_ol May 16 '17 at 23:39set the clipboard to your_data
;set pre_shell to get the clipboard
;set post_shell to (do shell script "pbpaste | tr -d '()'")
– I0_ol May 17 '17 at 00:40display dialog class of post_shell
shows that it isctxt
. – rubik's sphere May 17 '17 at 01:06