J'ai créé un fichier AppleScript (.scpt) intitulé " Tapez le presse-papiers en tant que texte brut d'une seule ligne ." Le script est déclenché par un raccourci clavier défini par FastScripts.
Comportement souhaité :
Je veux que ce script prenne le contenu du presse-papiers, supprime toute mise en forme, puis supprime tout saut de ligne ou tabulation de ce texte. Enfin, je veux que le script tape le nouveau texte. Je veux préserver -- et non écraser -- le contenu original du presse-papiers.
Le problème spécifique :
L'erreur spécifique est que mon script ne parvient pas à supprimer tout le formatage de certains textes riches.
Je ne peux pas inclure l'intégralité du contenu en texte enrichi dans un message Stack Exchange. Par conséquent, pour être témoin de mon problème exact, veuillez télécharger ce fichier .rtf via Dropbox . Ouvrez ce fichier dans TextEdit.app. Surlignez la phrase et copiez-la dans le presse-papiers. Ensuite, déclenchez mon script alors que votre curseur se trouve dans un formulaire qui prend en charge et affiche du texte riche (afin que vous puissiez voir que le script va taper du texte riche).
Vous remarquerez que la phrase tapée est un contenu de texte riche et contient toujours des éléments de mise en forme. Ces éléments comprennent la police de texte originale (Helvetica) et la taille de police originale (12). Ces éléments auraient dû être supprimés. Ainsi, soit mon code est négligé, soit j'ai trouvé un véritable bug dans AppleScript lui-même. Je suppose que c'est la deuxième hypothèse.
Le code le plus court nécessaire pour reproduire l'erreur :
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
-- Back up clipboard contents:
set savedClipboard to my fetchStorableClipboard()
(*
Converting the clipboard text to plain text to remove any formatting:
From: http://lifehacker.com/127683/clear-text-formatting-on-os-x
*)
set theClipboardTextWithoutAnyFormatting to (the clipboard as text)
(*
Removing line breaks and indentations in clipboard text:
From: http://stackoverflow.com/a/12546965
*)
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set theClipboardTextWithoutAnyFormatting to text items of (theClipboardTextWithoutAnyFormatting as text)
set AppleScript's text item delimiters to {" "}
set theClipboardTextWithoutAnyLineBreaksOrFormatting to theClipboardTextWithoutAnyFormatting as text
set the clipboard to theClipboardTextWithoutAnyLineBreaksOrFormatting
tell application "System Events" to keystroke "v" using {command down}
delay 0.1 -- Without this delay, may restore clipboard before pasting.
-- Restore the original 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:
Tout d'abord, quelqu'un peut-il confirmer que le problème mentionné se produit sur son ordinateur ?
Si oui, comment puis-je supprimer tout le formatage du texte riche dans AppleScript, en tenant compte de ce bogue que j'ai découvert ?