Kann mir jemand sagen, wie ich am besten automatisch eine Leerzeile oben und unten in diese neu erstellten .rtf-Dateien für diese beiden separaten Codeteile einfügen kann?
Erster Codeabschnitt (wenn die Zwischenablage Rich-Text enthält):
try
set richTextfromClipboard to get the clipboard as «class RTF »
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
return
end try
try
set fileHandle to open for access theRichTextFilepath with write permission
write richTextfromClipboard to fileHandle
close access fileHandle
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 fileHandle
end try
end try
Zweites Stück Code (wenn die Zwischenablage HTML enthält):
try
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 > " & quoted form of theRichTextFilepath
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
end try
Die Leerzeilen müssen keine vorgegebene Schriftart oder Schriftgröße haben; die leeren Zeilen sollten einfach dem Stil der umgebenden .rtf-Datei entsprechen (d. h. dem Stil des Rich-Texts in der Zwischenablage).
Sie können dieses Cocoa-AppleScript verwenden:
-- Cocoa-AppleScript-- you need these lines in your script
use framework "Foundation"
use scripting additions
on insertTextInRTFFile(f) -- the parameter must be the posix path of an existing file, example --> "/Users/someName/Desktop/xyz.rtf"
set myText to linefeed -- the text to insert in the rtf (a blank line as example)
tell current application
set fileURL to its (NSURL's fileURLWithPath:f)
set myOption to its (NSDictionary's alloc()'s initWithObjectsAndKeys_(its NSRTFTextDocumentType, its NSDocumentTypeDocumentOption, missing value))
set {attrString, docAttrib, err} to its ((NSMutableAttributedString's alloc())'s initWithURL:fileURL options:myOption documentAttributes:(reference) |error|:(reference)) -- get the attributed string from the RTF file
if attrString is missing value then error err's localizedDescription() as text -- not a RTF file or not a valid path
-- it use the string in the 'myText' variable to create an attributed string
set firstline to its ((NSAttributedString's alloc())'s initWithString:myText attributes:(attrString's attributesAtIndex:0 effectiveRange:(missing value))) -- create an attributed string with the style of the first character
set lastline to its ((NSAttributedString's alloc())'s initWithString:myText attributes:(attrString's attributesAtIndex:((attrString's |length|()) - 1) effectiveRange:(missing value))) -- create an attributed string with the style of the last character
end tell
attrString's insertAttributedString:firstline atIndex:0 -- insert an attributed string (blank line) at the top
attrString's appendAttributedString:lastline -- insert an attributed string (blank line) at bottom
set rtfData to attrString's RTFFromRange:{0, attrString's |length|()} documentAttributes:docAttrib -- create the data from an attributed string
rtfData's writeToFile:f atomically:true -- write the data to the RTF file
end insertTextInRTFFile
Nachdem Ihr Skript in die RTF-Datei geschrieben hat: Rufen Sie einfach den insertTextInRTFFile()
Handler mit dem Posix-Pfad der RTF-Datei als Parameter auf.
Benutzer3439894