Pages

Showing posts with label applescript. Show all posts
Showing posts with label applescript. Show all posts

Monday, December 9

Auto Creating Reminders from Email Messages

In my constant state of trying to make things a bit more efficient for myself. (I'm a big believer in automation, ask anyone that has ever worked with me.) We have computers! Make the computers do work instead of us manually doing things on the computer.  

I wanted to find a way to auto-create a reminder from an email. I assume you can figure out how to create a rule to match an email in Mail.app, but one of the actions you can take on a mail rule is "Run AppleScript".

This AppleScript, given the email match, will create a reminder that links back to the email for you:

using terms from application "Mail"

    on perform mail action with messages selectedMessages

        try

            log "Starting script execution"

            tell application "Reminders"

                repeat with theMessage in selectedMessages

                    try

                        tell application "Mail"

                            -- Fetch subject and message ID

                            set emailSubject to subject of theMessage

                            set messageID to message id of theMessage

                            

                            -- Mark email as read

                            set read status of theMessage to true

                            

                            -- Change email color to blue

                            set background color of theMessage to blue

                        end tell

                        

                        -- Log values for debugging

                        log "Email Subject: " & emailSubject

                        log "Message ID: " & messageID

                        

                        -- Construct reminder properties

                        set reminderText to emailSubject

                        set emailLink to "message://%3C" & messageID & "%3E"

                        set reminderNotes to "Link to the email: " & emailLink

                        

                        -- Create reminder in default list

                        tell list "Reminders" -- Change "Reminders" to your desired list name

                            make new reminder with properties {name:reminderText, body:reminderNotes}

                        end tell

                        

                    on error errMsg

                        log "Error processing message: " & errMsg

                    end try

                end repeat

            end tell

        on error errMsg

            log "Script execution error: " & errMsg

        end try

        log "Script finished execution"

    end perform mail action with messages

end using terms from



You need to change the "list" (see "tell list") you want the reminder to go into.

This will take an email, mark it as read, change the background color of the email to blue, create the reminder, and then exit.

You can't put the "message://" link in the URL field directly with AppleScript, as there is no method within the AppleScript reminders dictionary to access "URL". Shortcuts.app can, but AppleScript can't. (I know, right?)

But I have a Shortcut that I execute from the end of this AppleScript that moves it for me. You can execute the shortcut from the AppleScript by putting this after the last "end tell”.

            do shell script "shortcuts run \"URL Mover for Reminders message\""

            log "Shortcut executed successfully"



There's the shortcut. It'll handle both "message://" and "<message:" links (the latter created by OmniFocus if you moved from OmniFocus to Reminders).



Please leave comments below.

Monday, December 18

Deleting Duplicate Notes in Notes.app using AppleScript

I found myself digging through my Notes.app the other day and, lo and behold, there was a whole bunch of duplicate notes hanging around. Pretty sure I goofed and imported them twice or something along those lines.

To fix the mess, I whipped up a quick AppleScript that zapped the duplicates just by looking at the note titles. Since the folder wasn't a big mess and I was dealing with a pretty neat pile, I was cool with the script going on a deleting spree. Just to be safe, though, I did a quick sweep through the "Recently Deleted" folder afterward to make sure nothing important got caught in the crossfire.

Use with caution:

tell application "Notes"

set targetFolder to "Target" -- Change this to your folder name

if not (exists folder targetFolder) then

display alert "Folder not found"

return

end if

set theNotes to notes of folder targetFolder

set noteNames to {}

set duplicates to {}

repeat with aNote in theNotes

set noteName to name of aNote

if noteNames contains noteName then

set end of duplicates to aNote

else

set end of noteNames to noteName

end if

end repeat

repeat with duplicateNote in duplicates

delete duplicateNote

end repeat

if length of duplicates is 0 then

display dialog "No duplicates found"

else

display dialog (length of duplicates) & " duplicates deleted."

end if

end tell




Please leave comments below.

Thursday, December 7

Sort emails by year, using AppleScript

So, way back when I was using Thunderbird, I had this awesome plugin that organized my archived emails by year. It was super handy for finding stuff or just taking a trip down memory lane.

But when I switched to Mail.app, I couldn't find a similar plugin that I liked. Plus, even if one existed, I'd have to install it on every new Mac I got. So, I decided to take matters into my own hands and whip up some AppleScript magic.

In the code, you can pick a folder (sourceFolder) - in my case, it's "Recovered". You also need to specify your account name, like "iCloud" in my code. If your account goes by a different name, like "Exchange" or "Gmail", just tweak that part.

Then there's this variable called "yearsMailbox" at the top of the code, (in my case "Years") which is basically the folder where the emails will end up based on their years. You can rename it if you're feeling fancy.

You can probably make this more efficient by having it map the year folders one time instead of checking every time it wants to move a message, but I feel you're just playing "AppleScript golf" at that point.  This works.  It works great.  Moving on.

tell application "Mail"

-- Specify the parent mailbox where the year folders are located

set yearsMailbox to mailbox "Years" of account "iCloud" -- Adjust if the path is different

-- List of sub-mailboxes under 'Years', each named with a four-digit year

set yearFolders to every mailbox of yearsMailbox

-- Specify the source folder where emails are currently located

set sourceFolder to mailbox "Recovered" -- Replace "SourceFolder" with the name of your folder

repeat with eachMessage in (get messages of sourceFolder)

set messageDate to date received of eachMessage

set messageYear to year of messageDate as string

-- Check if a folder for the year exists and move the message

repeat with eachYearFolder in yearFolders

if name of eachYearFolder is messageYear then

move eachMessage to eachYearFolder

exit repeat

end if

end repeat

end repeat

end tell





Please leave comments below.

Wednesday, September 5

Is Evernote going away?

Evidently we don't know if Evernote is sticking around, since there seems to be some panic on the internet about it today.

https://appleinsider.com/articles/18/09/05/evernote-might-be-in-trouble-so-heres-how-to-get-your-notes-out-of-it-completely-and-safely

It is really easy to move your notes out of Evernote and into Notes.app on the Mac.  (Click the above link for the easy instructions.

However, if you use Hazel, to say, monitor the "Downloads" folder on your Mac, and if any files show up there (like a PDF for example) and you want to have it automatically saved to Notes.app.  (This is handy after you try it).

I wrote a little AppleScript to do this for you, (in Hazel) since I couldn't find one on the Internet, and it wasn't rocket science:

tell application "Notes"
set mynote to make new note at folder "PDFS"
make new attachment at mynote with data theFile
end tell

If you have a folder in Notes.app called "PDFS", it will create a new note in the folder for each file and attach the file to it (which for me, then syncs it with iCloud so the file is on all my devices). Pretty handy.

Anyway, putting this here in case any one else can use it.

Please leave comments below.