AppleScript help building a string with a loop

Hello all,

I’m hoping an AppleScript expert will chime in.

I have a script I cobbled together from MacScripter and DEVONthink forums. In it’s current state, the script replaces the DEVONthink record’s title to YYYYMMDDHHmmss.

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set recordName to (name of thisRecord)
		set d to (creation date of thisRecord)
		set theYear to year of d as string
        set theMonth to month of d as integer
		set theDay to day of d as integer
		set theHour to hours of d
		set theMinutes to minutes of d
		set theSeconds to seconds of d
		set theId to (theYear & my zero_pad(theMonth as integer, 2) & my zero_pad(theDay, 2) & my zero_pad(theHour, 2) & my zero_pad(theMinutes, 2) & my zero_pad(theSeconds, 2))
		set name of thisRecord to theId
	end repeat
end tell

on zero_pad(value, string_length)
	set string_zeroes to ""
	set digits_to_pad to string_length - (length of (value as string))
	if digits_to_pad > 0 then
		repeat digits_to_pad times
			set string_zeroes to string_zeroes & "0" as string
		end repeat
	end if
	set padded_value to string_zeroes & value as string
	return padded_value
end zero_pad

The script works. It’s nothing special. I can’t help but feel the area were I build theId is a hamfisted approach. Is there a way to make concatenating the month, hours, etc., more elegant?

I played around with a repeat, which failed. I was thinking of building a list of variables, then running each item through a zero_pad subroutine and combining the results into a variable until the entire id string is built.

set {month:theMonth, day:theDay, hours:theHour, minutes:theMinutes, seconds:theSeconds} to d
		set theList to {theMonth, theDay, theHour, theMinutes, theSeconds}
		repeat with a from 1 to length of theList
			set item of theList to my zero_pad(item, 2)
			set listId to listId & item
		end repeat
        set theId to listId

I have no clue what I’m doing. I would be very thankful to anyone who sees the solution and is willing to take time to guide this newb.


I think what you have is just fine. It ain’t broke so don’t try to fix it.

This is the most expert advice one could offer! :grinning:

Formatting dates in AppleScript is a pain unless the format you want happens to match up with the short date, long date, etc. format (which it seldom does). Given that you’ve already done the tedious work, I’d agree with @jec0047 that there’s little to be gained in trying to make the code cleaner. However…

The Unix date command has a way of reformatting dates, and we can use that in our AppleScript. On my machine, where the AppleScript

get current date as text

returns a string in the form

Tuesday, June 23, 2020 at 1:26:15 PM

I can use this AppleScript to reformat the date the way you want:

set myDate to (current date)
set cmd to "date -j -f '%A, %B %e, %Y at %I:%M:%S %p' " & quote & myDate & quote & " +%Y%m%d%H%M%S"
set theID to (do shell script cmd)

This uses the Unix-standard strptime and strftime formatting codes for parsing the input (the string that starts with %A) and writing the output (the string that starts with +%Y). If your computer is localized to return a differently formatted date in AppleScript, you’ll have to change the parsing string. Also, of course, you would replace myDate with d in the definition of cmd.

Is this more efficient than your code? Probably not. And it’s easier to understand only if you’re familiar with strptime and strftime formatting. But it is a lot shorter.

1 Like

I was not aware AppleScript could run shell commands. Like I said, I don’t know what I’m doing.

Would you consider shell commands more reliable, even if it’s not cleaner?

With some searching, you will find a host of options. This one was the shortest and sweetest that I could find.


I don’t think shell scripts are any more or less reliable than AppleScript. Depending on who’s writing the code, one may be easier than the other. I personally can write a shell script much faster than a AppleScript but that’s mostly because I’ve written a lot shell scripts.

I find AppleScripts difficult to write because, without something like Script Debugger, you have very little visibility into what is happening with your code - it either works or (more often) not. I could say that AppleScript’s syntax is obtuse but so is shell scripting.

A script’s reliability comes from the scripter’s understanding of where the pitfalls are (unexpected input is the most common) and how to avoid them. Robust and fragile scripts can be written in any language—it’s up to scripters to take advantage of the tools the languages give them.

Generally speaking, I avoid AppleScript when I can because its toolset is very different from the toolset that I’m used to seeing in the shell, Perl, Python, Awk, and other languages that started on Unix. I often forget to program defensively in AppleScript because of that.


Any word on improvements/changes/deprecations to Applescript or (the redheaded stepchild) Automator in Big Sur?