With apoligies for grimly pursuing something that in another thread has veered into JS, could some kind genius please explain why this works:
set targetRecord to "Thursday 1 July 2021.md"
try
tell application id "DNtp"
set theDatabase to open database "/Users/stapp/Documents/DevonThink/Diaries.dtBase2"
set foundRecord to (contents of theDatabase whose name is targetRecord)
set theUUID to uuid of item 1 of foundRecord
return theUUID
end tell
but this (infuriatingly) does not:
set dAnswer to "01/07/2021"
set userEntry to display dialog ¬
"Enter the link date as DD/MM/YYYY" default answer dAnswer
set userDate to text returned of userEntry
set linkDate to date userDate
set wDay to weekday of linkDate
set dateDay to day of linkDate
set dateMonth to month of linkDate
set dateYear to year of linkDate
set theTarget to wDay & " " & dateDay & " " & dateMonth & " " & dateYear & ".md" as string
set targetRecord to "\"" & theTarget & "\""
try
tell application id "DNtp"
set theDatabase to open database "/Users/stapp/Documents/DevonThink/Diaries.dtBase2"
set foundRecord to (contents of theDatabase whose name is targetRecord)
set theUUID to uuid of item 1 of foundRecord
return theUUID
end tell
I shall go away for while when this is sorted…I promise!
Thanks for the quick reply. That now works with the default answer (in the second piece of code) but not with the string assembled from the initial code (i.e., with a date I have input) - even though I’ve removed the line which adeed the quotation marks.
Also, ‘weekday’ and ‘month’ are AppleScript constants, so it will still fail.
Start the concatenation with an empty string, and what follows will be coerced to text automagically:
Completely unrelated: I suggest not using a name like targetRecord when the variable contains a string (but not a record). “targetName” is better or anything that does not hint to an wrong object type.
Will it? Given that @Stephen_C says set wday to weekday of linkDate, I don’t quite see how that goes against “weekday” being an AS constant. But I don’t know AS well, everyhting’s of course possible.
Shouldn’t AS automagically infer that “theTarget” is a string if it’s value is a concatenation of strings? Isn’t that one of the arguments for weakly typed languages, that they DWYM?
Well it does something because when I ran the script without it and included:
return class of theTarget
the result was “list”. With as string added the result is “text”.
I don’t entirely understand (sorry!) the point about the AppleScript constants. I’ve tested fairly thoroughly and what I end up with as the targetRecord is clearly a string in the exact format of the record name I’m seeking. (Also taking on board the comment from @chrillek about the naming of the variable.)
I simply love it. That’s “I know better than you what you want” at it’s best. Written out, the script does this
set target to "Friday" & " " & 3 & " " & "May" & " " & 2021
which this lovely language encourages to create a list:
{Friday, 3, May, 2021}
Never mind that the first value is already a string - oh wait, it is not. Of course, we mere mortals would expect weekday to be just a sequence of characters. Not so Apple in its wisdom: It is a constant. (The same goes for the month “name”, btw). Only if you add as string (to wday or at the end of the string) will you get a string. I can understand that @Stephen_C thinks
I simply love it. That’s “I know better than you what you want” at it’s best. Written out, the script does this
set target to "Friday" & " " & 3 & " " & "May" & " " & 2021
which this lovely language encourages to create a list:
{Friday, 3, "May", 2021}
Never mind that the first value is already a string - oh wait, it is not. Of course, we mere mortals would expect weekday to be just a sequence of characters. Not so Apple in its wisdom: It is a constant. (The same goes for the month “name”, btw). Only if you add as string (to wday or at the end of the string) will you get a string. I can understand that @Stephen_C
Yes. Sorry. Apparently I can type or think, but not at the same time…
I was thinking about coercing lists to text, which you might do by adding ‘as string’ or ‘as text’.
AppleScript will silently coerce list items that are not text to text, but there are things that it cannot convert, and you’ll have an error.
So i wanted to make clear that the list you are building in your script contains things that are not text. Luckily, AppleScript constants can be coerced to text, so all seems well.
But try it with a list where one item is a record, and you’ll get an error.
I hope I expressed myself more clearly this time.
Warning: This post has no relation whatsoever to DT!
weekday of date returns a constant (Monday, Tuesday etc.), not a string. And apparently (and contrary to my expectations), this constant is not silently coerced to text. It just builds a list if you have this constant as the first term of a collation.
The wonderful thing (ok, it is anything but) is that weekday of date as string actually returns a string, but in the system wide language setting. So the constant Monday is converted to the string “Monday” in any en locale, but to “lundi” in any fr locale, “Montag” in any de locale, and yí something in Mandarin. You as a programmer can’t influence this. So if you want to work with french weekdays on a machine where the system wide language is set to Mandarin … good luck.
As a side note: In “the other scripting” language, you can just set the locale and the format you want the values to be (short, long, numeric, two digits - whatever). Regardless of system settings.
I’m grateful for everyone’s comments which are, of course, interesting. However, in order that the thread is not completely derailed I’d be even more grateful if someone could tell me why, when I’ve included the amendment mentioned by @cgrunenberg, the second piece of code works using the default input but not with any other input. I assume if I’d been doing something stupid others would by now have pointed it out - so I’m still completely baffled.
set dAnswer to "01/07/2021"
set userEntry to display dialog ¬
"Enter the link date as DD/MM/YYYY" default answer dAnswer
set userDate to text returned of userEntry
set linkDate to date userDate
set wDay to weekday of linkDate
set dateDay to day of linkDate
set dateMonth to month of linkDate
set dateYear to year of linkDate
set targetRecord to (wDay & " " & dateDay & " " & dateMonth & " " & dateYear & ".md") as string
tell application id "DNtp"
if not (exists database "11") then
open database "~/Databases/Misc and Test Databases/Dummy Databases/11.dtBase2"
end if
set foundRecord to (search "name:" & targetRecord) in (database "11")
set theUUID to uuid of item 1 of foundRecord
return theUUID
end tell
Noting the foundRecord line could be written in different ways. I wouldn’t - and did not - use the contents… whose name construct.
And no, I didn’t error trap the open database line