I’m trying to convert someRTF TextEdit links to plain text.
I use this script which comes from many people help in this forum
activate application “TextEdit”
repeat with theIncrementValue from 1 to 15
tell application “System Events”
tell process “textedit”
keystroke “k” using {command down}
keystroke “a” using {command down}
keystroke “c” using {command down}
key code 53
keystroke “v” using {command down}
keystroke “g” using {command down}
end tell
end tell
end repeat
I have always the same RTF file with:
1 Name [tab] date [tab] subject [tab] and LINK [Carriage Return]
Therefore I only need to search for the word LINK which I place in find dialogue box in test Edit before running the script.
However eacvh file I receive has a different number of lines is there a way to tell the script how many times it needs to repeat automatically?
Another problem is as follows: after opening the file I put the cursor on the first line but mopre often than not the script seems to run from the middle or other lines.
So the first part will open the file, read how many lines end with LINK and dicides how many time to run
If I have an rtf file created in textedit then I can get the hyperlinks with the following. Note that the file need not be open in textedit for this to work. This works because if I read the rtf file as text then I get “rtf-styled” text code returned. As such I then only need to parse the text to extract the hyperlinks. That’s where “text item delimiters” are used.
set filePath to (path to desktop folder as text) & "linkTest.rtf"
set startDelimiter to "{HYPERLINK \""
set endDelimiter to "\"}}"
set hyperlinks to {}
set rtfText to read file filePath
set text item delimiters to startDelimiter
set theItems to text items of rtfText
if (count of theItems) is greater than 1 then
set text item delimiters to endDelimiter
repeat with i from 2 to count of theItems
set a to text items of (item i of theItems)
set end of hyperlinks to item 1 of a
end repeat
end if
set text item delimiters to ""
return hyperlinks
After looking at your original post again I see that you want to find the word “LINK” in the text and replace that with the actual link. My script in post #2 just gets the links, so I thought I’d make this script do what you wanted. Again there’s no reason to use unreliable gui scripting for this task. We can just manipulate the “rtf-Style” code text as we wish so try this on the rtf file.
NOTE: this will overwrite the rtf file, so make a backup of it before you try this script just in case…
set filePath to (path to desktop folder as text) & "linkTest.rtf"
set startDelimiter to "{HYPERLINK \""
set endDelimiter to "\"}}"
set linkDelimiter to "LINK"
set rtfText to read file filePath
set text item delimiters to startDelimiter
set theItems to text items of rtfText
if (count of theItems) is greater than 1 then
repeat with i from 2 to count of theItems
set text item delimiters to endDelimiter
set a to text items of (item i of theItems)
set theLink to item 1 of a
set text item delimiters to linkDelimiter
set b to text items of (item i of theItems)
set text item delimiters to theLink
set (item i of theItems) to b as text
end repeat
end if
set text item delimiters to startDelimiter
set adjustedText to theItems as text
set text item delimiters to ""
writeTo(adjustedText, filePath, false, string)
(*==================== SUBROUTINES ===================*)
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
try
set target_file to target_file as Unicode text
if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as mode
close access the open_target_file
return true
on error
try
close access file open_target_file
end try
return false
end try
end writeTo
Hi Hank. Your help saved me, however I still have some problems in a similar kind of file where there is no way to insert automatically the delimiters although I know what the are:
The page looks as this
Subject [with an hidden link] CR
Sender [sender name link but the address is hidden] CR
date in plain text CR
CR
Subject2 [with an hidden link] CR
Sender2 [sender name link but the address is hidden] CR
date2 in plain text CR
CR
Subject3 [with an hidden link] CR
Sender3 [sender name link but the address is hidden] CR
date3 in plain text CR
CR
And so on.
I merged the two scripts you sent me as follows:
set filePath to (path to desktop folder as text) & "linkTest.rtf"
set startDelimiter to "{HYPERLINK \""
set endDelimiter to "\"}}"
set hyperlinks to {}
set rtfText to read file filePath
set text item delimiters to startDelimiter
set theItems to text items of rtfText
if (count of theItems) is greater than 1 then
set text item delimiters to endDelimiter
repeat with i from 2 to count of theItems
set a to text items of (item i of theItems)
set end of hyperlinks to item 1 of a
end repeat
end if
set text item delimiters to ""
writeTo(adjustedText, filePath, false, string)
(*==================== SUBROUTINES ===================*)
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
try
set target_file to target_file as Unicode text
if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as mode
close access the open_target_file
return true
on error
try
close access file open_target_file
end try
return false
end try
end writeTo
This is what I get in results from the Applescript Result window at the end of the process:
In other words the script decodes the Hyperlinks but doesn’t overwrite POSIX file the original page and skips the Date completely.
I am not very experienced but looking at the subroutine the lines indeed contains “:” but even trying to change “:” to Student_Profile and reversing it as:
if target_file contains “Student_Profiles” then set target_file to POSIX file target_file as text it didn’t do anything.