Is there a way in applescript to choose the folder where they are located to then export the various Hyperlink as readable text and save each file in to a folder of choice?
Yes. I’d just read the RTF into an Applescript variable using Applescript’s “read” command, which will give you the plain text markup behind the RTF.
Example, I made this RTF:
With the “Hyperlink” and “email” being hyperlinks. Saved as RTF.
Then Applescripted this:
set rawRTFtext to read alias [file path]
and now the contents of the rawRTFtext variable is:
From there, you can use vanilla Applescript, or regex via terminal or ASObjC or whatever, to separate out the link addresses via the preceding delimiter:
and the terminating delimiter
Then format as needed and save as a .txt file wherever you like.
Here’s an approach that deals with the text as RTF:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set thePath to "/Users/shane/Desktop/Test.rtf"
-- read file into attributed string
set theURL to current application's NSURL's fileURLWithPath:thePath
set {attString, theError} to current application's NSAttributedString's alloc()'s initWithURL:theURL options:(missing value) documentAttributes:(missing value) |error|:(reference)
-- get elngth so we can start from the end
set start to (attString's |length|()) - 1
-- make plain string copy to work on
set theString to attString's |string|()'s mutableCopy()
repeat
-- find link
set {aURL, theRange} to attString's attribute:(current application's NSLinkAttributeName) atIndex:start effectiveRange:(reference)
if aURL is not missing value then
-- get linked text
set linkText to theString's substringWithRange:theRange
if aURL's |scheme|()'s isEqualToString:"mailto" then -- email address
set newLink to aURL's resourceSpecifier()
else if linkText's containsString:"This Site" then -- resource specifier, remove //
set newLink to aURL's resourceSpecifier()'s substringFromIndex:2
else -- full URL
set newLink to aURL's absoluteString()
end if
-- replace link
theString's replaceCharactersInRange:theRange withString:newLink
end if
set start to (location of theRange) - 2
if start < 0 then exit repeat
end repeat
return theString as text
I didn’t add anything as I am not skilled enough to do as follows:
1 ask the script to select the folder with my 400+files
2 create a new file with the same name as the one the script convert to plaintext
3 export the result to this new file
4 save the new file in a folder of choice
I think perhaps you misunderstand the role of MacScripter. It’s a help facility, not a free script writing service. This should help you get started:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set theFolder to choose folder -- choose the folder containing the .rtf files
tell application id "com.apple.finder" -- Finder
set theFiles to every file of theFolder as alias list
end tell
repeat with aFile in theFiles
if (theFile as text) ends with ".rtf" then
set theURL to (current application's NSURL's fileURLWithPath:(POSIX path of aFile))
set {attString, theError} to (current application's NSAttributedString's alloc()'s initWithURL:theURL options:(missing value) documentAttributes:(missing value) |error|:(reference))
-- get elngth so we can start from the end
set start to (attString's |length|()) - 1
-- make plain string copy to work on
set theString to attString's |string|()'s mutableCopy()
repeat
-- find link
set {aURL, theRange} to (attString's attribute:(current application's NSLinkAttributeName) atIndex:start effectiveRange:(reference))
if aURL is not missing value then
-- get linked text
set linkText to (theString's substringWithRange:theRange)
if (aURL's |scheme|()'s isEqualToString:"mailto") then -- email address
set newLink to aURL's resourceSpecifier()
else if (linkText's containsString:"This Site") then -- resource specifier, remove //
set newLink to (aURL's resourceSpecifier()'s substringFromIndex:2)
else -- full URL
set newLink to aURL's absoluteString()
end if
-- replace link
(theString's replaceCharactersInRange:theRange withString:newLink)
end if
set start to (location of theRange) - 2
if start < 0 then exit repeat
end repeat
set newFile to (theURL's URLByDeletingPathExtension()'s URLByAppendingPathExtension:"text")
(theString's writeToURL:newFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value))
end if
end repeat
Thanks To Shane and StefanK for their time and help.
This is the final fully working script.
Fixed as StefanK suggested, and another small typo.
This could be very useful to other “not very skilled as myself” in ObjectiveC and Applescript users dealing with rtf files when in need to convert hyperlinks to plain text while keeping plain text as it is in the rtf file.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set theFolder to choose folder -- choose the folder containing the .rtf files
tell application id "com.apple.finder" -- Finder
set theFiles to every file of theFolder as alias list
end tell
repeat with aFile in theFiles
if (theFiles as text) ends with ".rtf" then
set theURL to (current application's NSURL's fileURLWithPath:(POSIX path of aFile))
set {attString, theError} to (current application's NSAttributedString's alloc()'s initWithURL:theURL options:(missing value) documentAttributes:(missing value) |error|:(reference))
-- get elngth so we can start from the end
set start to (attString's |length|()) - 1
-- make plain string copy to work on
set theString to attString's |string|()'s mutableCopy()
repeat
-- find link
set {aURL, theRange} to (attString's attribute:(current application's NSLinkAttributeName) atIndex:start effectiveRange:(reference))
if aURL is not missing value then
-- get linked text
set linkText to (theString's substringWithRange:theRange)
if (aURL's |scheme|()'s isEqualToString:"mailto") then -- email address
set newLink to aURL's resourceSpecifier()
else if (linkText's containsString:"This Site") then -- resource specifier, remove //
set newLink to (aURL's resourceSpecifier()'s substringFromIndex:2)
else -- full URL
set newLink to aURL's absoluteString()
end if
-- replace link
(theString's replaceCharactersInRange:theRange withString:newLink)
end if
set start to (location of theRange) - 2
if start < 0 then exit repeat
end repeat
set newFile to (theURL's URLByDeletingPathExtension()'s URLByAppendingPathExtension:"txt")
(theString's writeToURL:newFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value))
end if
end repeat