One folder, one Pages doc; alias in folder to Pages doc in iCloud

I’ve decided to simplify my task/project management system by using folders and Pages docs. This has been working quite well for me except that the project creation part is a bit tedious. I’d like some help automating it. Here’s a description of my imagined process:

  1. Ask for $CommitmentName
  2. Create folder ~/Documents/Commitments/$CommitmentName
  3. Create a Pages Document using $CommitmentTemplate iCloud/Commitments/$CommitmentName
  4. Create alias in folder ~/Documents/Commitments/$CommitmentName/ to Pages Document iCloud/Commitments/$CommitmentName

Note that my “$” is intended to indicate a variable… I’m just using it because I’ve seen such notation here and there on the inter-tubes.

In case my pseudo, pseudo code is confusing:

  1. I want to create a folder and Pages document with the exact same names.
  2. Rather than save the Pages document in that folder, it should be saved on iCloud (~/Library/Mobile Documents/com~apple~Pages/Documents/Commitments/)
  3. An alias to that Pages document in iCloud should be saved in the folder.

In step #2, I reference a variable called “CommitmentTemplate”… I’m not sure if I can crate a Pages doc, from a Template, via applescript but if I can, that would be nice. Note that the template is simply a blank doc with a dummy title and two dummy headers that currently exists in my Pages templates on my Mac.

Much Thanks,

John

As I already wrote in an other thread, useful infos are available in :

http://macosxautomation.com/applescript/iwork/keynote/
http://macosxautomation.com/applescript/iwork/numbers/
http://macosxautomation.com/applescript/iwork/pages/

You will find the exact syntax required to create a document from a user template.
There is also the syntax required to save a document with a given name in a given location.
You will have to edit it to save in the Cloud.
As I am old fashioned my preferred protocol would be to save locally then copy the local file into the Cloud.

Yvan KOENIG (VALLAURIS, France) jeudi 3 avril 2014 16:39:59

That takes care of the template part, potentially. But I don’t think your comment about iCloud is true. If you create a Pages doc on your desktop and then drag it to /Library/Mobile Documents/com~apple~Pages/Documents/, it will sync up to iCloud. Same thing if you Save to that folder in pages. So I’m not sure why I couldn’t create a Pages doc there via applescript.

Any ideas on naming the folder and the pages doc the same thing? What about the alias?

=Later Added=
Another solution I’ve been playing with is, instead of creating a new document in Pages, I copy an existing blank document from a dedicated folder. But then it’s the renaming and moving and aliasing that gets me stumped.

The fact that something may be done is not a guaranty that it will be safe.
I received so many corrupted Pages and Numbers documents to repair that I prefer a protocol saving it locally first.

If something fails, I may extract a not too old copy from the datas stored by Versions in the hidden folder .DocumentRevisions-V100

At least, I was able to do it with iWork '09.

At this time I didn’t made tests to check if my scripts Version_as_a_recovery_tool apply to the new format.

I’m too old to play with matches. I’m preferring suspenders AND belt.

Yvan KOENIG (VALLAURIS, France) jeudi 3 avril 2014 18:28:54

Almost there… The applescript below will do everything that I’ve described but it also creates an “Untitled” pages doc. Could someone tell me how to suppress this “Untitled.pages” creation?

Thank you,

John

-- Create a new document and save it into the Documents folder

-- generate the default file name
display dialog "Name of Commitment?" default answer ""
set the nameToUse to text returned of result

-- make sure the file name is not in use
set the destinationFolderHFSPath to ¬
	"FDD:Users:john:Library:Mobile Documents:com~apple~Pages:Documents:Commitments:" as string
--	"FDD:Users:john:Documents:" as string
--	(path to the documents folder) as string
repeat with i from 0 to 100
	if i is 0 then
		set incrementText to ""
	else
		set incrementText to "-" & (i as string)
	end if
	set thisFileName to nameToUse & incrementText & ".pages"
	set thisFilePath to destinationFolderHFSPath & thisFileName
	tell application "Finder"
		if not (exists document file thisFilePath) then exit repeat
	end tell
end repeat

tell application "Pages"
	activate
	-- create a new document and store its reference in a variable
	set thisDocument to make new document with properties ¬
		{document template:template "Commitment"}
	
	-- save the document
	save thisDocument in file thisFilePath
	--	close every document without saving
	open thisFilePath
end tell

tell application "Finder"
	make new folder at "FDD:Users:john:Documents:Commitments:" with properties {name:nameToUse}
	make new alias at "FDD:Users:john:Documents:Commitments:" & nameToUse to thisFilePath
end tell

To be sure that the application made is full start process, I tested with :

set p2d to path to desktop as text

tell application "/Volumes/Samsung 32/Applications/Pages.app/"
	activate
	delay 0.1
	
	# Instructions borrowed from :
	# http://macosxautomation.com/applescript/iwork/keynote/
	# http://macosxautomation.com/applescript/iwork/numbers/
	# http://macosxautomation.com/applescript/iwork/pages/
	repeat with i from (count of documents) to 1 by -1
		set thisDocument to document i
		if file of thisDocument is missing value then
			close thisDocument without saving
		end if
	end repeat
	make new document with properties {document template:template "4See"}
	
	count documents
	save document 1 in file (p2d & "azerty.pages")
end tell

Pages created an Untitled document which is automatically closed without saving.
Then it create the wanted document from the named template.

CAUTION

If you run such a code with some unsaved document open, you will loose them.
From my point of view, it’s playing with matches but I answered your question and you are free to use the tip or to forget it.

Yvan KOENIG (VALLAURIS, France) vendredi 4 avril 2014 18:23:51

Yvan - the closing without saving part, I commented that out. It was just something with which I was experimenting.

Can you tell me how to stop Pages from creating this untitled document?

We can’t change the application behavior.
Go to Pages > send a Comment about Pages
and ask Apple to add a choice in the preferences :
create nothing at startup.

At this time we have only :

create a new document from a default template
navigate in the Templates window to select the template to use.

If my memory is right, it doesn’t match Apple Human Interface Guidelines but it’s not an exception.

iWork components always behaved this way so I guess that they will always do.
It’s the same for TextEdit, for AppleScript Editor .
A lot of third party applications behave this way too.

I never fought against that because, from my point of view, it’s not a problem.
If a script is correctly written, it will target the correct document even if there are already tenth already open ones.

Yvan KOENIG (VALLAURIS, France) samedi 5 avril 2014 11:20:02

I don’t know what the guidelines say, but it is default behaviour when you build a document-based application; a developer has to turn it off if they don’t want it.