Duplicating a document (file) in Numbers

Hi,

I’m trying to process a series of Numbers documents and copy some of their content into a few version of the document. I can’t seem to get the duplication command to work because I can’t get the “to location-specifier” phrase to accept a location. I’ve tried quite a few variation but here’s the current version of the program skeleton. I’m showing two different variations I’ve tried to create a proper “location-specifier”. I’m assuming this wants a directory but I’ve also tried creating a directory/filename as well.

The error I get is "Script Error – Numbers got an error: Can’t make alias “Macintosh HD:Users:username:Desktop:” into type location specifier.

I’ve tried the first version (the one that’s currently commented out) with it ending with “as alias”, “as string”, “as alias as string”, and with nothing after the closing “)”. None of those options work. I’ve also tried having the Finder application executate that command but that doesn’t work either.


tell application "Numbers"
activate
  set templateDoc to open "/Users/username/Desktop/TestDirectory/Template5.numbers"
 
  tell application "Finder" to set flist to files of folder (POSIX file "/Users/username/Desktop/TestDirectory/WeeklySalesSheets/") as alias list

-- Here's where I try to create the directory to use in the "to location-specifier" phrase of the duplicate command
--  I'm showing two different things I've tried -- and I've tried many other variations.

 
--set destDir to (POSIX file "/Users/username/Desktop/TestDirectory/WeeklySalesSheets/") as alias
 
  set destDir to (path to desktop)

 
  repeat with fn in flist
    set srcDoc to open fn
    set destDoc to duplicate templateDoc to destDir    -- This is the line that fails
    set destDoc's name to (name of fn & " Update")
 
  -- transfer data from srcDoc to destDoc
 
 
    close srcDoc
    close destDoc
  end repeat
 
 
    end tell

Thanks,
Dave

Numbers 3.5.2, OS X Yosemite (10.10.1)

A few thoughts.

You’re mixing POSIX and HFS paths. As templateDoc is POSIX, I’m guessing the target folder path should be POSIX too.

The commented code for destDir can only work when the target exists, a consequence of the as alias phrase.
In such cases you should build the target path as text, and use that to create the target object, before doing anything else.
[So, saying some_path_as_text as alias is a quick way to verify the target exists]

It may be (untested) that Numbers’ duplicate command is intended to duplicate objects in Numbers: sheets, tables, whatever.
In that case you’d have to use Finder to duplicate files.

The script has a tell Finder within a tell Numbers block. Don’t do that, even if it seems to work.
In general, a tell bock should contain only commands from the application being told.

Here is a rebuilt code which does the job.

property useShortcut : false
# true = use command + shift + "s" to duplicate
# false = trigger the menu item Numbers > File > Duplicate

# Don't call OSAX functions in a tell application block
set destDir to path to desktop

# Build the needed paths here, you will not have to hard code the username
set theFolder to (destDir as text) & "TestDirectory:WeeklySalesSheets:"
set template5path to (destDir as text) & "TestDirectory:Template5.numbers"

# Try to stop nesting tell different applications blocks
# I filter .DS_Store because on my machine it's visible
tell application "Finder" to set flist to (files of folder theFolder whose name is not ".DS_Store") as alias list

tell application "Numbers"
	activate
	repeat with fn in flist
		set srcDoc to open fn
		set sourceName to name of srcDoc
		if sourceName ends with ".numbers" then
			set newName to (text 1 thru -9 of sourceName) & " Update.numbers"
		else
			set newName to sourceName & " Update.numbers"
		end if
		# open template5 here to be sure that the document will be at front.
		set templateDoc to open template5path as alias
		# I'm not sure that Numbers duplicate command apply to files
		if useShortcut then
			# Use the dedicated shortcut
			tell application "System Events" to tell process "Numbers"
				# Put the process at front in case you clicked on a non-Numbers window during the execution
				set frontmost to true
				keystroke "s" using {command down, shift down}
			end tell
		else
			# Trigger the File > Duplicate menu item
			my selectMenu("Numbers", 3, 10)
		end if
		
		set destPath to (destDir as text) & newName
		# Save the newly created document in destDir with its new name
		save document 1 in file destPath
		delay 0.1
		set destDoc to document 1
		-- transfer data from srcDoc to destDoc
		# Here I put fake code to be sure that everything works well
		tell destDoc to tell active sheet to tell table 1
			set value of cell "C3" to current date
		end tell
		close srcDoc without saving
		close destDoc
		close templateDoc without saving
	end repeat
end tell

#=====
on selectMenu(theApp, mt, mi)
	activate application theApp
	tell application "System Events" to tell process theApp
		set frontmost to true # Required at this time by Yosemite 10.10.1 & 10.10.2
		tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi
	end tell
end selectMenu

#=====

I’m sure that it may be enhanced but see it as a starting point.

Yvan KOENIG (VALLAURIS, France) samedi 24 janvier 2015 12:08:27

Ah. Yes. No duplicating required.
Just create a new document, and save in file for which you build the output path yourself.
“document” is not same as “file”.
Thank you, Ivan.

Not exactly that.
The script use the Duplicate feature available in Numbers’s File menu.
It triggers it thru its shortcut command + shift + s
CAUTION : contrarily to what I thought for years, the shortcut is not valid worldwide.
This is why I edited my message containing the script.

Yvan KOENIG (VALLAURIS, France) samedi 24 janvier 2015 14:12:35

Thank you Yvan and Alastor,

I also got some help from a post I made to the Apple support forum as well.

Bottom line is that I should have just been using the Save command rather than the Duplicate command in the Numbers Applescript dictionary.

It seems my main confusion was thinking that I needed to duplicate the document before saving it with a new name. This is the model that’s used in the Numbers application and most Mac applications that i’m aware of, at least under more recent versions of OS X, so I assumed that’s how the Applescript for Numbers worked as well but it does not.

In fact, it seems that the duplicate command in Number’s Applescript dictionary is only there for duplicating objects within a spreadsheet (e.g. sheets and tables). It doesn’t work on documents.

As Yvan shows, you can cause the Numbers FIle|Duplicate function to be executed by using System Events and entering the keyboard shortcuts but as it turns out, you don’t need to duplicate the file.

The Save command in the Numbers Application dictionary actually functions as a “Save As…” function, you can use it to save a currently open document to a new file with a new file name.

Thanks to both of you for your help with this.

Dave

Model: macbook 2014
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

For completeness, here’s a simple example that opens a file/document and then saves a copy of it with a new location/filename.


tell application "Numbers"
	activate
	set templateDoc to open "/Users/davidsprague/Desktop/FoundationWine/Template5.numbers"
	set destDir to (path to desktop)
	
	set destDoc to save templateDoc in (destDir as text) & "DupTest"
	
end tell