Skip to content

Instantly share code, notes, and snippets.

@dpinney
Created July 6, 2021 15:43
Show Gist options
  • Save dpinney/f5de675274c9f1ad16df6794d614dea8 to your computer and use it in GitHub Desktop.
Save dpinney/f5de675274c9f1ad16df6794d614dea8 to your computer and use it in GitHub Desktop.
Convert all RTF and RTFD files to Word DOCX via AppleScript
# We do this with Pages because it's the only thing that can correctly convert rtfd.
# textutil works on rtf files with no text in them but not on rtfd.
set my_paths to {"/path/to/first/file.rtfd", "/path/to/second/file.rtfd"}
repeat with my_path in my_paths
tell application "Pages"
set my_file to (my_path as POSIX file)
set my_name to name of (info for my_file)
set doc to open my_file
export doc as Microsoft Word to alias (my_path & ".docx" as POSIX file)
close doc
tell application "Finder" to delete my_file
end tell
end repeat
@dpinney
Copy link
Author

dpinney commented Sep 22, 2022

Nothing worse than debugging applescript...

Maybe something like this would work?

set folderChoice to POSIX path of (choose folder)
tell application "System Events"
    set myPaths to POSIX path of disk items of folder folderChoice
end tell
repeat with myPath in myPaths
   <existing working code>
end repeat

@AJ-Duncan-Poole
Copy link

Your code worked..up to a point: Pages stopped running the script when it hit the menacing “.DS_Store”

I hard coded the folder and managed to do the same as your code: (with the same error)

tell application "System Events"
	set TitleList to POSIX path of items of folder "/Users/.../Documents/.../Client 1/"
end tell

set my_paths to result

repeat with my_path in my_paths
	tell application "Pages"
		set my_file to (my_path as POSIX file)
		set my_name to name of (info for my_file)
		set doc to open my_file
		export doc as formatted text to alias (my_path & ".rtfd" as POSIX file)
		close doc
	end tell
end repeat

I saw a post where someone suggested only showing visibleFiles but I don't know how to integrate this into the existing code..

set TitleList to POSIX path of "/Users/.../Client 1/"
tell application "System Events"
	set allVisibleFiles to files of folder TitleList whose visible is true
end tell

The result excludes the .DS_Store file but the list is not POSIX, rather it is: {file "Macintosh HD:Users:..:Client 1:Test_3.doc" of application "System Events", file...etc.}

@AJ-Duncan-Poole
Copy link

I GOT IT!!!! WOOOHOOO! So Stoked!

set folderChoice to POSIX path of (choose folder)
tell application "System Events"
	set allVisibleFiles to POSIX path of disk items of folder folderChoice whose visible is true
end tell
repeat with my_path in allVisibleFiles
	tell application "Pages"
		set my_file to (my_path as POSIX file)
		set my_name to name of (info for my_file)
		set doc to open my_file
		export doc as formatted text to alias (my_path & ".rtfd" as POSIX file)
		close doc
	end tell
end repeat

Thank you David for your valuable input! Doing cartwheels in my living room LOL!!!

@dpinney
Copy link
Author

dpinney commented Sep 22, 2022

Congrats! Nice hacking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment