Friday, January 28, 2022

#1 2018-08-27 10:54:40 am

ldicroce
Member
Registered: 2017-11-25
Posts: 264

Combining TextEdit files containing hyperlink

Hi all!!!
I have been silent for some time, but still developing scripts .....

I have a question:
I need very often to combine TextEdit files containing only 1 line of text each, but the text contains embedded hyperlink.
I basically need to open access to the file#1, copy all content and past it at the end of file#2 on a new line.
When I do this, I completely alter the hidden hypertext.

The files are generated using a subroutine kindly provided by Shane:

Applescript:

set styledText to ParsingNameAndURL(theList1)
-- first turn attributed string into RTF data
set theData to styledText's RTFFromRange:{0, styledText's |length|()} documentAttributes:{DocumentType:current application's NSRTFTextDocumentType}
-- write the data to the file
theData's writeToFile:posixPath atomically:true

Any suggestion?
Thanks a lot.

L.

Offline

 

#2 2018-08-27 06:00:23 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

Re: Combining TextEdit files containing hyperlink

This will get the contents of two files and write then to a third. You should be able to adjust it to your needs.

Applescript:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- needed for RTF

set posixPath1 to POSIX path of (choose file with prompt "Choose an RTF file" of type {"rtf"})
set posixPath2 to POSIX path of (choose file with prompt "Choose another RTF file" of type {"rtf"})
set destPath to POSIX path of (choose file name default name "Output.rtf")
-- read file as RTF data
set theData to current application's NSData's dataWithContentsOfFile:posixPath1
-- create attributed string from the data
set {theStyledString, docAttributes} to current application's NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(reference)
if theStyledString is missing value then error "Could not read RTF file"
-- make copy you can modify
set theStyledString to theStyledString's mutableCopy()
--- read second file as RTF data
set theData to current application's NSData's dataWithContentsOfFile:posixPath2
-- create attributed string from the data
set {theStyledString2, docAttributes} to current application's NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(reference)
if theStyledString2 is missing value then error "Could not read RTF file"
-- make attributed linefeed
set styledLF to current application's NSAttributedString's alloc()'s initWithString:linefeed
-- append to make single attributed string
theStyledString's appendAttributedString:styledLF
theStyledString's appendAttributedString:theStyledString2
-- turn attributed string into RTF data
set theData to theStyledString's RTFFromRange:{0, theStyledString's |length|()} documentAttributes:docAttributes
-- write the data to the file
theData's writeToFile:destPath atomically:true


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2018-08-28 02:07:40 am

ldicroce
Member
Registered: 2017-11-25
Posts: 264

Re: Combining TextEdit files containing hyperlink

Shane Stanley wrote:

This will get the contents of two files and write then to a third. You should be able to adjust it to your needs.

Although I dont know AppleScriptObjC, I can appreciate the logic of your script.
Thanks a lot !!!

It is still a bit puzzling that something like this which is very intuitive to do manually (with keyboard and/or mouse clicks) and it can be easily done using UI scripting, yet it is impossible to do it with AS or it becomes complicated at the time of using ObjC ....

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)