Sto scrivendo un file .scpt di AppleScript, attivato a livello di sistema dalla combinazione di tasti assegnata in FastScripts.app, che aggiunge parentesi attorno al testo modificabile selezionato.
Se il testo selezionato è già racchiuso tra parentesi, desidero che il mio script elimini efficacemente le parentesi dalla selezione. Questo è dove ho bisogno di assistenza. Non voglio rimuovere alcuna formattazione dal testo formattato.
Il mio script funziona se la selezione con parentesi è di solo testo, ma non se si tratta di dati RTF o HTML.
Ecco il mio codice completo:
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: