I have a script which reads an rtf-file, make some text-replacements and send an email with te rtf-file as body. The replacement is done with a sub-routine, since it has to be done multiple times.
The script is working perfectly, but now, I want to use in in Filemaker, as a native Applescript. I had to make some adaptations, since I need some variables from FileMaker. So, my script is:
[appleScript]
use AppleScript version “2.4” – Yosemite (10.10) or later
use framework “Foundation”
use framework “AppKit”
use scripting additions
on run
if current application’s NSThread’s isMainThread() as boolean then
doStuff()
else
my performSelectorOnMainThread:“doStuff” withObject:(missing value) waitUntilDone:true
end if
end run
on doStuff()
set cApp to current application
tell application “FileMaker Pro Advanced”
set NaamFileDoel to cell “DoelBestand”
end tell
–
set theFile to ((path to desktop folder as text) & “textB.rtf”
set fileData to cApp’s NSData’s dataWithContentsOfFile:(POSIX path of theFile)
set {attrstring, attributes} to cApp’s NSMutableAttributedString’s alloc()'s initWithRTF:fileData documentAttributes:(reference)
if attrstring is missing value then error “Unable to read RTF file”
–
tell cApp
Replace(attrstring, “CONTRACTDATUM”, ContractDatum)
Replace(attrstring, “BWPLOMSCHRIJVING”, BwplOmschrijving)
Replace(attrstring, “BWHRNAAM”, BwhrNaam)
end tell
[/appleScript]
and my subrouting is:
[appleScript]
on Replace(Tekst, Zoekwaarde, Vervangwaarde)
set PlatteTekst to Tekst’s |string|()
–
set {theRegex, theError} to current application’s NSRegularExpression’s regularExpressionWithPattern:Zoekwaarde options:0 |error|:(reference)
if theRegex = missing value then error theError’s localizedDescription() as text
–
set theMatches to (theRegex’s matchesInString:PlatteTekst options:0 range:{0, PlatteTekst’s |length|()}) as list
set theMatches to reverse of theMatches – so you work from back to front to keep ranges accurate
repeat with aMatch in theMatches
(Tekst’s replaceCharactersInRange:(aMatch’s range()) withString:Vervangwaarde)
end repeat
end Vervangen
[/appleScript]
He’s not doing the sub-routine What I’m doing wrong? He says: impossible to continue…
Can someone help me?