I like to set my group icons in DEVONthink to colorful custom icons. Copy-pasting icons using the inspector has become tedious, so I wanted to try write a script to set the icon of a selected group to that of its parent. But the code that I thought would work,
tell application id "DNtp"
set rec to (item 1 of (selection as list))
set parentRec to (item 1 of (parent of rec))
set parentThumb to get thumbnail of parentRec
set thumbnail of rec to parentThumb
end tell
does not: it produces an error along the lines of
DEVONthink 3 got an error: Canāt make Ā«data ***89504E470D0A1A....6082Ā» into type anything.
Does anyone know how I can resolve this error and make the script work?
@cgrunenberg would probably know about that. I stumbled about a similar issue some time ago with JavaScript: the data returned by reading the Thumbnail property seems to be not quite what setting the same property requires.
is undefined, i.e. thereās no (direct) result that could be used.
But we can test whether thereās a thumbnail by using a try block.
-- Set child groups's thumbnails to selected group's thumbnail
tell application id "DNtp"
try
set theRecords to selected records
repeat with thisRecord in theRecords
set thisRecord_Type to (type of thisRecord) as string
if thisRecord_Type is in {"group", "Ā«constant ****DTgrĀ»"} then
try
-- test whether this selected group got a thumbnail
set thisRecord_Thumbnail to thumbnail of thisRecord as anything
-- set all child groups' thumbnails to this selected group's thumbnail
set (thumbnail of children of thisRecord whose type = group) to thumbnail of thisRecord
on error
-- do something else, e.g. reset child groups' thumbnails
set thisRecord_ChildGroups to (children of thisRecord whose type = group)
repeat with thisChildGroup in thisRecord_ChildGroups
delete thumbnail of thisChildGroup
end repeat
end try
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
Maybe. However itās only possible to set a recordās thumbnail to another recordās thumbnail directly (see my script), i.e. without storing the thumbnail in a variable.
Yuck. Iād overlooked that in your script. The scripting architecture probably does something ācleverā when reading the property.
So basically, this
should set the thumbnail of toRec to the one of fromRec? Which it doesnāt do here
Edit: Well, the AppleScript equivalent of this script works just as expected. So I guess this is another case of the JS bridge (or whatever it is) not quite behaving as it should, @cgrunenberg?
Iām sorry it wasnāt obvious (and I didnāt explicitly mention) that I was careful to test a case where there was a custom icon on the parent group.
I had seen that before (fantastic work, btw) as well as the related threads (e.g., Can we color the project icon? from 2019), but Iām just not a fan of DEVONthinkās icons, so have been replacing them with custom icons rather than just coloring them.
Ahhhhh ā¦ thatās the part that didnāt occur to me [1].
Yes, indeed. If I change the code to
tell application id "DNtp"
set rec to (item 1 of (selection as list))
set parentRec to (item 1 of (parent of rec))
set thumbnail of rec to thumbnail of parentRec
end tell
In case itās useful for anyone else, hereās the script as it currently stands. This tests that the selection is a group, and when looking at parents, uses the first one that is a group. (The latter is a precaution against the selectionās list of parents containing tag groups. Getting tag groups seems to be possible in other scripts Iāve written, so while I donāt have an actual example of this script running into that case, it seems reasonable to take precautions.)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application id "DNtp"
try
repeat with rec in (selected records)
set recType to (type of rec) as string
if recType is in {"group", "Ā«constant ****DTgrĀ»"} then
my setIconToParentIcon(rec)
else
set recName to name of rec
display notification Ā¬
"Ignoring item because it's not a group: " & recName
end if
end repeat
on error msg number err
if err is not -128 then Ā¬
display alert "DEVONthink" message msg as warning
end try
end tell
on setIconToParentIcon(rec)
-- Items can have multiple parents. Look for the first one
-- that is a group, and use it.
tell application id "DNtp"
repeat with parentRec in (parent of rec)
set recType to (type of parentRec) as string
if recType is in {"group", "Ā«constant ****DTgrĀ»"} then
-- Note: *must* set directly; can't use intermediate variable.
set thumbnail of rec to thumbnail of parentRec
return
end if
end repeat
end tell
end setIconToParentIcon
And thatās only AppleScript: With JavaScript, one canāt even set the thumbnail of one record directly from the thumbnail of another one. Which leads me to the question: In what format is the thumbnail stored, and why do I see a string when I look at the thumbnail property?
I see the same thing, but that doesnāt look very binary to me. In fact, it seems to be a hexadecimal representation (at least after ā****ā). Iāll see if I can figure that out.
Edit: Yes, thatās a hexadecimal representation of the binary data. The string ā504E47ā above corresponds to āPNGā, and one can actually use Perl to transform this beast to a PNG (and possibly other tools, too).
Itās a string representation of the data and its AppleEvent code created by the Script Editor.app for logging. Itās definitely not a string unless you use JXA