There was a problem of permissions with the folder storing the files.
Here is the final script.
use AppleScript version "2.4" # Requires at least 10.10
use scripting additions
use framework "Foundation"
# 2016/11/04 - Use date stamp as file name
# 2016/11/04 - Store the new files in a subfolder named "Adam_reports" in the folder containing the original
# 2016/11/05 - some cleaning
# One instruction used here to select files to treat
# In real life I assume that the files would be delivered thru an other scheme
# submitting Numbers files as well as Excel ones
on run
set these_items to choose file "choose Excel or Numbers file(s)" of type {"org.openxmlformats.spreadsheetml.sheet", "com.microsoft.excel.xls", "com.apple.iwork.numbers.sffnumbers", "com.apple.iwork.Numbers.Numbers"} with multiple selections allowed
my germaine(these_items)
end run
on open sel
# sel is the list of items dropped onto the script saved as an application
set these_items to {}
tell application "System Events"
repeat with anItem in sel
if type identifier of anItem is in {"org.openxmlformats.spreadsheetml.sheet", "com.microsoft.excel.xls", "com.apple.iwork.numbers.sffnumbers", "com.apple.iwork.Numbers.Numbers"} then
set end of these_items to contents of anItem
end if
end repeat
end tell
my germaine(these_items)
end open
on germaine(these_items)
# Grabs the OS version once for future repeated use
set OSversion to text 1 thru 5 of system version of (system info)
set nbLoop to 50 # I never saw the loop using more than 6 pass
repeat with anItem in these_items
set cheminPosixDuFichierSource to POSIX path of anItem
set {posixName, posixContainer} to (my getNameAndContainer:cheminPosixDuFichierSource)
# Build a new name derived from the current dateTime : yyyyMMdd_hhmmss_Report_Adam.numbers
set HfsName to my dateStamp()
# This time we store the files in the subfolder "Adam_reports" in the hosting one
set subFolder to "Adam_reports"
copy posixContainer to originalPosixContainer
set posixContainer to (my buildFullPath:subFolder inFolder:posixContainer)
(my createFolder:posixContainer)
set cheminPosixDuFichierNumbers to (my buildFullPath:HfsName inFolder:posixContainer)
# coerce the paths into a format preferred by Numbers
set furlFichierNumbers to cheminPosixDuFichierNumbers as «class furl»
set furlSource to anItem as «class furl»
tell application "Numbers"
activate
open furlSource # opens the original file
end tell
tell application "System Events" to tell process "Numbers"
set frontmost to true
set pass to 0
repeat nbLoop times
delay 0.1
set pass to pass + 1
try
if name of window 1 ≠"" then exit repeat
end try
end repeat # nbLoop times
if pass = nbLoop then error "Numbers failed to open the doc !" number 12341
# Now we are sure that the original doc is open
tell window 1
set origName to its name # will use it later to close the window
keystroke "s" using {command down, shift down} # issue the duplicate command
end tell
# Now we have a new doc that we will save with the new name in the final location
tell window 1
keystroke "s" using {command down} # issue the save command
set pass to 0
repeat nbLoop times
delay 0.01
set pass to pass + 1
try
if exists sheet 1 then exit repeat
end try
end repeat # nbLoop times
if pass = nbLoop then error "Numbers failed to open the Save sheet !" number 12342
tell sheet 1
# I disabled these two instructions which are useful only to create the code
--class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
--title of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
set value of text field 1 to HfsName
keystroke "g" using {command down, shift down}
set pass to 0
repeat nbLoop times
delay 0.01
set pass to pass + 1
try
if exists sheet 1 then exit repeat
end try
end repeat # nbLoop times
if pass = nbLoop then error "Numbers failed to open the Go To sheet !" number 12343
tell sheet 1
# I disabled these two instructions which are useful only to create the code
--class of UI elements --> {static text, combo box, button, button}
--title of buttons --> {"Aller", "Annuler"}
# CAUTION, El Capitan and Sierra braught changes.
if OSversion is in {"10.11", "10.12"} then
set theTarget to combo box 1
else
set theTarget to text field 1
end if # OSversion is in {"10.11", "10.12"}
-- value of theTarget # I used it during tests
set value of theTarget to posixContainer
delay 0.01
click button 1 # "Aller" (in French)
end tell # sheet 1 (the Go To one)
delay 0.01
click button 1 # "Enregistrer" (in French)
end tell # sheet 1 (the Save one)
end tell # Window 1
end tell # System Events & process
delay 0.1
tell application "Numbers"
# Close the original document. I play safe with the try / end try instructions
try
close document origName without saving
end try
delay 0.1
# Open the Numbers file which was created just before.
open furlFichierNumbers
set myDoc to name of document 1
# here you may put your code.
tell document myDoc
tell sheet 1 to tell table 1
count rows --> 1084
count columns --> 80
end tell # sheet .
end tell # document .
close document myDoc with saving
end tell # Numbers
end repeat
end germaine
#=====
on getNameAndContainer:posixPath
local theURL, posixContainer, posixName
log "Entre dans getNameAndContainer"
set theURL to current application's |NSURL|'s fileURLWithPath:posixPath
set posixContainer to theURL's URLByDeletingLastPathComponent()
set posixName to theURL's lastPathComponent()
# ATTENTION, Sierra ne met pas le / final
# return two Posix objects
return {posixName as text, posixContainer's |path|() as text}
end getNameAndContainer:
#=====
on buildFullPath:proposedName inFolder:cheminPosixDuFichierSource
local theFolderURL, proposedName, theDestURL
log "Entre dans buildFullPath"
set theFolderURL to current application's |NSURL|'s fileURLWithPath:cheminPosixDuFichierSource
if class of proposedName is text then set proposedName to current application's NSString's stringWithString:proposedName
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
return theDestURL's |path| as text
end buildFullPath:inFolder:
#=====
-- Creates a new folder. There is no error if the folder already exists, and it will also create intermediate folders if required
on createFolder:posixPath # appelé par une instruction
local theDestURL, theFileManager, theResult, theError
set theDestURL to current application's |NSURL|'s fileURLWithPath:posixPath
set theFileManager to current application's NSFileManager's |defaultManager|()
set {theResult, theError} to theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference) # EDITED: replaced specifier by reference
if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
return theDestURL's |path| as text
end createFolder:
#=====
on dateStamp()
tell (current date) to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) & "_Report_Adam.numbers"
end dateStamp
#=====
In fact it’s not really final because I’m facing a wall under Sierra 10.12.1
The open handler fails.
I tested it with a code borrowed from AppleScript Language Guide
on open names
# sel is the list of items dropped onto the script saved as an application
set pathNamesString to "" -- Start with empty text string.
repeat with i in names
-- In this loop, you can perform operations on each dropped item.
-- For now, just get the name and append a return character.
set iPath to (i as text)
set pathNamesString to pathNamesString & iPath & return
end repeat
-- Store list in open document, to verify what was dropped.
tell application "TextEdit"
set text of front document to pathNamesString
end tell
return
end open
I got an awful result.
If I drop four files of the same type on the droplet, I get a list of three pathnames.
If I drop a jpeg file, an xls one, a numbers one and an xlsx one I get only the path of the two Excel files.
I wish that some of you test the droplet on their side because I can’t decide if I found a bug or if something is odd on my system.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 9 novembre 2016 16:32:03