Converting .docx to .dotx is there a script/automator somewhere?

I need to transfer a larger number of documents from .docx to .dotx. I was hoping to find a script somewhere but have not been able to find one. Does anybody know of such a script/automator? Or another fast way to do so?

PS I will need the same for Excel and PowerPoint, but figured that once I had one for one I can change this to work for the others.

My understanding is that it’s just matter of changing the name extension.
I will post a script doing that soon but it would be good to make a backup of your datas while I build the code.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 31 mai 2020 16:06:24

Please, click the dedicated button to extract the script from this message,
save the new script as a .scpt file
and run it without any change.

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- Yvan KOENIG (VALLAURIS, France) dimanche 31 mai 2020 16:29:22
----------------------------------------------------------------

property |⌘| : a reference to current application

set theFolder to choose folder

set fileManager to a reference to |⌘|'s NSFileManager's defaultManager()

set keysToRequest to {}

set skipsPackageDescendants to current application's NSDirectoryEnumerationSkipsPackageDescendants --> 2
set skipsHiddenFiles to current application's NSDirectoryEnumerationSkipsHiddenFiles --> 4
set theOptions to skipsPackageDescendants + skipsHiddenFiles
set theURL to theFolder as «class furl»
-- list every visible files from the selected folder (including subfolders content)
set allURLs to (fileManager's enumeratorAtURL:theURL includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value))'s allObjects()

set theFormat to "(self.pathExtension == 'docx')" -- to list  xx.lproj
set thePredicate to |⌘|'s NSPredicate's predicateWithFormat:theFormat
-- keep only files whose name extension is docx
set theDocxs to (allURLs's filteredArrayUsingPredicate:thePredicate)

-- replace the name extension docx by the dotx one.
repeat with anURL in theDocxs
	set bareURL to anURL's URLByDeletingPathExtension()
	set newURL to (bareURL's URLByAppendingPathExtension:"dotx")
	set {theResult, theError} to (fileManager's moveItemAtURL:anURL toURL:newURL |error|:(reference))
end repeat

#=====

It will list the entire content of the selected folder, will keep only the files whose name extension is docx and at last will change the extension to dotx.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 31 mai 2020 16:35:05

Yvan, I think this was so in Office 2008 and in my experience so in Office 2011. However, if you do so in Office 365 it will not open. I ran a test before posting and it does not. That is why I was hoping for a script.

In 2011 you could also designate a file to be a template by just dropping it in the template folder. In 365 they are not visible if you do this.

If you open the .doc file and choose to do a safe as and safe it as a .dotx you get the following dialog: https://ibb.co/7VKfrMk

Now if I am informed correctly the .dotx format in 365 is another format/structure as a .docx document. This would be the reason why I need to convert what I have if I change over and, as said, was hoping there is a script somewhere out there as I assume I am not the first one to be in this spot.

Yvan our posts must have crossed.

Your script does indeed change the document .docx into .dotx, but it refuse to open.

Right, when I ask LibreOffice to save as docx and dotx, I really get files with different contents so, as far as I understand, doing the wanted conversion require this protocol:
open the docx file
save as dotx file.
As Merdosoft products aren’t allowed on my machines, I can’t help you more.
I’m able to do the job with LibreOffice but I’m afraid that some details are changed by this app which, happily, is not a perfect clone of Office 365.

LibreOffice open the renamed files but, logically, treat them as standard documents, not as templates.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 31 mai 2020 17:51:24

I built a skeleton for the wanted script.

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- Yvan KOENIG (VALLAURIS, France) dimanche 31 mai 2020 16:29:22
----------------------------------------------------------------

property |⌘| : a reference to current application

set theFolder to choose folder

set fileManager to a reference to |⌘|'s NSFileManager's defaultManager()

set keysToRequest to {}

set skipsPackageDescendants to current application's NSDirectoryEnumerationSkipsPackageDescendants --> 2
set skipsHiddenFiles to current application's NSDirectoryEnumerationSkipsHiddenFiles --> 4
set theOptions to skipsPackageDescendants + skipsHiddenFiles
set theURL to theFolder as «class furl»
-- list every visible files from the selected folder (including subfolders content)
set allURLs to (fileManager's enumeratorAtURL:theURL includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value))'s allObjects()

set theFormat to "(self.pathExtension == 'docx')" -- to list  xx.lproj
set thePredicate to |⌘|'s NSPredicate's predicateWithFormat:theFormat
-- keep only files whose name extension is docx
set theDocxs to (allURLs's filteredArrayUsingPredicate:thePredicate)

-- Now we have an array containing the URLs of docx files
repeat with anURL in theDocxs
	-- convert the URL into an Hfs path
	set aPath to anURL as «class furl» as string
	set newPath to (text 1 thru -3 of aPath) & "tx" -- build the path of a dotx file
	(*
	-- here, fake code supposed to open and save as the file
	-- check the correct syntax in the application's dictionary
	tell application "Microsoft Word"
		set aDoc to open file aPath
		save aDoc in file newPath as "open format template" -- double check this instruction
		close aDoc without saving
	end tell
	*)
end repeat

#=====

Look in Office’s dictionary to get the correct syntax. I know that the application’s name must be changed but I have no idea about the correct save as instruction

I like Merdosoft Yvan. Unfortunately we are stuck with it.

I see how far I get with or suggestions.

Or hopefully there is somebody that has a ready made one.

Given what I found here and there, it seems that my pseudo code is quite correct.
I doubt about the string describing what the dictionaries name :
[as type] : the file type of the document in which to save the data.
I guess that it would resemble to : “open format template” but I saw also “open format template97”
As the found documents are quite old, maybe the developers introduced new types.

This found list is :[open format auto/open format document/open format template/open format rtf/open format text/open format Unicode text/open format encoded text/open format mac readable/open format web pages/open format xml/open format document97/open format template97/open format office]
It’s supposed to come from the description of the open function.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 31 mai 2020 20:53:41

Good morning, will have no time today to look in to it further so will need to wait till tomorrow.

Testing would be easier with this stripped script :

set aPath to choose file with prompt "Select a file of type “docx”"
(*
I tried with 
set aPath to choose file with prompt "Select a file of type “docx”" of type {"docx"}
but wasn't allowed to select files whose name extension is "docx"
*)
tell application "Microsoft Word"
	set aDoc to open file aPath
	log "point 1"
	save aDoc in file newPath as "open format template" -- double check this instruction
	log "point 2"
	close aDoc without saving
	log "point 3"
end tell

which don’t scan a folder’s content and whose history would tell which instruction failed if one does.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 2 juin 2020 11:55:30