Add a subgroup automatically to a newly created group

Hi,

I’m adapting slightly the Daily Journal scripts which have been posted by various people: it’s essentially a cut and shut between Christian’s markdown daily journal script and Iain Dunn’s ‘Daily Journal with the lot’ script (Daily Journal with the lot 😄). There are no problems with this hybrid — it works perfectly…

My problem is that I want to adapt the part which creates a new location for the Monthly Journals to add a “media” subgroup by default (for images for easy markdown links). The media subgroup will not be populated by any documents at this stage: it’s just creating the empty subgroup in advance.

My attempt is:

set myGroup to create location "/My Writing/Journal/Journal " & theYear & "/" & numMonth & " " & theMonth

set myMediaGroup to create location myGroup & "/media"

The first line works (as it should, because it’s copied from people who know what they’re talking about…), but the second one causes an error:

The second line is the only different line in the whole script, so I assume that this is the line causing the problem, but I can’t see why.

What am I doing wrong and/or misunderstanding, please?

Many thanks

David

create location needs a “path” but you are a passing a group reference.

You either need to

  • join (location of myGroup) & (name of myGroup) (this one should be escaped in case it contains a /) & "/media"

or

  • don’t use create location in this case and use create record with instead:
create record with {name:"media", type:group} in myGroup
1 Like

@pete31,

Thanks very much for such a quick reply — that makes sense now you’ve pointed it out!

I’m very grateful

Cheers

David

1 Like

Just a followup for completeness in case anyone else finds this thread looking for the answer.

create record with {name:"media", type:group} in myGroup

is not enough on its own, because it creates the subfolder every time you run the script and you end up with duplicates.

The answer is to wrap this line in an if expression with checks to see if the subgroup exists, for which the syntax (substitute the path of the parent folder for myGroup and after exists record at , of course).

set theDatabase to open database journalDatabase
		
	set myGroup to create location "/My Writing/Journal/Journal " & theYear & "/" & numMonth & " " & theMonth
		
if not (exists record at "/My Writing/Journal/Journal " & theYear & "/" & numMonth & " " & theMonth & "/media") then
			
	create record with {name:"media", type:group} in myGroup
end if

(I know this will be obvious to most, but if someone’s come looking for the answer to my original very basic question, perhaps it will also be useful to them?)

1 Like

A good attempt!

However, the error-trapping is actually unnecessary in this case as creating a location will not generate duplicate groups.

tell application id "DNtp"
	tell current database
		set myGroup to create location "/My Writing/Journal/Journal"
		create location "/My Writing/Journal/Journal/media"
	end tell
end tell

You can run this a dozen times and it will never generate a duplicate set of groups.

Hi Jim,

A second ‘create location’ didn’t work when I tried it originally (see the first post in this thread — I kept getting an error message).

As Pete1 suggested, that was because I was using myGroup incorrectly as the basis for the second location.

As you point out, I could simply recreate the path manually for the second location command, without the need for myGroup / trapping a create record command.

Secondly, Pete1 suggested

join (location of myGroup) & (name of myGroup) (this one should be escaped in case it contains a /) & “/media”

I didn’t try that (mainly because I wasn’t sure of the exact syntax), but messing around further, I see that

create location (location of myGroup) & "/media"

also works.

I’ll get there in the end — many thanks to both of you!

David

Indeed this is certainly feasible in this instance…

create location (location of myGroup) & "/media"

You’re welcome. :slight_smile:

Jim,

Sorry for reopening this discussion, but I want to test my (clearly faulty) understanding of what’s going on:

(Quick Recap: I have a template which creates a new markdown file under a monthly group, the name of which changes as the month does. I want the subgroup ‘media’ to be created whenever the month changes.)

The following code (based on the advice from Pete and Jim above) almost works as expected:

set theDatabase to open database journalDatabase
set myGroup to create location "/My Writing/Journal/Journal " & theYear & "/" & numMonth & " " & theMonth
create location (location of myGroup) & "/media"

The problem is that it creates the subgroup ‘media’ as a sibling of myGroup, not as a child.

This is what I see:

I’ve just been moving it manually for the time being, because life got in the way of tinkering with AppleScript. Today I decided to see what’s going on, but I just can’t see why it doesn’t work as expected.

What am I missing, please?

Thanks!

You have to include name of myGroup.

create location (location of myGroup) & (name of myGroup) & "/media"

Take a look at AppleScript: A brief description of “create location” and a handler

1 Like

Pete,

It works now – thanks very much for your quick reply and the link to your post (which somehow I’d missed before…)!

1 Like