Unable to send result of small AppleScript to Clipboard

I am less than a novice.

I have an AppleScript which works perfectly in Script Editor. It generates of list of all my tags in Evernote in the results window, which is extremely convenient, if only because in the Evernote tab view tabs are truncated.

tell application "Evernote"
	set TagList to tags
end tell

I am embarrassed to admit that I am unable to modify the script simply to send the results to the clipboard.

Basically I want to run the script in Keyboard Maestro and simply type Cmd-V to paste the results in an editor of my choice (BBEdit probably).

I tried in vain adding the line

set the clipboard to tags

inspired by a previous post by the excellent @sylumer

I perhaps added the clipboard command in the wrong place in the script.

thanks very much for your time and help !

Pretty sure that you’re getting a list of tags, so take a look at this MacScripter thread on converting lists to delimited strings and see if you can add a few line to get the tags into the format you want.

2 Likes

thank you for your reply.
I look at the link and it is way above my head.
It’s frustrating because i need to get working on the list of tags. When I run the script editor, I can see all the tags in the results pane which I can copy and paste into BBEdit to clean them up with regex.
All I would like to do is automatically send the results to the clipboard to make the process less tedious.
I am frankly flabbergasted that it’s not just a question of adding one of 2 short lines of code to do so.
thanks again very much

I registered in MacScripter, but I can’t see any button or link to create a Post.

Okay, a little more complex as it isn’t a list of strings, but a list of tags. From the AppleScript dictionary for Evernote I could see there was then a name property. So here’s one way to do it. I’ve heavily commented it so you can see exactly how I did it.

tell application "Evernote"
	-- Get all tags as a list of Evernote tags
	set allTags to tags
	
	-- Initialise a list of tag names
	set listOfTagNames to {}
	
	-- Repeat with each tag
	repeat with someTag from 1 to (count of allTags)
		-- Get the name of the tag
		set someTagName to name of item someTag of allTags
		
		-- Add it to the end of the list of tag names
		copy someTagName to the end of the |listOfTagNames|
	end repeat
	
	-- Store the current delimiter
	set originalDelimiter to AppleScript's text item delimiters
	
	-- Set a new delimiter to be a new line
	set AppleScript's text item delimiters to "
"
	
	-- Copy the list using the new delimiter to the clipboard
	set the clipboard to listOfTagNames as text
	
	-- Restore the original delimiter
	set AppleScript's text item delimiters to originalDelimiter
	
	-- Pop-up a notification indicating how many lines/tags are on the clipboard
	display notification (count of allTags) with title "Tags Copied to Clipboard" subtitle "Number of tags:" sound name "Frog"
end tell
---

Hope that helps.
1 Like

thank you VERY much !! extremely kind of you.
I asked you about posting in Macscripter. Turns out it was blocked until I am approved which I am now.

Sorry - it just looked like a statement above rather than a question. But I’ve never posted on MacScripter, so I wouldn’t actually know.

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

1 Like

Very didactic and kind of you to put so much effort into answering my post.
I am very grateful, and will get to work on it right away

1 Like