How Do I Copy 6 Fields of Text From an Email & Paste Into My Notes App?

I didn't post the entire macro for privacy reasons. I thought the image would be enough, sorry. I will change some things and then send the actual macro.

I readied the AppleScript content part. Not sure where it went, must have deleted it in all my meddling.

And then the last part of what you just said... the variable being set manually - I actually have that disabled, it was just a copy and paste part from the test scripts. BUT - for the parameter piece, should it look like this? :

image

I am cleaning up and going to send full macro shortly.

Timesheet Generator copy.kmmacros (24.8 KB)

Here is the macro with some random content. NOTE: I have not yet updated the variables in the rest of the macro because I disabled it all and was waiting until I did the top portion correctly (after working with you). I will update the variables now.

1 Like

Nowhere in that macro are you actually setting the variable varNHF to anything. It's still a global so will be using whatever value it was last set to.

So you aren't using any parameter you are passing in with the AppleScript, whatever does happen has nothing to do with the email you think you are processing. Also -- and this may just be because you are working on a copy -- the macro UUID is not the same as the one you are using in the AppleScript.

OH MY...

So, and please don't give up on me after reading this.

First, yes the UUID is different because of the copy (sorry, should have updated that as well).

Second, I need help with the part of setting the content to the variable (and we can make it local_NHF so it's not global).

Third, this is where you might give up on me... I misspelled theContent in my applescript. One of the times I had it with an extra "c". thecContent.

I fixed the spelling and it worked. Well the part where the script triggers the macro and the macro displays the content of the email in a text box.

It worked! I have been playing with it all and tweaking and rereading every post in this thread SEVERAL times. I finally got it! The only thing that is contradicting what you told me - in order for it to work, I need to set theContent to varNHF instead of Local_NHF. If I go the global way, it works. If I do the local way, it doesn't. Is there something there I am missing?

Anyways regardless.... thank you for your patience. You have made gotten me to a place where this is functioning and awesome. Thank you so much.

Update: I got the variables to display in the text box. The thing I am noticing - which you warned me about - if I leave NHF as a global variable, it works but only the first time I run it. If I change it to Local, it does not work. Is there something there I am overlooking?

Hey Mike,

Nyet...


If you haven't read this it's worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?


The best way to get help on the forum is to post a proper test macro that demonstrates your issue as simply as possible.

If we're not testing we're guessing...

Reading through macros is tedious and time consuming – especially if you can't download, examine, and test them on your own system. Asking people to do this will frequently get you very little useful help – unless your problem is so obvious it can be seen from space.

Failing to provide test macros asks people to spend an inordinate amount of time and effort on your problem without remuneration.


Please do not use the text variable form: %varNHF%.

While it's (unfortunately) a legal syntax in Keyboard Maestro it is the same syntax as KM's text tokens, and you cannot tell at a glance which of two you're looking at.

%Variable%varNHF% on the other hand can be instantly recognized as a varible and not a text token.

Using this syntax will save you debugging time.


Everyone makes these kinds of mistakes from time to time...

Take Care,
Chris

(Keyboard Maestro Moderator)

2 Likes

Hey Mike,

I don't see how this could be working at all – your AppleScript is never getting the content of each message...

You're using parameter theContent, but you're never setting setting theContent to the content of the message.

I don't like embedding application tells inside of application tells, so this is how I'd write your AppleScript:

--------------------------------------------------------

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
      tell application "Mail"
         repeat with eachMessage in theMessages
            my doKeyboardMaestroMacro(eachMessage's content)
         end repeat
      end tell
   end perform mail action with messages
end using terms from

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on doKeyboardMaestroMacro(msgContent)
   tell application "Keyboard Maestro Engine"
      do script "8A9042AB-81E0-498E-AFEC-A772B6F94CA8" with parameter msgContent
   end tell
end doKeyboardMaestroMacro
--------------------------------------------------------

I don't understand why you're doing this when you never use theContent anywhere else:

image

From what I see you should be setting varNHF to %TriggerValue%.

Probably because the global variable already exists and has a value.

You're not actually setting your local variable to a real value, so the macro will fail, since there's no data to parse.

This is a common problem when testing macros with global variables.

If you don't make certain they're zeroed-out before running your macro again your macro may use old data unexpectedly and cause you minutes or hours of head scratching.

When I test a macro with global variables I always zero them out at either the beginning or end of the macro – unless of course they're supposed to be persistent.

I cost myself a whole lot of debugging time, before I made this a practice.

These days I usually employ local variables from the get-go when developing a macro. It makes debugging a little bit more difficult, but I save time and trouble on the back end by not having to convert from global to local.

(Note – there's a macro or two on the forum that makes this process easier.)

-Chris

3 Likes

Or, in my case, every freakin' day...

2 Likes

Wellll this morning I woke up clear headed. Came at this again. Changed and cleaned and fixed everything you told me to. Made NHF a local variable instead of global. Changed the script, read things over so I can understand what you are telling me (for future). It works. The script works from top to bottom. Repeatedly. Because the NHF is no longer global, it clears every time. I see where setting the trigger value is the crucial part at the top of the macro. Because... obviously if it's not picking up theContent, it is useless. Guys.. I can't stress this enough, thank you.

2 Likes

Actually, one last piece.

The part in the macro where I "save as" needs to have the variables in a certain order as the image shows. That part I got and am able to save it perfectly.

The thing I am missing is saving it as that name, and to a variable in.

I will then be emailing the file. I know the emailing variable part. The part I am stuck on is when I save it as 2022-%Local_Entity%-Timesheet-%Local_Client%-%Local_Name%-%Local_Class% I need to save it to a variable and/or to a different folder as well. So I can then insert the variable into an email.

image

Do you need to keep the file after renaming and emailing it?

You will find life much easier if you save to the same file every time and then rename it with a KM action (using your variables), rather than trying to script the "Save As" dialog to do it all in one operation.

I guess it could be just refilled, saved with the new name (variables) and then sent. So it's the same actual doc each time.

Actually, I just reread your comment. Once the file is sent off, that new file that was created can be deleted or anything. But the initial template file needs to be kept with the same name as before the macro triggered.

I would need the original file's name to stay the same because the rest of the macro pulls the proper excel file based on %Local_Client%.

So when the timesheet for that worker is saved, it needs to be saved with the Variable string name. The original needs to keep it's name.

Given all that, you might be best to stick with what you are doing.

Your post is a bit confusing, mentioning one set of variables while the screenshot shows another -- but I'll assume that's just an old screenie and you're using the correct names...

You might find it easier if you build your file name then use a single value later when you need it. Where does the year at the start come from -- is it the year when you run the macro, or something else (ie what will it be if you run this next January 3rd)? Or are you happy to go into your macro once year and update it?

Assuming a manual update I would make the file name then use it to save the file and later add the attachment:

filename

Then when you save the file you can

rename

(I've used pasting rather than typing because I find that less distracting.)

And when you come to add the attachment later you just add the file name to your folder path (assuming you are always saving into the same folder!)

email

Do understand the assumptions here -- e.g. when you rename the file during the "Save As" the extension remains untouched, so you don't need to include it in the file name you've created. But that means you do need to add it when you say what file to attach (the .xlsx at the end). And, obviously, you'll have to change /path/to/folder/ to the path to the folder where you are saving your documents.

Notice also that we are using the %Variable%Local_fileName% that @ccstone urged you to use earlier. It's a bit more work but we're explicitly telling KM that "this is a variable called Local_fileName" rather than leaving KM to guess what we mean -- and possibly guess wrong...

Beautiful it worked, and I understand it which is more important I think.

I appreciate you understanding my quick copies and rapid screenshots and scattered comments.

Let's say I do want to set it to update the year automatically on the name of the new document... I tried a few variations and it would not work. What would I put instead of 2022 to keep the year automatic?

And while we are in the make it pretty stage, is there a way to keep the file extension if they vary in the template folder? Or should I convert all of the files to the same extension?

The usual way is with the %ICUDateTime% token, formatted so that just the year is output:
%ICUDateTime%yyyy%, which gives you the year at the time the macro runs. You'd include in Local_fileName creation like so:

filenameWithYear

Extensions indicate the file format. You could convert all your spreadsheets to macro-enabled, so they all share the same extension -- in fact I'm surprised they aren't all the same because you currently appear to be locking the spreadsheet for some values of Local_Entity but not for others. But if there's a business reason for such a difference you'll have to abide by it -- you can use similar logic to add the appropriate extension after you've renamed the file but before you attach it to the email.

I am confident in saying.. I think this macro is complete (until I think of something I also need added to it). Thank you so so so much for the incredible help and lessons - and patience.

You're saving an attachment?

Keep in mind that you can save attachments with AppleScript and streamline the process.

Hi Chris. I am saving a file that then becomes an attachment on an email. I have it set as is with setting the file name to a variable before the naming process begins. Nige was a tremendous help. If there is a clearer way, always willing. And want to learn…

Hey Mike,

To emphasize something @Nige_S said:

When developing a macro never try to concatenate and/or calculate strings or paths in an input or output action.

This is really hard to debug.

Always start out by composing your new string in a variable. That way you can easily put a diisplay-in-window action after it to eyeball it for debugging.

This approach is definitely less svelte than is possible, but in the long run it will save you a lot of time and aggravation.

-Chris

Explain what you're saving from where...