Hyperlink (with TextEdit)

Hey all,

I have list of records {fileA:nameFile, URLa:URL, fileB:nameFile, URLb:URL}
and I want to convert it into a document in textEdit such as:
NameFileA
NameFileB
And each name (which is only one for each line) is an hyperlink containing the corresponding URL.

I wrote the script below. It works.
My questions are:
Is there any better way to do it? I guess I am forced to use TextEdit, which is very little scriptable !!!
It is even impossible to say: “select word 2 of line 3 of paragraph 4”
To select some text I had to use command such: “key code 125 using {command down}” !!!

On the very top of the script I use the line:

 "set target_app to (path to frontmost application as Unicode text)"

Which I copied from an example I found. Without this, it doesn’t work.
But I don’t really understand its function

Here is the script:

set target_app to (path to frontmost application as Unicode text)
-- Making a list of records a sit will be in th final script
set theList1 to {}
set g_theFileName to "s41588x"
set thePath1_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x.pdf"
set this_file_name to "s41588x"
set thePath2_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x.pdf"
set end of theList1 to {filename1:g_theFileName, pathURL1:thePath1_URL, filename2:this_file_name, pathURL2:thePath2_URL}
# one more record
set g_theFileName to "s41588x"
set thePath1_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x.pdf"
set this_file_name to "s41588x2"
set thePath2_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x2.pdf"
set end of theList1 to {filename1:g_theFileName, pathURL1:thePath1_URL, filename2:this_file_name, pathURL2:thePath2_URL}

ParsingNameAndURL(theList1)

on ParsingNameAndURL(theList)
	tell application "TextEdit"
		activate
		make new document
		delay 0.5
		repeat with i in theList
			make new paragraph at end of paragraphs of text of document 1 with data ((filename1 of i) as string) & return
			set the clipboard to ((pathURL1 of i) as string)
			tell application "System Events"
				-- keystroke (ASCII character 127) using {command down} -- key code 126 using {command down}
				tell process (my target_app)
					key code 125 using {command down} -- all the way down
					delay 0.8
					key code 126 using {option down, shift down} -- 1 paragraph up and selecting it
					keystroke "k" using {command down}
					delay 0.5
					keystroke "v" using {command down}
					keystroke return
				end tell
			end tell
			make new paragraph at end of paragraphs of text of document 1 with data ((filename2 of i) as string) & return
			set the clipboard to ((pathURL2 of i) as string)
			tell application "System Events"
				-- keystroke (ASCII character 127) using {command down} -- key code 126 using {command down}
				tell process (my target_app)
					key code 125 using {command down} -- all the way down
					delay 0.8
					key code 126 using {option down, shift down} -- 1 paragraph up and selecting it
					keystroke "k" using {command down}
					delay 0.5
					keystroke "v" using {command down}
					keystroke return
				end tell
			end tell
		end repeat
	end tell
end ParsingNameAndURL

Thanks !

If by better you mean without using UI scripting, then yes. But it’s not exactly intuitive…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

-- where to save file
set posixPath to POSIX path of ((path to desktop as text) & "Test.rtf")

-- Making a list of records a sit will be in th final script
set theList1 to {}
set g_theFileName to "s41588x"
set thePath1_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x.pdf"
set this_file_name to "s41588x"
set thePath2_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x.pdf"
set end of theList1 to {filename1:g_theFileName, pathURL1:thePath1_URL, filename2:this_file_name, pathURL2:thePath2_URL}
# one more record
set g_theFileName to "s41588x"
set thePath1_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x.pdf"
set this_file_name to "s41588x2"
set thePath2_URL to "file:///Users/ldicroce/Desktop/Test%20Folder/s41588x2.pdf"
set end of theList1 to {filename1:g_theFileName, pathURL1:thePath1_URL, filename2:this_file_name, pathURL2:thePath2_URL}

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

on ParsingNameAndURL(theList)
	-- attributed string to add to
	set styledText to current application's NSMutableAttributedString's new()
	repeat with i in theList
		set nameString to (filename1 of i) & return
		set linkString to (pathURL1 of i)
		set attString to (my makeLinkWithString:nameString fileURL:linkString)
		(styledText's appendAttributedString:attString)
		
		set nameString to (filename2 of i) & return
		set linkString to (pathURL2 of i)
		set attString to (my makeLinkWithString:nameString fileURL:linkString)
		(styledText's appendAttributedString:attString)
	end repeat
	return styledText
end ParsingNameAndURL

on makeLinkWithString:nameString fileURL:linkString
	set linkURL to (current application's |NSURL|'s URLWithString:linkString)
	set linkAtts to (current application's NSDictionary's dictionaryWithObjects:{linkURL, current application's NSColor's blueColor(), (get current application's NSSingleUnderlineStyle)} forKeys:{current application's NSLinkAttributeName, current application's NSForegroundColorAttributeName, current application's NSUnderlineStyleAttributeName})
	set attString to (current application's NSMutableAttributedString's alloc()'s initWithString:nameString attributes:linkAtts)
	return attString
end makeLinkWithString:fileURL:

Edited script.

Thanks a lot !!!
More things to study now :rolleyes:

Hey,

I realised that the Summary file generated does not contain any URL.
I tried to click on the files name (in the TextEdit document) but nothing append.
I also opened it with BBedit and there is no URLs inside.

Am i missing something ?

My mistake. Where it says:

       set linkString to (pathURL1 of i) & return

delete the “& return”. Do the same where it uses pathURL2. I’ll edit the original.

Thanks.
It is perfect now !!