Can I create an alias programmatically?

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:

In PHP (or other languages) you could simply use
$date->format('yymmddhhii');
because someone actually thought about making the life of programmers easier.

Though formatting a date like this is also possible in AppleScript (with some help of ObjectivC), it is not exactly “easy”.

I just made a simple explanatory post about script libraries…

https://discourse.devontechnologies.com/t/script-libraries/59158/2

1 Like

Or in the shell…

date +"%Y%m%d%H%M%S"
# 20201029121143

:stuck_out_tongue:

1 Like

Right. I was more talking in general about “if I had a 10-line block of obscure code I had to re-use consistently…”

The thing that kind of weirds me out about AppleScript is that it’s both a super-high-level language (with lots of “syntactic sugar”) and for some reason they decided to make it strongly typed.

Those two things just don’t seem to go together to me.

Something to consider…

:slight_smile:

You’re right of course about the library aspect. The problem is that one needs 10 lines of code for something that would be a one-liner in other languages.
On the other hand don’t think that there’s a relationship between high-level and strongly typed one way or another. To wit: Pascal/Python/Java/C/C++/ObjC are strongly typed, Perl/JavaScript/PHP are not. Fortran is (or was) something in between.

What nags me is that they haven’t used the last 27 years to add more features to the language. Reg Ex, date/time, HTML, XML, JSON… Yes, that’s all in ASObjC, but that forces me to jump through a lot of loops for a little effect.
And they apparently do not deem it fit for some of their own products: Notes is badly supported, and there’s still no possibility to automatize (is that a word?) Numbers or Pages. The documentation is written (if it’s written at all) with no love and no interest to teach people.
I actually learned more from you and other people here in this forum in the last 6 months than in the 6 years before from trying to read Apple’s tech doc.

1 Like

I’ll agree that word-of-mouth is a great tool in learning AppleScript. :slight_smile: