Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Workflow for opening, creating a new Word document in current Finder folder

Hello, I am new to automator. I am trying to create a small app which can be activated from a finder toolbar. Once activated it will create a new word document in the current folder and save it as say, Untitled. Maybe even open it...


The problem I am facing is how to capture the current path name (full path name) of the current folder which can then be fed as input to the Save Word Document action.


My sequence is:


1 Get Selected Finder items (but not sure how I can pass the value of the current folder path name to next step)


2 Set Value of Variable


3 Create New Word Document


4 Save Word Document. Here is where I am stuck (I suppose) where I need to specify a variable under Where field that contains the pathname.



here is my screenshot.


iMac 27″, macOS 11.1

Posted on Dec 30, 2020 9:43 AM

Reply
Question marked as Best reply

Posted on Dec 30, 2020 1:03 PM

As Barney noted there's nothing in Automator directly that will do what you want, but all is not lost.


What you want can be done via AppleScript - and you can invoke it via the Run AppleScript action if you want to keep it within Automator.


tell application "Finder"
	-- find the current open folder
	set currentPath to POSIX path of ((get insertion location) as alias)
	-- work out the new file name
	set newFilename to (currentPath & "new.doc")
	-- hack using 'touch' to create the file
	do shell script "touch " & quoted form of newFilename
	-- and open it
	open POSIX file newFilename as alias
end tell


This works on a couple of principles. First is that the Finder tracks the current logical 'insertion location' for you, which typically points to the folder for the frontmost window. I use that with an appended 'new.doc' filename.

I then use the shell command "touch" to validate the file - this does one of two things - it either updates the modification date of the file, or it creates the file if it doesn't already exist. This gets around needing extra logic to handle pre-existing files.

Either way we know the file will exist at this point, so we just tell the Finder open it. Since it has a '.doc' extension, Word will open, and it doesn't care if the file is empty.


Note that doing it this way you'll just end up with a load of 'new.doc' files spread around, and you'll likely need to rename these at some point. Usually people opt to name the file when it's saved, but whatever floats your boat... :)

Similar questions

4 replies
Question marked as Best reply

Dec 30, 2020 1:03 PM in response to sarafn

As Barney noted there's nothing in Automator directly that will do what you want, but all is not lost.


What you want can be done via AppleScript - and you can invoke it via the Run AppleScript action if you want to keep it within Automator.


tell application "Finder"
	-- find the current open folder
	set currentPath to POSIX path of ((get insertion location) as alias)
	-- work out the new file name
	set newFilename to (currentPath & "new.doc")
	-- hack using 'touch' to create the file
	do shell script "touch " & quoted form of newFilename
	-- and open it
	open POSIX file newFilename as alias
end tell


This works on a couple of principles. First is that the Finder tracks the current logical 'insertion location' for you, which typically points to the folder for the frontmost window. I use that with an appended 'new.doc' filename.

I then use the shell command "touch" to validate the file - this does one of two things - it either updates the modification date of the file, or it creates the file if it doesn't already exist. This gets around needing extra logic to handle pre-existing files.

Either way we know the file will exist at this point, so we just tell the Finder open it. Since it has a '.doc' extension, Word will open, and it doesn't care if the file is empty.


Note that doing it this way you'll just end up with a load of 'new.doc' files spread around, and you'll likely need to rename these at some point. Usually people opt to name the file when it's saved, but whatever floats your boat... :)

Dec 31, 2020 10:09 AM in response to sarafn

I'm not altogether surprised by this - few applications ever expect to find a 0-byte sized file. This really is a kind of hack making a dummy file and tricking the app into opening it.


If you do want to handle other apps then there are two alternative approaches I can think of.


One is to launch the application and save a blank document (rather than create an empty document and open the app). The success on this may depend on the target app's ability to be scripted).


The other is to create a library of valid empty documents for each application (.xlsd, .key, .pages, .whatever). Then, instead of creating a blank document you simply duplicate the template file, then open the new copy. Since it's a valid document the application shouldn't care. Something like this:


tell application "Finder"
	-- find the templates folder
	set templateFolder to folder "Templates" of (path to documents folder)
	-- get a list of files in it
	set apps to name of every file of templateFolder
	-- ask the user which one they want
	set appChoice to choose from list apps with prompt "Which document would you like to create?"
	-- extract the selected item
	set theDoc to item 1 of appChoice
	-- duplicate the template file
	set newFile to duplicate (file theDoc of templateFolder) to (get insertion location)
	-- and open it
	open newFile
end tell


Workflow for opening, creating a new Word document in current Finder folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.