Scenario: Keynote v1 files are a package in which the presentation is in file ‘presentation.apxl’ in the package root folder. I’ve a process that makes a valid ‘presentation.apxl’ (it works as a Keynote if I add the file manually). I’d like to script this via a droplet.
My inital sketch is this:
tell application "Finder"
set myPath to (folder of (path to me))
set dataFilepath to ((myPath) as text) & "info.apxl"
set targetFolderPath to ((myPath) as text) & "target.key:"
move file dataFilepath to folder targetFolderPath
end tell
But it fails on the ‘set myPath…’ line. If the target is a normal folder:
set targetFolderPath to ((myPath) as text) & "xy:"
The process works. What extra must I do to be able to move the file into a package? Can anyone set straight?
Follow up question: I’d like the Keynote file to be a stationary file and the script a droplet. On dropping the apxl file on the script I’d like it to make a new copy of the stationery file and add the apxl file to that. Is this feasible?
I figured it out. I’ve taken out a lot of the warnings and input dialogs to shorten the script but here it is:
property presName : "Presentation"
tell application "Finder"
set myPath to (folder of (path to me)) as text
set srcPresPath to myPath & "template.key"
set srcDataPath to myPath & "presentation.apxl"
-- error stuff here to check a presentation.apxl file is present
-- get the desired Keynote file name
-- dialog stuff here to get users desired filename for keynote file
-- and to add '.key' extension if not supplied
set srcNewPresPath to myPath & presName
set oldPresPath to quoted form of POSIX path of srcPresPath
set newPresPath to quoted form of POSIX path of srcNewPresPath
-- warn before overwriting existing same-name file
set dittoParams to " " & oldPresPath & " " & newPresPath
do shell script "ditto" & dittoParams
set oldDataFilePath to quoted form of POSIX path of srcDataPath
set newDataFilePath to quoted form of POSIX path of (srcNewPresPath & ":presentation.apxl")
set mvParams to " " & oldDataFilePath & " " & newDataFilePath
do shell script "mv" & mvParams
open srcNewPresPath
end tell
-- bring Keynote to the front
tell application "Keynote"
activate
end tell
Note: Template.key is a blank Keynote v1 Keynote file. As AppleScript seemed incapable of doing the task, I’m using Shell commands (in which I’ve no great expertise). The copy of the ‘template’ Keynote file is done with ditto as the file is actually a package (folder). ditto gives no warning of overwrites so I test in Finder first. The mv command happily puts a file inside the package (Finder AS dictionary offers no such feature?). Again mv - I don’t think - gives overwrite warning but in this contexts we’d want to suppress them anyway. a lesson I learned from this is that for shell work I need the ‘quoted’ form of POSIX paths that take escape things like spaces in paths as they will break a command line if not escaped properly.
I doubt anyone will want to do exactly what I’m doing but I figure the above might help others working with packages. Any suggested improvements to my logic are welcomed!