-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTF styling 2
More file actions
129 lines (108 loc) · 4.72 KB
/
Copy pathRTF styling 2
File metadata and controls
129 lines (108 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use framework "Foundation"
use scripting additions
tell application id "DNtp"
try
-- Ensure a selection is made
set theseItems to the selection
if theseItems is {} then error "Please select a document."
-- Get Foundation classes we'll use
set NSFileManager to current application's NSFileManager
set NSString to current application's NSString
set NSAttributedString to current application's NSAttributedString
set NSMutableAttributedString to current application's NSMutableAttributedString
set NSData to current application's NSData
set NSUTF8StringEncoding to current application's NSUTF8StringEncoding
set NSMacOSRomanStringEncoding to current application's NSMacOSRomanStringEncoding
set NSDocumentTypeDocumentAttribute to current application's NSDocumentTypeDocumentAttribute
set NSRTFTextDocumentType to current application's NSRTFTextDocumentType
set NSNotFound to current application's NSNotFound
-- Process each selected item
repeat with thisItem in theseItems
set itemName to name of thisItem
set itemLocation to location of thisItem
set targetGroupName to itemLocation & "/Exploded: " & itemName
set targetGroup to create location targetGroupName in current database
-- Create a temporary path for RTFD
set desktopPath to POSIX path of (path to desktop as text)
set tempPath to desktopPath & "temp_exploded.rtfd"
-- Export the document as RTFD
export record thisItem to tempPath
-- Initialize file manager
set fm to NSFileManager's defaultManager()
-- Verify RTFD package exists and find RTF file
if not (fm's fileExistsAtPath:tempPath) then
error "Failed to create RTFD package"
end if
-- Try to find the RTF file
set rtfPath to tempPath & "/New Rich Text 1.rtf"
if not (fm's fileExistsAtPath:rtfPath) then
set rtfPath to tempPath & "/TXT.rtf"
if not (fm's fileExistsAtPath:rtfPath) then
error "Could not find RTF file in package"
end if
end if
-- Read RTF data
set rtfData to NSData's dataWithContentsOfFile:rtfPath
if rtfData is missing value then
error "Failed to read RTF file data"
end if
-- Create attributed string from RTF
set options to current application's NSDictionary's dictionaryWithObject:NSRTFTextDocumentType forKey:NSDocumentTypeDocumentAttribute
set errorRef to missing value
set attrString to (NSAttributedString's alloc()'s initWithData:rtfData options:options documentAttributes:(missing value) |error|:(reference to errorRef))
if errorRef is not missing value then
error "Failed to create attributed string: " & ((errorRef's localizedDescription()) as text)
end if
-- Get plain text and split into paragraphs
set plainText to attrString's |string|() as text
-- Split text into paragraphs using AppleScript's text handling
set paragraphList to {}
set tempText to plainText
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return, linefeed}
set paragraphList to text items of tempText
set AppleScript's text item delimiters to oldDelimiters
-- Process each paragraph
set startLocation to 0
repeat with i from 1 to count of paragraphList
set paraText to item i of paragraphList
if length of paraText > 0 then
try
-- Find the range of this paragraph in the original attributed string
set paraLength to length of paraText
set paraRange to current application's NSMakeRange(startLocation, paraLength)
-- Extract the styled paragraph
set paraAttrStr to (attrString's attributedSubstringFromRange:paraRange)
-- Convert to RTF data
set docAttrs to current application's NSDictionary's dictionary()
set paraRTFData to (paraAttrStr's RTFFromRange:(current application's NSMakeRange(0, paraAttrStr's |length|())) documentAttributes:docAttrs)
-- Convert to string
set rtfString to (NSString's alloc()'s initWithData:paraRTFData encoding:NSMacOSRomanStringEncoding) as text
-- Create record
create record with {name:paraText, rich text:rtfString, type:rtf} in targetGroup
on error errText
log "Failed to process paragraph: " & errText
end try
end if
-- Update start location for next paragraph (add 1 for the line ending)
set startLocation to startLocation + (length of paraText) + 1
end repeat
-- Clean up
tell application "Finder"
if exists POSIX file tempPath then
delete POSIX file tempPath
end if
end tell
end repeat
on error errMsg
-- Clean up on error
try
tell application "Finder"
if exists POSIX file tempPath then
delete POSIX file tempPath
end if
end tell
end try
display dialog "Error: " & errMsg
end try
end tell