Automator WorkFlow/Applescript to pause until Excel is closed

I’ve got a MS Excel Macro enable file that reformats a csv file to a particular format and writes a new file to a folder in /use/library/group containers. It has to write it here because of the sandboxing of current version of Excel. What I want to then do is move the file to the user/downloads (destination) folder from that folder in /use/library/group containers (source). The issue I’m having is that the work flow consists of running an apple script to open excel with the correct file, then find that file in Finder and Move it to /user/downloads. The problem is that movement portion of the script happens before Excel is finished processing. What I need to do is get the apple script to wait until Excel is closed then move the file after it is created. I’ve tried a couple of things including a portion the script that checks to see if Excel is running or not but it still doesn’t seem to wait. The file remains in the source folder. The Excel macro itself functions perfectly.

Here’s the script I’m trying to run:



tell application "Finder" to open POSIX file "/Users/maxtoddring/Documents/YNAB/Excel Files/YNAB Conversion 2.7 working.xlsm"

repeat
	if application "Microsoft Excel" is not running then exit repeat
	delay 1
end repeat

tell application "Finder" to move entire contents of folder "Macintosh HD:Users:maxtoddring:Library:Group Containers:UBF8T346G9.Office:YNAB" to folder "Macintosh HD:Users:maxtoddring:Downloads"



How about avoiding the problem altogether, by changing your macro to save directly into the downloads folder. I think you should be able to remove the sandboxing restriction on the downloads folder by running this bit of code before you run your macro:

set theDowloads to path to downloads folder
tell application id "com.microsoft.Excel" -- Microsoft Excel
	try
		paste theDowloads -- will error
	end try
end tell

The paste could be any other command that takes a parameter. It’s just a way of sending a file object to Excel, which then gives it access to it.