Ich habe eine AppleScript-Datei (.scpt) mit dem Titel „ Type Clipboard As Single Line Plain Text “ erstellt. Das Skript wird durch eine von FastScripts festgelegte Tastenkombination ausgelöst.
Gewünschtes Verhalten:
Ich möchte, dass dieses Skript den Inhalt der Zwischenablage übernimmt, alle Formatierungen entfernt und dann alle Zeilenumbrüche oder Tabulatoren aus diesem Text entfernt. Schließlich möchte ich, dass das Skript den neuen Text eingibt. Ich möchte den ursprünglichen Inhalt der Zwischenablage beibehalten – nicht überschreiben.
Das konkrete Problem:
Der spezifische Fehler ist, dass mein Skript nicht alle Formatierungen aus Rich-Text entfernen kann.
Ich kann keinen vollständigen Rich-Text-Inhalt in einen Stack Exchange-Beitrag einfügen. Um mein genaues Problem zu sehen, laden Sie daher bitte diese .rtf-Datei über Dropbox herunter . Öffnen Sie diese Datei in TextEdit.app. Markieren Sie den Satz und kopieren Sie ihn in die Zwischenablage. Lösen Sie dann mein Skript aus, während sich Ihr Cursor in einem Formular befindet, das Rich-Text unterstützt und anzeigt (damit Sie sehen können, dass das Skript Rich-Text eingibt).
Sie werden feststellen, dass der eingegebene Satz Rich-Text-Inhalt ist und noch Formatierungselemente enthält. Zu diesen Elementen gehören die ursprüngliche Textschriftart (Helvetica) und die ursprüngliche Schriftgröße (12). Diese Elemente hätten verworfen werden müssen. Also ist entweder mein Code fahrlässig oder ich habe einen echten Fehler in AppleScript selbst gefunden. Ich gehe davon aus, dass es letzteres ist.
Der kürzeste Code, der zum Reproduzieren des Fehlers erforderlich ist:
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:
Kann jemand bestätigen, dass das angegebene Problem auf seinem Computer auftritt?
Wenn ja, wie kann ich die gesamte Formatierung von Rich Text in AppleScript entfernen, um diesen von mir entdeckten Fehler zu berücksichtigen?
Da the clipboard
der Befehl andere Typen automatisch hinzufügt, testen Sie dieses Skript:
set the clipboard to "hello" as string
delay 1
return clipboard info
das Ergebnis ist --> {{Unicode-Text, 10}, {String, 5}, {Scrap-Stile, 22}, {«class utf8», 5}, {«class ut16», 12}, {scrap-Stile, 22 }}
Verwenden Sie die Methoden von NSPasteboard , um die Stile zu vermeiden :
-- *** add the missing lines from your script here ***
--- set the clipboard to theClipboardTextWithoutAnyLineBreaksOrFormatting -- don't use this command to avoid the scrap styles type.
my putTextOnClipboard:theClipboardTextWithoutAnyLineBreaksOrFormatting -- use this method to put some string in the clipboard.
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 putTextOnClipboard:t
set thePasteboard to current application's NSPasteboard's generalPasteboard()
thePasteboard's clearContents()
thePasteboard's declareTypes:{current application's NSPasteboardTypeString} owner:(missing value)
thePasteboard's setString:t forType:(current application's NSPasteboardTypeString)
--> now the clipboard contains these types only: («class utf8», «class ut16», string and Unicode text)
end putTextOnClipboard:
Beim Testen mit Ihrem Code und dann mit meinem eigenen einfachen AppleScript- Code kann ich das (unerwünschte) Verhalten bis zu einem Punkt reproduzieren, dem ich zustimmen würde, dass das Verhalten nicht das ist, was gewollt ist und als Fehler angesehen werden könnte, aber die Problemumgehung ist ein bisschen wie ein Klacks .
Bei dieser Methode wird das ProblemtheClipboardTextWithoutAnyLineBreaksOrFormatting
nicht direkt in die Zwischenablage verschoben, sondern in eine temporäre Datei geschrieben, dann mit einem Befehl in die Zwischenablage verschoben und dann die temporäre Datei gelöscht. Dann kann es am Ziel-Einfügepunkt eingefügt werden.pbcopy
do shell script
Kommentieren Sie zum Testen des unten stehenden Workaround- Codes die Zeile aus set the clipboard to theClipboardTextWithoutAnyLineBreaksOrFormatting
und platzieren Sie den Workaround- Code direkt danach und vor der tell application "System Events" to keystroke "v" using {command down}
Zeile.
set tempFileToRead to POSIX path of (path to desktop) & ".tmpfile"
try
set referenceNumber to open for access tempFileToRead with write permission
write theClipboardTextWithoutAnyLineBreaksOrFormatting to referenceNumber
close access referenceNumber
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
try
close access referenceNumber
end try
return
end try
do shell script "pbcopy<" & tempFileToRead & "; rm " & tempFileToRead
as text
, eine Rich-Text-Schriftart und -Schriftgröße nicht beibehalten werden sollte?
grg
Benutzer3439894
Rubiks Sphäre
Rubiks Sphäre