Can I create an alias programmatically?

Say I have a Zettelkasten setup, and I’d like to effectively have two names for a document so I can use wiki linking.

Name 1: Really Awesome Note About Tacos
Name 2: 2020303080812

I can do this with aliases, and I can auto-generate that 2020303080812 number with something like Keyboard Maestro. So generating the names I want to use isn’t a challenge at all.

But assigning that alias to that file in an automated way is where things get challenging.

Is there a way to do this with a keyboard shortcut of some sort?

One possibility is to script this, another one might to use smart rules and the action Change Aliases. How is the alias exactly auto-generated?

Currently it’s generated manually in Keyboard Maestro - but I suppose I could generate it in a different way if that helps the process. The big thing is that I pretty much want this alias to be available immediately upon note creation.

In that case a smart rule which is performed on creation would be ideal. The smart rule could either use the Change Aliases action and placeholders or a script which would generate & set the alias.

tell application id "DNtp"
	set theFile to item 1 of (selection as list)
	log aliases of theFile as string
        --> ""
	exists (aliases of theFile)
        --> true
end tell

Aliases is a null string when none are present. I’m supposing that’s why exists reports as true. Since you can have multiple aliases, should they be a list instead and exists would report as false, like tags?

Just asking.

Okay…I have my tentative smart rule that at least applies an alias, and it fires properly on file creation. My challenge now is that date format. I see there’s a “creation date” property of each record - is there an easy way to turn that date into a string of the format I’m looking for in DEVONthink?

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set aliases of theRecord to "foo"
		end repeat
	end tell
end performSmartRule

Which format do you actually prefer?

YYYYDDDhhmmss. (DDD being “day of the year” - but I could do the 2 digit month + the 2 digit day as well)

There are many examples of woriing with dates in AppleScript on these forums. Here’s one…

A script isn’t even necessary, a rule is sufficient:

True but I was following @webwalrus’ train of thought :slight_smile:

I see that now, actually. That’s pretty cool. :slight_smile: Thanks!

Just as a note, I tried to do this as a script, because sometimes I like to futz around with things more than necessary. :wink: Any idea why the below code wouldn’t work? Aliases is still empty after the code fires. My code above works, but this doesn’t.

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set creationDate to (creation date of theRecord)
			set aliases of theRecord to creationDate
		end repeat
	end tell
end performSmartRule 

I don’t need it to get my task done, but it always frustrates me when I’m coding and something that looks like it should obviously work just doesn’t for an unknown reason.

Any thoughts?

You have to convert the date to a string before setting the aliases.

1 Like

Thanks!

In the dictionary…

These data types do not match - text and date - so you know some coercion is required.

That makes a ton of sense. Thanks!

In my day-to-day work I mainly work in PHP, so frequently I forget that other languages are…pickier? about their type conversions. :smiley:

You’re welcome.

As to do the conversion in AppleScript:

set creationDate to (creation date of theRecord)
set theMonth to month of creationDate as number
set {theYear, theDay, theSeconds} to creationDate's {year, day, time}
set theAlias to (theYear as string) & theMonth & theDay & theSeconds

That gives something like "2020102957418" in theAlias
as string in the last line forces theYear to a string and then the concatenation operator does the same to the other variables – so AppleScript is sometimes less strict :wink:

1 Like

That’s really helpful. :slight_smile: Thanks!

Yes, but my teaching edition lays out the basics. :stuck_out_tongue:

2 Likes