I’ve no doubt you know this, so this is more for @cyrus if he finds it useful. If name
is a property of the tag
element, one should, in theory, be able to get the list of tag names in one go, like this:
tell application "Evernote" to ¬
set the listOfTagNames ¬
to the name of every tag
It’s especially useful if you had lots of tags, for which a repeat
loop would be slower. It also helps simplify the script somewhat:
tell application "Evernote" to ¬
set the listOfTagNames ¬
to the name of every tag
set the text item delimiters to linefeed
set the clipboard to the listOfTagNames as text
or, even just this:
set the text item delimiters to linefeed
tell application "Evernote" to (set the clipboard ¬
to the name of every tag as text) of me
[ The of me
at the end is because the set the clipboard
command belongs to Standard Additions and not Evernote. It will still work without it, but it’s a good practice to get into from the start. ]
Regarding the original problem that can crop up in other situations, where you might wish to take an AppleScript list
or record
and “stringify”, there is a wee trick one can use to obtain a text representation of pretty much any AppleScript object, with which you can do as you please (such as put it on the clipboard):
to __string__(object)
tell the object to if its class ¬
is text then return it
try
return the data object
on error E
return text 16 thru -2 of E
end try
end __string__
This is a handler, called "__string__"
(although you can name it whatever you like), which is a convenient way to reuse code that you might need to rely upon more than once in a script. You can place it anywhere you like in a script (to keep things neat and tidy, I usually put all my handlers at the bottom of the script), then call it like this:
tell application "Evernote" to set TagList to the tags
set the clipboard to __string__(TagList)
to __string__(object)
tell the object to if its class ¬
is text then return it
try
return the data object
on error E
return text 16 thru -2 of E
end try
end __string__
Briefly, the handler takes any input you give it. If it’s already text
, it just gives it back to you unaltered. Otherwise, it purposely generates an error using the line return the data object
(this is a nonsense line, and doesn’t do anything except throw an error). The try
block catches the error, and stores the error message in the variable E
. An error message is always text, and it always contains the contents of the object that caused the error—in this case, this will be whatever it was you passed to the handler. The first 16 characters of the error message are superfluous to our needs, as are the last two, so these are trimmed away, and the handler gives you back just the text representation of the AppleScript data, which will, of course, include—in the case of a list
object—the curly braces at either end, and the commas separating each item (which will be a good thing or a bad thing, depending on your needs, but not to difficult to remove if you wish to).