Can I create an alias programmatically?

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

Just as a note, support like this is probably one of the things I love best about DEVONthink. Being able to get support for random stuff from the actual company behind the product is huge!

Regarding your “teaching edition”, in PHP I would normally take these lines:

	set thisYear to year of creationDate as string
	set thisMonth to month of creationDate as integer -- As string produces the name of the month.
	set thisDay to day of creationDate as string
	set thisHour to hours of creationDate
	set thisMinute to minutes of creationDate
	set thisSecond to seconds of creationDate
	set thisYear to (characters 3 thru 4 of thisYear) as string

and turn them into something like:

function fixTheDate(creationDate){
...
}

and then relegate that function to a library file of some sort - especially since I don’t write AppleScript enough to get this stuff etched into my brain. Are external libraries a thing in AppleScript? Or is this the sort of thing where you just keep a list of code samples hanging out in a (DEVONthink? :smiley: ) database somewhere?

1 Like

Either approach is workable.

You can create a script library with handlers (i.e., functions) for reusable code, though they’re not as commonly discussed in AppleScript circles. (Many AppleScripts for people are one-off projects.)

If you create a script library and save it in ~/Library/Script Libraries, you can call it from other scripts.

Just for the heck of it, a version in ASObjC:

use AppleScript version "2.5" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
tell application id "DNtp" 
repeat with theRecord in theRecords
set cDate to creationDate of theRecord
set theCalendar to current application's NSCalendar's currentCalendar()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyMMddHHmm"
set hour to ((cDate's time) / 3600) as integer
set minute to ((cDate's time) mod (3600 * hour)) as integer
set {theYear, theDay} to cDate's {year, day}
set theMonth to month of cDate as number
set newDate to theCalendar's dateWithEra:1 |year|:theYear |month|:theMonth |day|:theDay hour:hour minute:minute |second|:0 nanosecond:0
set theAlias to theFormatter's stringFromDate:newDate
end repeat
end tell

I think I’ll stick with pure AppleScript here :wink: