# Two pieces of AppleScript: one works, one fails—why?

**URL:** <https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776>\
**Category:** DEVONthink\
**Created:** [July 23, 2021, 11:18am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776 "2021-07-23T11:18:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 11:18am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/1 "2021-07-23T11:18:15Z")

</div>

With apoligies for grimly pursuing something that in another thread has veered into JS, could some kind genius please explain why this works:

```auto
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:

```auto
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!

Stephen

---

<div class="post-metadata">

**Author:** ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)\
**Post date:** [July 23, 2021, 11:35am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/2 "2021-07-23T11:35:33Z")

</div>

The enclosing quotes are only necessary for constant strings like…

`set targetRecord to "Thursday 1 July 2021.md"`

…but they’re not part of the value of targetRecord whereas this line…

`set targetRecord to "\"" & theTarget & "\""`

…adds enclosing quotes to the value and therefore it fails.

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 11:43am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/3 "2021-07-23T11:43:18Z")

</div>

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.

Stephen

---

<div class="post-metadata">

**Author:** ![alastor933](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@alastor933](https://discourse.devontechnologies.com/u/alastor933)\
**Post date:** [July 23, 2021, 11:44am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/4 "2021-07-23T11:44:10Z")

</div>

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:

```auto
set theTarget to "" & wDay & " " & dateDay & " " & dateMonth & " " & dateYear & ".md"

```

The ‘as string’ at the end does nothing.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [July 23, 2021, 11:45am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/5 "2021-07-23T11:45:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [July 23, 2021, 11:50am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/6 "2021-07-23T11:50:21Z")

</div>

> [@alastor933](#):
>
> Also, ‘weekday’ and ‘month’ are AppleScript constants, so it will still fail.

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.

> [@alastor933](#):
>
> Start the concatenation with an empty string

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?

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 11:54am UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/7 "2021-07-23T11:54:01Z")

</div>

> [@alastor933](#):
>
> The ‘as string’ at the end does nothing.

Well it does _something_ because when I ran the script without it and included:

```auto
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.)

Stephen

P.S, I think I am slowly going completely mad!

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [July 23, 2021, 12:09pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/8 "2021-07-23T12:09:52Z")

</div>

> [@Stephen\_C](#):
>
> Well it does _something_ because when I ran the script without it and included:

I simply love it. That’s “I know better than you what you want” at it’s best. Written out, the script does this

```auto
set target to "Friday" & " " & 3 & " " & "May" & " " & 2021

```

which this lovely language encourages to create a list:

```auto
{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

> [@Stephen\_C](#):
>
> I am slowly going completely mad!

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [July 23, 2021, 12:10pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/9 "2021-07-23T12:10:56Z")

</div>

> [@Stephen\_C](#):
>
> Well it does _something_ because when I ran the script without it and included:

I simply love it. That’s “I know better than you what you want” at it’s best. Written out, the script does this

```auto
set target to "Friday" & " " & 3 & " " & "May" & " " & 2021

```

which this lovely language encourages to create a list:

```auto
{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

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 12:11pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/10 "2021-07-23T12:11:36Z")

</div>

> [@chrillek](#):
>
> I simply love it.

I _knew_ I’d get that sort of response from you! It did make me laugh.

Stephen

---

<div class="post-metadata">

**Author:** ![alastor933](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@alastor933](https://discourse.devontechnologies.com/u/alastor933)\
**Post date:** [July 23, 2021, 2:17pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/11 "2021-07-23T14:17:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [July 23, 2021, 2:33pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/12 "2021-07-23T14:33:57Z")

</div>

**Warning** : This post has no relation whatsoever to DT!

> [@alastor933](#):
>
> 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.

`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.

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 3:14pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/13 "2021-07-23T15:14:07Z")

</div>

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.

Stephen

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [July 23, 2021, 4:24pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/14 "2021-07-23T16:24:48Z")

</div>

This is working as expected for me…

```auto
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 😛

 ![Screen Shot 2021-07-23 at 12.23.11 PM](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/d/c/dc57a374bbd9a7b1120d3abb3ef0a8a138eaa63f.jpeg)

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/2/d/2dc4537656f3f38b4d051f3b1626195ded0d8c47.jpeg)

---

<div class="post-metadata">

**Author:** ![alastor933](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@alastor933](https://discourse.devontechnologies.com/u/alastor933)\
**Post date:** [July 23, 2021, 4:51pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/15 "2021-07-23T16:51:02Z")

</div>

> [@chrillek](#):
>
> 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.

Not here, no. When I run

```auto
weekday of (current date) as text

```

I get “Friday”. According to you, it should be “vrijdag”, as I’m running dutch.  
And no, placement of braces doesn’t matter.

Constants are not localised when coercing them to text. Sorry.

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 4:52pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/16 "2021-07-23T16:52:24Z")

</div>

Thanks so much for that…but I _still_ get the following error:

```auto
error "Can’t get item 1 of {}." number -1728 from item 1 of {}

```

I must be doing something stupid. Let me look at it yet again. The only material difference now between my code and yours is my line:

```auto
set theDatabase to open database "/Users/stapp/Documents/DevonThink/Diaries.dtBase2"

```

with the consequent change in the following line.

Stephen

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [July 23, 2021, 4:59pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/17 "2021-07-23T16:59:27Z")

</div>

That error would be because it’s not finding anything.

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/0/b/0b2dbeade55fdc00ebc88b6e82e069dd0069f6f8.jpeg)

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 5:00pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/18 "2021-07-23T17:00:02Z")

</div>

Yes indeed. ☹

Stephen

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [July 23, 2021, 5:00pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/19 "2021-07-23T17:00:36Z")

</div>

```auto
	if foundRecord ≠ {} then
		set theUUID to uuid of item 1 of foundRecord
		return theUUID
	end if

```

This error traps it for no results.

---

<div class="post-metadata">

**Author:** ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)\
**Post date:** [July 23, 2021, 5:01pm UTC](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776/20 "2021-07-23T17:01:17Z")

</div>

But _why_ does it not find a record that clearly exists?

Stephen

[Next page](https://discourse.devontechnologies.com/t/two-pieces-of-applescript-one-works-one-fails-why/65776.md?page=2)
